0

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

3
  • show json string please. Commented Oct 21, 2014 at 5:53
  • something like this: { "happy" : 3, "sad" : 4, "ok" : 5 } Commented Oct 21, 2014 at 5:54
  • change it to !text.equals("") && text != null that may work.. Commented Oct 21, 2014 at 5:57

1 Answer 1

2

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 
Sign up to request clarification or add additional context in comments.

7 Comments

perhaps text.trim().equals(""), to get rid of the empty space
Thank you, it works for empty strings but if there is no string, I still get a null pointer exception. Why would this be?
obj.toString().equals("{}"); this could be what u looking for in that case.
@LiluPatel OBJECT_MAPPER is null then
@LiluPatel check my updated answer .actually you should swap null check and empty check.first you should check null or not
|

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.