0

I'm pretty new to android dev and I need some help.

I'm building an agenda that loads the information from a JSON then a ListView is inflated with a custom adapter. I've done this and works just fine.

My problem is the following when I click a contact another Activity is loaded with more information about the user, using the same JSON. I debug it and it recieves the information like this:

Example Item: [{"id":1,"name":"Leanne Graham","hobby":"Play soccer","address":"Kulas Light, Gwenborough","phone":"1-770-736-8031 x56442"}]

Because I sent the information as a JSONObject I cast it to be a JSONArray, but when I pass that array to my requestComplete my app breaks.

The error is:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

/**Main activity onclick listener*/

 @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    System.out.println("POSITION: " + position);
    JSONObject jsonObject = (JSONObject)JSONadapter.getItem(position);


    Intent intent = new Intent(this, InfoActivity.class);
    String pos_json = jsonObject.toString();
    intent.putExtra("pos_json",pos_json);


    startActivity(intent);

}

/**Info activity*/

public class InfoActivity extends AppCompatActivity implements JSONRequest.JSONCallback {

AdapterInfo JSONAdapter;
private ListView listInfo;
private JSONObject json_object;
private JSONArray arrayMain;

private ArrayList<String> jsonarray;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info);

    JSONArray array = new JSONArray();

    try {
        json_object = new JSONObject(getIntent().getStringExtra("pos_json"));
        arrayMain = array.put(json_object);


        System.out.println("Example Item: "+ arrayMain.toString());
        System.out.println(arrayMain.getClass().getName());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    requestComplete(arrayMain);


    this.listInfo = (ListView) findViewById(R.id.listView2);


}



@Override
public void requestComplete(JSONArray array) {
    JSONAdapter = new AdapterInfo(InfoActivity.this,array);
    this.listInfo.setAdapter(JSONAdapter);


}

/**Adapter*/

public class AdapterInfo extends BaseAdapter{

private JSONArray array;
private Activity infoAct;

public AdapterInfo(Activity infoAct, JSONArray array){
    this.array = array;
    this.infoAct = infoAct;
}


@Override
public int getCount() {
    if(array == null){
        return 0;
    }else{
        return array.length();
    }
}

@Override
public JSONObject getItem(int position) {
    if(array == null){
        return null;
    }else{
        return array.optJSONObject(position);
    }

}

@Override
public long getItemId(int position) {
    JSONObject object = getItem(position);
    return object.optLong("id");
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null){
        convertView = infoAct.getLayoutInflater().inflate(R.layout.row,null);

    }

    TextView name = (TextView)convertView.findViewById(R.id.infoName);
    TextView hobby = (TextView)convertView.findViewById(R.id.infoHobby);
    TextView address = (TextView)convertView.findViewById(R.id.infoAddress);
    TextView phone = (TextView)convertView.findViewById(R.id.infoPhone);

    JSONObject json_data = getItem(position);
    if(json_data != null){
        try {
            String nombre = json_data.getString("name");
            String pasatiempo = json_data.getString("hobby");
            String direccion = json_data.getString("address");
            String telefono = json_data.getString("phone");

            name.setText(nombre);
            hobby.setText(pasatiempo);
            address.setText(direccion);
            phone.setText(telefono);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    return convertView;
}}

/**JSONRequest*/

public class JSONRequest extends AsyncTask<String, Void, JSONArray> {

private JSONCallback callback;

public JSONRequest(JSONCallback callback){
    this.callback = callback;
}


@Override
protected JSONArray doInBackground(String... params) {

    URLConnection connection = null;
    BufferedReader br = null;
    JSONArray result = null;

    try{
        URL url = new URL(params[0]);
        connection = (URLConnection) url.openConnection();


        InputStream is = connection.getInputStream();
        br = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String line = "";

        while((line = br.readLine()) != null){

            builder.append(line);
        }

        result = new JSONArray(builder.toString());

    }catch (Exception e) {

        e.printStackTrace();
    } finally {


        try{


            if(br != null) br.close();

        }catch(Exception e) {

            e.printStackTrace();
        }
    }

    return result;
}

@Override
protected void onPostExecute(JSONArray jsonArray) {
    super.onPostExecute(jsonArray);
    callback.requestComplete(jsonArray);
}


public interface JSONCallback{

    void requestComplete(JSONArray array);
}}
1
  • The error has nothing to do with the JSONObject. It has to do with the listInfo object being null and you are trying to call a method on it. Also, avoid System.out calls on Android; use Log instead. Commented Mar 24, 2016 at 0:28

1 Answer 1

1

Your code:

requestComplete(arrayMain);

this.listInfo = (ListView) findViewById(R.id.listView2);

requestComplete() uses this.listInfo instance but this.listInfo is null because it is set after requestComplete(). So you need to switch their order.

this.listInfo = (ListView) findViewById(R.id.listView2);
requestComplete(arrayMain);

It is better if you just put it right after call to setContentView() just to make sure this.listInfo holds valid ListView instance.

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.