-1

Getting Null Pointer exception JSON AJAX /Servlet Ajax post call which call the below servlet

 $.ajax({
            url: '/bin/commersenext/assetUpload',
            type: 'POST',
            contentType:'application/json',
            data: JSON.stringify(ob),
            dataType: 'json',
          success:function(msg){
            alert("data"+msg);
              console.log(msg);
            }

        });

Servlet Code - where nullpointerexception occurred:

    JSONParser parser = new JSONParser();
            JSONArray jsonArray = null;
            String tabledata = request.getParameter("data");


                  try{
                     Object obj = parser.parse(tabledata);
                     JSONArray array = (JSONArray)obj;


                     System.out.println(array.get(1));
Akki
  • 1
  • 3
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – DanielBarbarian Jun 13 '17 at 05:39
  • First of all you should have a look at this: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it, second it usually helps if you provide a stacktrace, especially one which tells us at which line the exception occurs. – DanielBarbarian Jun 13 '17 at 05:43

2 Answers2

0

Try to put this condition in your code.

if(data.status == 0){
    var value = JSON.parse(data.data);
    console.log("data saved");
    return;
}       
Pang
  • 9,564
  • 146
  • 81
  • 122
Subhash Choudhari
  • 73
  • 1
  • 2
  • 12
0
    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
    String json = "";
    if(br != null){
        json = br.readLine();
        System.out.println(json);
    }

Its working now..!!!

Akki
  • 1
  • 3