0

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?

3 Answers 3

1

I think there is 2 mistake in your code.

The first:

if (itinerary.getItems().stream().flatMap  // 
        (item -> Arrays.stream(item.getClass().getDeclaredFields()))
        .map(field -> { field.setAccessible(true); return field.get(nu) == null; } ) ) {
    return true;
}

the result of .map method is a Stream<Boolean>,not a bool expression.So there will be a syntax error and not be compiled.

The second:

return field.get(nu) == null;

this code will throw NullPointerException while the filed is an instance field.And the method get in Field can throw some Exception,so should try-catch on it.

Sign up to request clarification or add additional context in comments.

Comments

1

The doc for Field.get

Returns the value of the field represented by this Field, on the specified object. The value is automatically wrapped in an object if it has a primitive type.

See, how it says on the specified object? That's because the Field object is no longer associated with a specific object.

Which means that Field.get operation need to be passed the item object, not the null assigned nu object you are currently passing.

This seems really tedious, do try to use a library like this for JSON schema validation.

Comments

0
Object nu = null;

You are trying to get 'nu' which is a Null Object. That's the reason you are getting an IlligalAccessException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.