0

how do get string array from JSONArray ?

String[] d;

protected void parseJsonString(ArrayList<String[]> deptList,String json){
        try {
            JSONArray array=new JSONArray(json);
            int dl = array.length();
            System.out.println(dl);

            for(int i=0;i<array.length();i++){
                d[i] = array.getString(i);

            }
            deptList.add(d);
            for (String e : d) {
                System.out.println(e);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

I debuging this code and I get an error NullPointerException here:

d[i] = array.getString(i);

2 Answers 2

1

you forgot

String[] d = new String[array.length];

So String array d is never initialised.. and you try it access its 1st element saying d[i]

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

Comments

0

You did not initialize your string array:

int dl = array.length();
System.out.println(dl); 
String[] d = new String[dl];

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.