-1

I am still new in android development, now i am trying to develop a simple apps to study more android development. I am also try to using JSON Parser, but i have a bit problem when display a data.

I got an error from my debugger :

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.banjir.info.infobanjir, PID: 1847
              java.lang.NullPointerException
                  at com.banjir.info.infobanjir.perak_activity$GetData.onPostExecute(perak_activity.java:146)
                  at com.banjir.info.infobanjir.perak_activity$GetData.onPostExecute(perak_activity.java:40)
                  at android.os.AsyncTask.finish(AsyncTask.java:632)
                  at android.os.AsyncTask.access$600(AsyncTask.java:177)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:149)
                  at android.app.ActivityThread.main(ActivityThread.java:5257)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                  at dalvik.system.NativeStart.main(Native Method)

I already look at line 146 and 40, but i could not find the problem. I already make some research in google before asking here, because i did not understand the concept, i could not fix this problem.

Here is my activity code :

package com.banjir.info.infobanjir;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by USER on 15/2/2017.
 */
public class perak_activity extends AppCompatActivity {

    private String TAG = perak_activity.class.getSimpleName();
    private ListView lv;

    ArrayList<HashMap<String, String>> banjirList;

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

        banjirList = new ArrayList<>();
        lv = (ListView) findViewById(R.id.list);

        new GetData().execute();
    }

    private class GetData extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(perak_activity.this,"Json Data is downloading",Toast.LENGTH_LONG).show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();
            // Making a request to url and getting response
            String url = "https://banjir-api.herokuapp.com/api/v1/reports.json";
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    JSONArray data = jsonObj.getJSONArray("data");


                    // looping through All data
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject c = data.getJSONObject(i);
                        String id = c.getString("id");
                        String paras_air = c.getString("paras_air");
                        String jenis_jalan = c.getString("jenis_jalan");
                        String section = c.getString("section");
                        String no_laluan = c.getString("no_laluan");
                        String nama_laluan = c.getString("nama_laluan");
                        String nama_jalan = c.getString("nama_jalan");
                        String daerah = c.getString("daerah");
                        String negeri = c.getString("negeri");
                        String dikemaskini = c.getString("dikemaskini");
                        String status = c.getString("status");
                        String latitude = c.getString("latitude");
                        String longitude = c.getString("longitude");
                        String google_maps_url = c.getString("google_maps_url");

                        // tmp hash map for single contact
                        HashMap<String, String> data1 = new HashMap<>();

                        // adding each child node to HashMap key => value
                        data1.put("id", id);
                        data1.put("paras_air", paras_air);
                        data1.put("jenis_jalan", jenis_jalan);
                        data1.put("section", section);
                        data1.put("no_laluan", no_laluan);
                        data1.put("nama_laluan", nama_laluan);
                        data1.put("nama_jalan", nama_jalan);
                        data1.put("daerah", daerah);
                        data1.put("negeri", negeri);
                        data1.put("dikemaskini", dikemaskini);
                        data1.put("status", status);
                        data1.put("latitude", latitude);
                        data1.put("longitude", longitude);
                        data1.put("google_maps_url", google_maps_url);

                        // adding contact to contact list
                        banjirList.add(data1);
                    }

                    // Getting JSON Object About
                    JSONObject about  = jsonObj.getJSONObject("about");
                    String github = about.getString("github");
                    String source = about.getString("source");
                    String created_by = about.getString("created_by");


                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                }

            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG).show();
                    }
                });
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            ListAdapter adapter = new SimpleAdapter(perak_activity.this, banjirList,
                    R.layout.list_item, new String[]{ "paras_air","nama_laluan", "daerah", "status"},
                    new int[]{R.id.paras_air, R.id.nama_laluan, R.id.daerah, R.id.status});
            lv.setAdapter(adapter);
        }
    }
}

I followed tutorial JSON Parse from TutorialPoint and i tried to change the api url to explore more. But i got an error like that, the original coding from tutorial working.

Kero Kero
  • 9
  • 6

1 Answers1

0

Make sure that your R.layout.perak_layout has a ListView with id R.id.list. If it doesn't have a view with id R.id.list then your ListView lv will not be initialized and it be null and hence the NullPointerException on lv.setAdapter(adapter);

Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44
  • Sorry my mistake, I just change it and forget to add the ListView in the layout. I could not understand the problem, that why i could not realise the simple mistake I made. I hope this could be my last mistake. – Kero Kero Feb 17 '17 at 17:41