0

I need to convert a JSON response into Java Object, but I am gettin nullpointerException. Here is my model class:

public class Cheque {

private String payeeName;
private String accountNumber;
private String ifsCode;
private String micr;
private String bankName;

public Cheque() {
    super();
    // TODO Auto-generated constructor stub
}

public Cheque(String payeeName, String accountNumber, String ifsCode, String micr, String bankName) {
    super();
    this.payeeName = payeeName;
    this.accountNumber = accountNumber;
    this.ifsCode = ifsCode;
    this.micr = micr;
    this.bankName = bankName;
}


public String getPayeeName() {
    return payeeName;
}

public void setPayeeName(String payeeName) {
    this.payeeName = payeeName;
}

public String getAccountNumber() {
    return accountNumber;
}

public void setAccountNumber(String accountNumber) {
    this.accountNumber = accountNumber;
}

public String getIfsCode() {
    return ifsCode;
}

public void setIfscCode(String ifsCode) {
    this.ifsCode = ifsCode;
}

public String getMicr() {
    return micr;
}

public void setMicr(String micr) {
    this.micr = micr;
}

public String getBankName() {
    return bankName;
}

public void setBankName(String bankName) {
    this.bankName = bankName;
}

Below I am posting the method in which I am invoking a Python program and getting a Json response from it:

public class RunPython {

public static void main(String[] args) throws IOException,ScriptException,NullPointerException{
    // TODO Auto-generated method stub
    Process p = Runtime.getRuntime().exec("python <path to file>/reg.py");
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line ;
    try {
        while((line =in.readLine()) != null) {
            System.out.println(line);
        }
   } finally {
     in.close();
   }

    ObjectMapper mapper = new ObjectMapper();
    try { 

        Cheque cheque = mapper.readValue(line, Cheque.class); 
        System.out.println("Java object :"); 
        System.out.println(cheque); 
        }
    catch (JsonGenerationException ex) { 
        ex.printStackTrace(); 
        } 
    catch (JsonMappingException ex) {
        ex.printStackTrace(); 
        } 
    catch (IOException ex) { 
        ex.printStackTrace(); 
        }}}

The JSON response which I am getting is:

{
"bankName": [
    "Bank"
],
"accountNumber": [
    "989898989898"
],
"ifsCode": [
    "0000000"
],
"micr": [
    "0000000"
],
"payeeName": [
    "name"
]

}

After running the program I am getting the JSON response as expected, but while converting it to a Java object it is showing nullPointerException in the main thread. Help me out to find where I am making the mistake.

Yurizen
  • 11
  • 4

3 Answers3

1

You consume/exhaust all your Process Inputstream here when printing it :

 try {
        while((line =in.readLine()) != null) {
            System.out.println(line);
        }

And later on when you call the below it's already null :

Cheque cheque = mapper.readValue(line, Cheque.class); 

You'd have to process it in the above while loop, instead of only printing it out

nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

When you reach the statement:

    Cheque cheque = mapper.readValue(line, Cheque.class); 

variable line is null.

So you can either rebuild the JSON string (with a StringBuilder), or remove the code that prints the response, and parse the JSON directly from p.getInputStream().

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
-1

Your Java object has String value, but JSON has array type ([ ])

Taavi Kivimaa
  • 242
  • 1
  • 7
  • that is true but a) not very well worded for OP to understand and b) irrelevant in terms of the exception. – luk2302 Feb 18 '19 at 15:35