문제

Hi everybody i need some help with my code. I want to get JSON data from web server but when i try to call the doInBackground function nullException error appear.

This is my code:

public class viewPOI extends ListActivity {
//private ProgressDialog progress;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> POIList;
private static String url_webservice = 
    "http://127.0.0.1/locaRemService/db_function.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_POIN = "point";
private static final String TAG_ALAMAT = "alamat_lokasi";
private static final String TAG_KONTEKS = "konteks_lokasi";
JSONArray POI = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_poi);

    POIList = new ArrayList<HashMap<String, String>>();
    new LoadAllPOI().execute();
    ListView Lpoi = getListView();
}

class LoadAllPOI extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.getJSONFromURL(url_webservice, "GET", params);
        Log.d("All Products: ", json.toString());

        try {
            int sukses = json.getInt(TAG_SUCCESS);
            if(sukses == 1) {
                POI = json.getJSONArray(TAG_POIN);
                for(int i = 0; i < POI.length(); i++) {
                    JSONObject c = POI.getJSONObject(i);
                    String alamat = c.getString(TAG_ALAMAT);
                    String konteks = c.getString(TAG_KONTEKS);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_ALAMAT, alamat);
                    map.put(TAG_KONTEKS, konteks);
                    POIList.add(map);
                }
            } else {
                Toast.makeText(getApplicationContext(), "Tidak ada lokasi", Toast.LENGTH_SHORT).show();
            }
        } catch(JSONException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String file_url) {
        //progress.dismiss();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                ListAdapter adapter = new SimpleAdapter(viewPOI.this, POIList, 
                        R.layout.list_item, new String[]{TAG_ALAMAT, TAG_KONTEKS}, 
                        new int[]{R.id.alamatPOI, R.id.konteksPOI});
                setListAdapter(adapter);
            }

        });
    }
}

}

Log Cat

04-16 20:06:59.389: ERROR/AndroidRuntime(503): FATAL EXCEPTION: AsyncTask #1
04-16 20:06:59.389: ERROR/AndroidRuntime(503): java.lang.RuntimeException: An error occured while executing doInBackground()
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.lang.Thread.run(Thread.java:1019)
04-16 20:06:59.389: ERROR/AndroidRuntime(503): Caused by: java.lang.NullPointerException
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at com.locationReminder.viewPOI$LoadAllPOI.doInBackground(viewPOI.java:61)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at com.locationReminder.viewPOI$LoadAllPOI.doInBackground(viewPOI.java:1)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     ... 4 more

Thanks for any help...

[SOLVED] Thanks for everybody i have done with this problem. I change the url to 10.0.2.2 and the problem is over. Thanks for all...

도움이 되었습니까?

해결책

If you're using this code, you're probably getting a null value for your object because of one of many reasons (no net connection, wrong site, malformed JSON Object).

Instead of

Log.d("All Products: ", json.toString());

try

if (json!=null)
    Log.d("All Products: ", json.toString());

On further review: your

try{...}  catch(JSONException ex) {...}

will throw an exception as well, because you're not testing json for null values in there. So

 if (json!=null){
    Log.d("All Products: ", json.toString());
     try{...}  catch(JSONException ex) {...}
 }

would be your actual solution.

다른 팁

First of all you use String, String, String generic parameteres for async task so you need to pass a String parameter on execute(String param goes here). Also you need to return a String param for onPostExecute(String file_url) at the end of doInBackground(String... args). in your code you only return null.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top