Question

I am building an app using listview and lazy adapter based on @fedor post on this link

here's my first activity :

public class MenuViewAll extends ListActivity {

// url to make request
private static String url = "http://10.0.2.2/culigui/getdataresto.php";
//public static String TAG_NAME,TAG_ADDRESS;
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_ADDRESS = "address";
static final String KEY_THUMB = "linkthumb";

ListView list;
LazyAdapter adapter;

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

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> userList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting JSONArray of listresto
        JSONArray  listresto = json.getJSONArray("listresto");

        // looping through All listresto
        for(int i = 0; i < listresto.length(); i++){
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject list = listresto.getJSONObject(i);


            // insert String to Local Variable
            map.put(KEY_ID, list.getString("id_resto"));
            map.put(KEY_NAME, list.getString("nama_resto"));
            map.put(KEY_ADDRESS, list.getString("alamat_resto"));
            map.put(KEY_THUMB, list.getString("thumb_img"));
            userList.add(map);


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


    /**
     * Updating parsed JSON data into ListView
     * */



    adapter = new LazyAdapter(this, userList);
    list.setAdapter(adapter);

   // ListView yourListView = getListView() 

}

and here's my lazy adapter class :

public class LazyAdapter extends BaseAdapter  {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.listitemviewall, null);

    TextView namaresto = (TextView)vi.findViewById(R.id.name); // resto name
    TextView alamatresto = (TextView)vi.findViewById(R.id.address); // resto address
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.defaultthumb); // thumb image

    HashMap<String, String> resto = new HashMap<String, String>();
    resto = data.get(position);

    // Setting all values in listview
    namaresto.setText(resto.get(MenuViewAll.KEY_NAME));
    alamatresto.setText(resto.get(MenuViewAll.KEY_ADDRESS));
    imageLoader.DisplayImage(resto.get(MenuViewAll.KEY_THUMB), thumb_image);
    return vi;
}}

and after i run my code, here's the logcat :

04-05 14:51:32.714: D/dalvikvm(311): GC_EXTERNAL_ALLOC freed 869 objects / 60304 bytes in 76ms
04-05 14:51:46.235: D/dalvikvm(311): GC_EXTERNAL_ALLOC freed 757 objects / 43592 bytes in 362ms
04-05 14:51:46.945: D/AndroidRuntime(311): Shutting down VM
04-05 14:51:46.945: W/dalvikvm(311): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-05 14:51:46.975: E/AndroidRuntime(311): FATAL EXCEPTION: main
04-05 14:51:46.975: E/AndroidRuntime(311): java.lang.RuntimeException: Unable to start activity ComponentInfo{last.project.CuliGUI/last.project.CuliGUI.MenuViewAll}: java.lang.NullPointerException
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.os.Looper.loop(Looper.java:123)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-05 14:51:46.975: E/AndroidRuntime(311):  at java.lang.reflect.Method.invokeNative(Native Method)
04-05 14:51:46.975: E/AndroidRuntime(311):  at java.lang.reflect.Method.invoke(Method.java:521)
04-05 14:51:46.975: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-05 14:51:46.975: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-05 14:51:46.975: E/AndroidRuntime(311):  at dalvik.system.NativeStart.main(Native Method)
04-05 14:51:46.975: E/AndroidRuntime(311): Caused by: java.lang.NullPointerException
04-05 14:51:46.975: E/AndroidRuntime(311):  at     last.project.CuliGUI.MenuViewAll.onCreate(MenuViewAll.java:102)

my program always force close... maybe someone can help me...

Was it helpful?

Solution

You should pay attention to this part of the error:

 04-05 14:51:46.975: E/AndroidRuntime(311): Caused by: java.lang.NullPointerException
04-05 14:51:46.975: E/AndroidRuntime(311):  at     last.project.CuliGUI.MenuViewAll.onCreate(MenuViewAll.java:102)

And especially:

MenuViewAll.java:102

That means that something in that line (i'm not going to count out the lines :) ) is NULL, but you try to use it. Look for the using of an object that might be NULL

Example (JUST AN EXAMPLE, becasue i don't know what line it is). Lets say this line:

JSONObject list = listresto.getJSONObject(i);

goes wrong for some reason. (there is no JSONobject i, listresto is false, I dunno). Then later on this:

        map.put(KEY_ID, list.getString("id_resto"));

will give a nullpointer, as you can't do getString on a NULL object.

so:

  • find the line
  • find the object
  • fix the reason why it is NULL, or make sure you handle it beforehand
  • ...
  • profit
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top