question

Danielle Cox avatar image
Danielle Cox asked Danielle Cox commented

LineItem.getIsRevenue() has null pointer exception

 LineItem testItem = new LineItem("{\"createdTime\":1493752362963,\"taxRates\":{\"elements\":[{\"id\":\"H81CTR0MNB502\",\"isDefault\":true,\"name\":\"Broward Sales\",\"rate\":650000}]},\"userData\":null,\"alternateName\":\"\",\"id\":\"HWS8WYG986J58\",\"exchanged\":false,\"price\":1100,\"refunded\":false,\"binName\":null,\"isRevenue\":null,\"name\":\"Custom Item\",\"printed\":false,\"note\":null,\"itemCode\":null}");
boolean foo = testItem.getIsRevenue();
Log.d(TAG, "Initialization: " + foo);

generates a nullPointerException. The lineItem this was taken from was a simple custom item created in register.

10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Jeffrey Blattman avatar image
Jeffrey Blattman answered Danielle Cox commented

You are getting an NPE because you are unboxing a null.

12 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Danielle Cox avatar image Danielle Cox commented ·

Except I have been told that getIsRevenue() will return true if that value is null. If you call hasIsRevenue() in that example, it returns true. Please see https://devask.clover.com/question/76... where I was told that getIsRevenue() will default to true. I am working around this with a try/catch, but this is not behaving how Sam described it, and your way doesn't make sense (since hasIsRevenue() returns true and so there is no way to tell if it has null or not since there is not a not null function for this. I am interested in what you think the official way is.

0 Likes 0 ·
Danielle Cox avatar image Danielle Cox commented ·

I am also interested then if you agree with Sam that the default behavior in the case of null should be that it is revenue?

0 Likes 0 ·
Danielle Cox avatar image Danielle Cox commented ·

BTW, This line item was created from by adding a "custom item" to the order inside register.

0 Likes 0 ·
Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

In your example, you create a new line item and set the value for "is revenue" to null. In that case it's going to return null. You can look at the source of the SDK and see why that is. The LineItem class is just a simple data object and doesn't encapsulate any logic about what fields can be null, or not. What the link you posted says is that in our DB, the column for "is revenue" may not be null. If you fetch an order from the server, it should return a non-null "is revenue" field.

0 Likes 0 ·
Danielle Cox avatar image Danielle Cox commented ·

The Lineitem I am using there was created by creating a custom item in register, I used the logic in the question to try to give you something to test. So, to be clear here, a custom item in register creates a lineItem where isRevenue is set to null. hasIsRevenue() returns true. I am checking to see if an item is revenue. In a previous question, Sam FROM Clover, said that getIsRevenue should return true in the case of it being null. I would be satisfied if it returned the null. Instead the call to getIsRevenue throws an exception. You are saying this is correct behavior and Sam is wrong?

0 Likes 0 ·
Show more comments

Welcome to the
Clover Developer Community