Lilu Patel
Lilu Patel

Reputation: 321

JsonMappingException even if checking for null values

I am getting an exception when doing a null and empty check for a string that I am passing to an ObjectMapper Json Parser method. This is my code here:

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

@RequestMapping(value = "/url", method = RequestMethod.GET)
    public ModelAndView displayStuff(
            @RequestParam(value = "item") int itemCode) throws JsonProcessingException, IOException {


           if (!text.equals("") || text != null) {
                JsonNode jsonThresholds = OBJECT_MAPPER.readTree(text);
                do stuff()..

          }else {
               do nothing();
          }
     }

I am getting this exception:

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: java.io.StringReader@182d2fd7; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:2840)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2782)
    at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:1659)

Could someone please explain to me what is happening and how I can fix this.

Thankyou

Upvotes: 0

Views: 946

Answers (1)

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9900

change

!text.equals("") || text != null

to

text != null && !text.equals("")


because even text="" then text != null become true and goes to inside of if..

you should first check null or not because if value is null .equls will give nullpointer exception

this is wrong too

 !text.equals("")  &&  text != null 
       ↑
     null pointer Exception can be occurred here 

Upvotes: 2

Related Questions