0
votes

I had error when build my code on android device. its the error report

FATAL EXCEPTION: AsyncTask #1 Process: com.example.guntu, PID: 1248 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:304) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference at com.example.guntu.List$DownloadJSON.doInBackground(List.java:74) at com.example.guntu.List$DownloadJSON.doInBackground(List.java:49) at android.os.AsyncTask$2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  at java.lang.Thread.run(Thread.java:818)  09-06 07:37:48.656 1248-1248/com.example.guntu E/WindowManager: android.view.WindowLeaked: Activity com.example.guntu.List has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{35557537 V.E..... R......D 0,0-684,322} that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:364) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at android.app.Dialog.show(Dialog.java:298) at com.example.guntu.List$DownloadJSON.onPreExecute(List.java:62) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591) at android.os.AsyncTask.execute(AsyncTask.java:539) at com.example.guntu.List.onCreate(List.java:44) at android.app.Activity.performCreate(Activity.java:6033) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5268) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

here is my code

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(List.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Android JSON Parse Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONFunction
                .getJSONfromURL("http://localhost/atm_db/json/json.php");
        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("nama_atm", jsonobject.getString("nama_atm"));
                map.put("alamat", jsonobject.getString("alamat"));
                map.put("latitude", jsonobject.getString("latitude"));
                map.put("longtitude", jsonobject.getString("longtitude"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
2
What line exactly is your error pointing to?Farbod Salamat-Zadeh

2 Answers

0
votes

It looks like jsonobject.getJSONArray(""); is throwing the error due to the fact that jsonobject is null.

You will want to double check that you are getting data back and if so the format of that JSON data.

0
votes

Replace DownloadJSON with the following code :

    public class DownloadJSON extends AsyncTask<String ,Integer , JSONObject> {

    DownloadJSON(){
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(List.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Android JSON Parse Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONFunction.getJSONfromURL("http://localhost/atm_db/json/json.php");
        return jsonobject;
    }

    @Override
    protected void onPostExecute(JSONObject s) {
        super.onPostExecute(s);
        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("PUT KEY OF JSON HERE");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("nama_atm", jsonobject.getString("nama_atm"));
                map.put("alamat", jsonobject.getString("alamat"));
                map.put("latitude", jsonobject.getString("latitude"));
                map.put("longtitude", jsonobject.getString("longtitude"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
    }


    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }
}

Note :

1 . Before returning jsonobject from doInBackground() be sure that you are receiving valid jsonobject from the URL you mentioned .

2 . While getting jsonArray from jsonobject use key of the array .