Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap fails for null Boolean feed #22

Closed
dbachelder opened this issue Mar 3, 2014 · 18 comments
Closed

wrap fails for null Boolean feed #22

dbachelder opened this issue Mar 3, 2014 · 18 comments

Comments

@dbachelder
Copy link
Contributor

I have a Boolean field in my parcel which can sometimes be null...

the generated code fails with an NPE:

        parcel$$1 .writeBooleanArray(new boolean[] {InjectionUtil.getField(java.lang.Boolean.class, com.company.ValueObject.class, valueObject$$0, "isFeatured")});
@johncarl81
Copy link
Owner

Is the boolean[] field itself null or does it contain nulls?

@dbachelder
Copy link
Contributor Author

It's not even a boolean[] field... the def is:

Boolean isFeatured;

@johncarl81
Copy link
Owner

Oh, I see... will fix tonight.

@johncarl81
Copy link
Owner

Now that I think about it.. I bet this is a problem for all Object version of primitives (Integer, Float, etc). We should probably have a null check for all of them.

@dbachelder
Copy link
Contributor Author

Yeah.. I think I'm going to hit this problem quite a bit in these classes... lots of primitive wrappers..

@johncarl81
Copy link
Owner

this should do the trick: 73d8d68 Deployed under 0.2.8-SNAPSHOT. Let me know if it works for you.

@dbachelder
Copy link
Contributor Author

Awesome! Thanks! I'll try it shortly

@dbachelder
Copy link
Contributor Author

I think this fixed my problem... but is this generated code for an Integer correct?

        int int$$3 = parcel$$0 .readInt();
        java.lang.Long long$$0;
        if (int$$3 < 0) {
            long$$0 = null;
        } else {
            long$$0 = parcel$$0 .readLong();
        }
        listingEntity$$0 .listingId = long$$0;

That looks like I can't have negative values in an Integer field?

@dbachelder
Copy link
Contributor Author

er.. sorry, Long

@johncarl81
Copy link
Owner

There are two variables in play here. The int value is there as a flag to determine if the Long is null or not.

        int int$$3 = parcel$$0 .readInt(); //null flag
        java.lang.Long long$$0; // eventual value
        if (int$$3 < 0) { // <- is the value null?
            long$$0 = null;
        } else { // nope...
            long$$0 = parcel$$0 .readLong();
        }
        listingEntity$$0 .listingId = long$$0;

@dbachelder
Copy link
Contributor Author

Ahhh, duh, got it... thanks. Seems like everything is working great then!

@johncarl81
Copy link
Owner

I was thinking about defining a null constant so that this code is more readable:

        int int$$3 = parcel$$0 .readInt();
        java.lang.Long long$$0;
        if (int$$3 == IS_NULL) {
            long$$0 = null;
        } else {
            long$$0 = parcel$$0 .readLong();
        }
        listingEntity$$0 .listingId = long$$0;

I use this technique all over the place, especially in collections and arrays where -1 represents null, 0 represents empty and > 0 represents the size of the collection:

        int int$$18 = parcel$$12 .readInt();
        java.util.ArrayList<org.parceler.SubParcel> list$$4;
        if (int$$18 < 0) {
            list$$4 = null;
        } else {
            list$$4 = new java.util.ArrayList<org.parceler.SubParcel>();
            for (int int$$19 = 0; (int$$19 <int$$18); int$$19 ++) {
                list$$4 .add(...);
            }
        }
        converterTarget$$0 .parcelList = list$$4;

@dbachelder
Copy link
Contributor Author

That certainly would have prevented my stupid question :)

@dbachelder
Copy link
Contributor Author

Still hitting this issue in public void writeToParcel(android.os.Parcel parcel$$1, int flags):

parcel$$1 .writeBooleanArray(new boolean[] {listingEntity$$0 .isFeatured });

where isFeatured is a null Boolean

@dbachelder dbachelder reopened this Mar 4, 2014
@johncarl81
Copy link
Owner

Robolectic's lack of Parcelable support is biting me... Looks like I missed
Boolean and Character. Will fix shortly.
On Mar 4, 2014 4:53 PM, "dbachelder" notifications@github.com wrote:

Still hitting this issue in public void writeToParcel(android.os.Parcel
parcel$$1, int flags):

parcel$$1 .writeBooleanArray(new boolean[] {listingEntity$$0 .isFeatured
});

where isFeatured is a null Boolean

Reply to this email directly or view it on GitHubhttps://github.com//issues/22#issuecomment-36694495
.

@johncarl81
Copy link
Owner

try again with 0.2.8-SNAPSHOT

@dbachelder
Copy link
Contributor Author

looks good, thanks

@johncarl81
Copy link
Owner

Finally... thanks for sticking with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants