0

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...

Ivan Ahmed
  • 39
  • 7
  • 3
    post Logcat and mark the line where you get the NULLPointer, please – Bigflow Apr 16 '13 at 14:06
  • 1
    stacktrace from logcat please? – Sankar V Apr 16 '13 at 14:06
  • 6
    A quick mention, using runOnUiThread() in onPostExecute() doesn't make any sense, because onPostExecute() already runs on the UI thread. – Egor Apr 16 '13 at 14:10
  • You _cannot_ display Toast messages in `doInBackground()` because it runs on a different Thread and you can only alter the UI from main Thread. (The irony is that your error message will throw an exception.) – Sam Apr 16 '13 at 14:17
  • 1
    which is the line on viewPOI.java:61 – Sankar V Apr 16 '13 at 14:35
  • Probable cause: your json object is null because your jParser.getJSONFromURL method returns null. Post that code as well... – DigCamara Apr 16 '13 at 14:54

2 Answers2

0

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.

anzaidemirzoi
  • 386
  • 4
  • 13
  • 1
    The author doesn't read any of these parameter, so that fact that they are null won't break anything (they could be changed ``). The ellipse in `doInBackground(String... args)` means "zero or more", `new LoadAllPOI().execute();` send zero parameters, so `args` is simply an empty String Array. – Sam Apr 16 '13 at 14:29
  • Can you explain more with your words " 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." I'm sorry – Ivan Ahmed Apr 16 '13 at 14:37
0

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.

DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • Thanks.. How about the execute function?? Is that need parsing String parameter? And postExecute()?? – Ivan Ahmed Apr 16 '13 at 15:07
  • It's like @Sam said: you can change to (for instance) and not have any trouble. – DigCamara Apr 16 '13 at 15:16
  • After i place comment tag in my code, i have error "org.json.jsonexception end of input at character 0 of" what is that mean? – Ivan Ahmed Apr 18 '13 at 04:24
  • Well, first things first. This site needs active user participation, which means you have to mark the answers you've gotten as correct. For instance: this question, even though it seems to have been answered correctly, is still unmarked. Come back as soon as you've done that and I'll continue helping you. http://stackoverflow.com/questions/15898391/query-select-where-return-no-matched-data-in-sqlite-android – DigCamara Apr 18 '13 at 14:30
  • ... there are other questions that people have answered and that you haven't marked as finished as well... for example: http://stackoverflow.com/questions/15876646/error-android-your-content-must-have-a-listview-whose-id-attribute-is-android-r – DigCamara Apr 22 '13 at 14:53
  • (While you're at it: mark this answer as resolved as well). And by the way: your error means that the information you got from your URL is empty. You need to check if your query actually returns information. Now: if you're testing on your own computer it actually might work, but as far as I can tell, the address "http://127.0.0.1/locaRemService/db_function.php" won't actually work otherwise, since it's only a redirection to your own computer. – DigCamara Apr 22 '13 at 15:13
  • I've done it... Sorry i forgot the rules. Your solution worked. Thanks.. I put the IP to 10.0.2.2/locRemService/db_function.php – Ivan Ahmed Apr 22 '13 at 16:27
  • No problem. Good to see it helped! – DigCamara Apr 22 '13 at 16:29