0

i m trying to call web services method from android app but i get in error java.lang.RuntimeException error occured while executing doInBackground() and the app stopped i have saw alot of question about this but they solved by adding internet permission to the manifest file but its Didn't work to

here is my code

package com.yeftaandrea.plesirsolo;

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

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.yeftaandrea.plesirsolo.adapter.JsonAttractionAdapter;

public class KaranganyarAttractionFragment extends Fragment {

    // URL to get contacts JSON
    // JSON Node names
    public static final String TAG_ID = "ID";
    public static final String TAG_NAME = "Name";
    public static final String TAG_ADDRESS = "Address";
    public static final String TAG_PHONE = "Phone";
    public static final String TAG_PICPATH = "PicPath";

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> attractionList;
    ListView list;

    JsonAttractionAdapter adapter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_attraction_karanganyar, container, false);

        list = (ListView) rootView.findViewById(R.id.list);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // TODO Auto-generated method stub
            }
            });

        // Calling async task to get json
        new LoadTask().execute();

        return rootView;
    }

    private void loadAttraction(){
        attractionList = new 
                ArrayList<HashMap<String, String>>();
        JSONParser parser = new JSONParser();

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        try {
            JSONArray attraction = parser.makeHttpRequestArray("http://plesirsolo.meximas.com/plesir/attraction/wisata/getWisata/id/1", "GET", params);
            // JSONArray attraction = parser.makeHttpRequestArray("http://192.168.56.1:8080/examples/json.html", "GET", params);
            // looping through All messages
            for (int i = 0; i < attraction.length(); i++) {
                JSONObject c = attraction.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String nama = c.getString(TAG_NAME);
                String address = c.getString(TAG_ADDRESS);
                String phone = c.getString(TAG_PHONE);
                String picPath = c.getString(TAG_PICPATH);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, nama);
                map.put(TAG_ADDRESS, address);
                map.put(TAG_PHONE, phone);
                map.put(TAG_PICPATH, picPath);

                // adding HashList to ArrayList
                attractionList.add(map);
            }

            adapter = new JsonAttractionAdapter(
                    getActivity(), attractionList);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

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

        @Override
        protected Void doInBackground(Void... params) {
            loadAttraction();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            list.setAdapter(adapter);
            Log.i("JSON", "TEST");
            Log.i("JSON", attractionList.toString());
        }       

    }

}

and the logcat

06-02 09:35:33.245: E/AndroidRuntime(1322): FATAL EXCEPTION: AsyncTask #2
06-02 09:35:33.245: E/AndroidRuntime(1322): java.lang.RuntimeException: An error occured while executing doInBackground()
06-02 09:35:33.245: E/AndroidRuntime(1322):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at java.lang.Thread.run(Thread.java:841)
06-02 09:35:33.245: E/AndroidRuntime(1322): Caused by: java.lang.NullPointerException
06-02 09:35:33.245: E/AndroidRuntime(1322):     at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment.loadAttraction(KaranganyarAttractionFragment.java:74)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment.access$0(KaranganyarAttractionFragment.java:63)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment$LoadTask.doInBackground(KaranganyarAttractionFragment.java:110)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment$LoadTask.doInBackground(KaranganyarAttractionFragment.java:1)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-02 09:35:33.245: E/AndroidRuntime(1322):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-02 09:35:33.245: E/AndroidRuntime(1322):     ... 4 more

I need your help, thank you

2
  • figure out whats null here KaranganyarAttractionFragment.java:74 Commented Jun 2, 2014 at 13:54
  • line : 74 is " creating new HashMap " @tyczj Commented Jun 2, 2014 at 13:59

2 Answers 2

2

check attraction!=null before doing the for loop

JSONArray attraction = parser.makeHttpRequestArray("http://plesirsolo.meximas.com/plesir/attraction/wisata/getWisata/id/1", "GET", params);

if(attraction!=null){
  // do something
}
Sign up to request clarification or add additional context in comments.

4 Comments

so I moved the code after JSONArray attraction = parser.makeHttpRequestArray("plesirsolo.meximas.com/plesir/attraction/wisata/getWisata/id/1", "GET", params); inside if ??
Not inside the if, you need to surround the loop with the if, and execute the loop if and only if attraction!=null
its error was gone, but the data does not appear , the logcat : Error parsing data org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONArray
That URL is not returning JSON, is returning HTML
1

It seems like your variable atraction is null because this call

JSONArray attraction = parser.makeHttpRequestArray("http://plesirsolo.meximas.com/plesir/attraction/wisata/getWisata/id/1", "GET", params);

is returning null, and later you are trying to access that variable in

for (int i = 0; i < attraction.length(); i++) {
    JSONObject c = attraction.getJSONObject(i);

Check that before accesing the object attraction it is initialized or if it is null do not enter the loop. You should also read: What is a NullPointerException, and how do I fix it?

Comments

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.