https://kotlinlang.org logo
#announcements
Title
# announcements
e

ericksli

09/07/2017, 7:20 AM
I just found a problem in my code. When I use
Array<CreditCardType>
for
types
, the first element of the array is null. However, when I use
List<CreditCardType>
, it works normally. https://pastebin.com/u1LiT2im
d

diesieben07

09/07/2017, 7:22 AM
When using an array, how are you creating that array?
e

ericksli

09/07/2017, 7:23 AM
private val types: Array<CreditCardType> = arrayOf(Visa, MasterCard, AmericanExpress, DinersClub, Discovery, Jcb, UnionPay, Maestro)
k

karelpeeters

09/07/2017, 7:29 AM
Can you try to create a minimal example that demonstrates the problem?
e

ericksli

09/07/2017, 7:43 AM
I tried in my android project and the first element is null But I tried it in try.kotlinlang.org it works perfectly
I got error like this when I am in my Android Project java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String CreditCardType.getName()’ on a null object reference at CreditCardType$Companion.matchName(CreditCardTypes.kt:29)
k

karelpeeters

09/07/2017, 8:00 AM
Again, try to create a minimal example. Remove the object implementations, make the list shorter, remove irrelevant methods, ...
e

ericksli

09/07/2017, 8:27 AM
I have tried in a new andriod project and it works. But it doesn’t work in my project
I found out how to trigger the null element: When I set a var field of
CreditCardType
in a data class with default value of Visa, the Visa object in array/list will become null
d

diesieben07

09/07/2017, 9:16 AM
Please can you show the code...
e

ericksli

09/07/2017, 9:29 AM
The data class:
Copy code
@PaperParcel
data class PaymentMethod(
        var id: Int = -1,
        // skip other fields...
        var issuer: CreditCardType? = null,
        var type: String = TYPE_CREDIT,
       
) : Parcelable {
    companion object {
        @JvmField
        val CREATOR = PaperParcelPaymentMethod.CREATOR

        const val TYPE_CREDIT = "credit"
        const val TYPE_DEBIT = "debit"
    }

    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) {
        PaperParcelPaymentMethod.writeToParcel(this, dest, flags)
    }
}
If I change issuer to:
Copy code
var issuer: CreditCardType = Visa
the array/list will become null for Visa
2 Views