Tips To Fix “no content to map due to end-of-input error”

JSON is known as JavaScript Object Notation, which is ideally used to easily interchange data. It gives access to data interchange format that too in a human-readable form. It is also a format for structured data. Being a programmer-friendly language, it is simple to parse and use it. When working with JSON, there are many libraries that can make the programming smooth, and Jackson is one of those libraries. Working with any library needs programmers, especially newbies to understand the library functions to avoid errors. You may experience the exception “no content to map due to end-of-input error”.

Go ahead with the article to get the error fixed. Let’s check out how the error occurs

How the error warning occurs

When you are using the Jackson parser library to part the JSON string, you get the following mapping-exception

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: java.io.StringReader@421ea4c0; line: 1, column: 1]

Have a look at the program code you may use to parse

StatusResponses loginValidator = null;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);

try {
    String res = result.getResponseAsString();//{"status":"true","msg":"success"}
    loginValidator = objectMapper.readValue(result.getResponseAsString(), StatusResponses.class);
} catch (Exception e) {
    e.printStackTrace();
}

The StatusResponse class may look like this:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "status","msg" })
public class StatusResponses {

    @JsonProperty("status")
    public String getStatus() {
        return status;
    }

    @JsonProperty("status")
    public void setStatus(String status) {
        this.status = status;
    }

    @JsonProperty("msg")
    public String getMessage() {
        return message;
    }

    @JsonProperty("msg")
    public void setMessage(String message) {
        this.message = message;
    }

    @JsonProperty("status")
    private String status;

    @JsonProperty("msg")
    private String message;

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonGetter
    public Map<String, Object> getAdditionalProperties() {
        return additionalProperties;
    }

    @JsonSetter
    public void setAdditionalProperties(Map<String, Object> additionalProperties) {
        this.additionalProperties = additionalProperties;
    }
}

This overall program code returns the error message.

How To Fix the Error “no content to map due to end-of-input error”

The error happens when an empty InputStream is passed to the ObjectMapper.readValue. The other possibility to have an error is on the client side as the server didn’t shut that you are working on. Have a look at the solutions to fix the error

Solution 1 – Close the stream

Sometimes, you forgot to close the stream you are working on or writing to the server, which results in the error message. Closing the stream can fix the error. Use the below code to close

OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); 
out.write(jsonstring.getBytes()); 
out.close() ; //This is what I did

It resolves the error warning.

Solution 2 – Follow the code

You need to implement the following code to get the error resolved

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;

StatusResponses loginValidator = null;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);

try {
    String res = result.getResponseAsString();//{"status":"true","msg":"success"}
    loginValidator = objectMapper.readValue(res, StatusResponses.class);//replaced result.getResponseAsString() with res
} catch (Exception e) {
    e.printStackTrace();
}

Conclusion

We have highlighted the solutions that can help you fix the error “no content to map due to end-of-input error” in a simpler way. You implement the solutions to remove the error warning.

I hope you find it helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *