-1

I have been looking everywhere for an example of how to isolate a null issue.

I'm being sent a Json string and I'm checking for certain values but sometimes the object (colUniqueID) is not present in the Json string.

{
    "mitigant": {
        "productUnid": "123456789123456789",
        "createdDate": "",
        "basketNumber": "bb012456",
        "shareIndicator": "",
        "assetDetails": [
            {
                "alias": {
                    "createdDate": {"value": ""},   
                    "assetID": {"value": "1111"},
                    "assetNumber": {"value": "aaa123456"},
                    "colUniqueID": {"value": ""}
                }
            }
        ]
    }
}
String assetNum = collateral.getMitigant().getAssetDetails().get(i).getAlias().getColUniqueID().getValue();

if (assetNum != null) {
//Do something
}

But I get "HTTP JVM: java.lang.NullPointerException" error when I declare the assetNum variable. So it doesn't even get to the "if" statement

Any ideas??

Please help.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Eugene
  • 1
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – vincrichaud May 03 '19 at 09:54

1 Answers1

1

Just break a little before the exception:

ColUniquueID id = collateral.getMitigant().getAssetDetails().get(i).getAlias().getColUniqueID();

if (id == null) {
  // manage the case
} else {
  String assetNum = id.getValue()
}


ADDING:

The NullPointerException occurs when you try to access at the value of your colUniqueID object, that is null.

if you call any method of a null object you will have a NPE, so before call getValue() on a possible null, you must check if it is null or not.

Frighi
  • 475
  • 4
  • 17