I want to check if my data has null value. The data in JSON format is below:
"{
"totalAmount" : 20000,
"items" : [
{
"id" : "9fd91eaf-2b9a-4bae-9692-4222f85e868",
"name" : "X hotel",
"itemType" : 0,
"orderCount" : 2,
"amount" : 5000
},
{
"id" : "9fd91eaf-2b9a-4bae-9692-4222f85e869",
"name": "Special Dinner",
"itemType" : 1,
"orderCount" : 1,
"amount" : 10000
}
]
}"
And here is my checker logic.
class NullCheck {
public checkNull (Itinerary itinerary)
Object nu = null;
if (itinerary == null) {
return true;
} else if (itinerary.getTotalAmount() == null || itinerary.getItems() == null) {
return true;
} else if (itinerary.getItems().contains(null)) {
return true;
} else if (itinerary.getItems().stream().flatMap //
(item -> Arrays.stream(item.getClass().getDeclaredFields()))
.map(field -> { field.setAccessible(true); return field.get(nu) == null; } ) ) {
return true;
}
}
However, IlligalAccessException is thrown at field.get(nu), and I can't figure out why my code doesn't work well.
As I read java document, It seems correct in syntax.
Can I ask which part of my code is failed?
further, some of the json format data is converted to object of type String, so I also need to check if the string is empty string or not.
Is there easy way to do it?