1

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

05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: Process: com.example.ckw.mapku, PID: 17982 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground() 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:300) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:242) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: Caused by: java.lang.NullPointerException 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at com.example.ckw.mapku.ServiceHandler.makeHttpRequest(ServiceHandler.java:71) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at com.example.ckw.mapku.ServiceHandler.makeHttpRequest(ServiceHandler.java:33) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at com.example.ckw.mapku.Hasil_pencarian$GetMedik.doInBackground(Hasil_pencarian.java:77) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at com.example.ckw.mapku.Hasil_pencarian$GetMedik.doInBackground(Hasil_pencarian.java:60) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:288) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)

and this is my code

    package com.example.ckw.mapku;

import android.app.ProgressDialog;
import android.app.Service;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class Hasil_pencarian extends AppCompatActivity {

    String myJSON;

    private static final String TAG_RESULT = "result";
    private static final String TAG_ID = "id_lay";
    private static final String TAG_NAME = "Nama";
    private static final String TAG_LONG = "Longitude";
    private static final String TAG_LAT = "Latitude";

    JSONArray layanan = null;

    ArrayList<HashMap<String, String>> layananList;

    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hasil_pencarian);
        list = (ListView) findViewById(R.id.listView);
        layananList = new ArrayList<HashMap<String, String>>();
        new GetMedik().execute();
    }

    private  class GetMedik extends AsyncTask<String, Void, JSONObject>{
        private ProgressDialog progressDialog;

        private static final String ALAMAT = "http://rugimuleh.com/medikmap/medik.php";

        @Override
        protected void onPreExecute(){
            progressDialog = new ProgressDialog(Hasil_pencarian.this);
            progressDialog.setMessage("Tunggu sebentar...");
            progressDialog.setCancelable(false);
            progressDialog.show();

        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            ServiceHandler sh = new ServiceHandler();
            JSONObject jsonStr = sh.makeHttpRequest(ALAMAT, "GET");
            Log.d("JSON result ", jsonStr.toString());
            myJSON = jsonStr.toString();

            if (jsonStr != null) {
                try {
                    JSONObject jsonObject = new JSONObject(myJSON);
                    layanan = jsonObject.getJSONArray(TAG_RESULT);

                    for (int i=0;i<layanan.length();i++){
                        JSONObject c = layanan.getJSONObject(i);
                        String id_lay = c.getString(TAG_ID);
                        String Nama = c.getString(TAG_NAME);
                        String Longitude = c.getString(TAG_LONG);
                        String Latitude = c.getString(TAG_LAT);

                        HashMap<String, String> medik = new HashMap<String, String>();

                        medik.put(TAG_ID, id_lay);
                        medik.put(TAG_NAME, Nama);
                        medik.put(TAG_LONG, Longitude);
                        medik.put(TAG_LAT, Latitude);

                        layananList.add(medik);
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");}
            return null;
        }

        protected void onPostExecute(JSONObject result){
            super.onPostExecute(result);

            if(progressDialog.isShowing())
                progressDialog.dismiss();

            ListAdapter adapter = new SimpleAdapter(
                    Hasil_pencarian.this, layananList, R.layout.list_item,
                    new String[]{TAG_ID, TAG_NAME, TAG_LONG, TAG_LAT},
                    new  int[]{R.id.id_lay, R.id.Nama, R.id.Longitude, R.id.Latitude}
            );
            list.setAdapter(adapter);

        }
    }

}

2
  • 1
    There's a java.lang.NullPointerException thrown from ServiceHandler.java's line 71. You should check that file. Commented May 17, 2016 at 3:17
  • ok i got it. thats very helpfull thank Commented May 17, 2016 at 7:35

1 Answer 1

1

Check for a NullPointerException here :

        JSONObject jsonStr = sh.makeHttpRequest(ALAMAT, "GET");

Caused by: java.lang.NullPointerException 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime: at com.example.ckw.mapku.ServiceHandler.makeHttpRequest(ServiceHandler.java:71)

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

3 Comments

ok i got it. the problem is in my declaration . thx. but i have another problem again. when my apps running there is no error but the data from my php cant display on my apps. how bout that
@StepHanusCkw that could go on as another question please, this shall be sufficient by itself :)
@StepHanusCkw : wc

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.