0

I am trying to parse the JSON data which is present in datas.json.I need to parse the Exclusion datas using Beans/POJO classes.The JSON data is as follows:
"ExclusionList" : { "serviceLevel" : ["sl1","sl2","sl3"], "item" : ["ABC","XYZ"] } }

I've created POJO classes for the ExclusionList but unable to get and print in the console of ECLIPSE IDE.Mypojo class:

List<String> serviceLevel;
List<String> item;
//with gettter and setters.

My main class is as follows:

public static void main(String[] args)
            throws JsonParseException, JsonMappingException, IOException, ParseException, JSONException {
    File jsonFile = new File("C:\\Users\\sameepra\\Videos\\datas.json");
    ObjectMapper mapper = new ObjectMapper();  
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    ExclusionList exclusionlist=mapper.readValue(jsonFile,ExclusionList.class);
    List<String> excllist=exclusionlist.getServiceLevel();
    for (int i = 0; i < excllist.size(); i++) {
        System.out.println(excllist.get(i));
    }
}

Getting error as Exception in thread "main" java.lang.NullPointerException

4
  • have you initialized List<String> excllist ?? Commented Mar 1, 2019 at 6:50
  • Its already inititalized on that provided line as String: Commented Mar 1, 2019 at 6:53
  • String type List* Commented Mar 1, 2019 at 7:03
  • post your console log Commented Mar 1, 2019 at 7:04

1 Answer 1

1
You need to wrap your pojo class in another containing an ExclusionList property.
Try this. The examples below uses lombok for getters , setters and default constructor.


import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Data;

@Data
public class ExclusionListWrapper {

    @JsonProperty("ExclusionList")
    private ExclusionList exclusionList;

    @Data
    class ExclusionList {
        List<String>    serviceLevel;
        List<String>    item;

    }

    public static void main(String[] args) throws Exception {
        String data = "{\"ExclusionList\" : {\"serviceLevel\" : [\"sl1\",\"sl2\",\"sl3\"], \"item\" : [\"ABC\",\"XYZ\"]}}";
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        ExclusionListWrapper exclusionlistWrapper = mapper.readValue(data, ExclusionListWrapper.class);
        List<String> excllist = exclusionlistWrapper.getExclusionList().getServiceLevel();
        for (int i = 0; i < excllist.size(); i++) {
            System.out.println(excllist.get(i));
        }
    }

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

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.