NullPointerException when using CustomListAdapter
Asked Answered
R

3

3

I create CustomListAdapter in Android Appiclation. But i get this error NullPointerException when implement it.

Here is my CustomListAdapter.java code :

/**
 * 
 */
package com.fanjavaid.searchhttprequest.adapter;

import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanjavaid.searchhttprequest.R;
import com.fanjavaid.searchhttprequest.app.AppController;
import com.fanjavaid.searchhttprequest.model.Menu;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author fanjavaid
 *
 */
public class CustomListAdapter extends BaseAdapter {

    private Activity activity;

    private LayoutInflater inflater;

    private List<Menu> menuItems;

    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListAdapter(Activity activity, List<Menu> menuItems) {
        this.activity = activity;
        this.menuItems = menuItems;
    }

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

    @Override
    public Object getItem(int location) {
        // TODO Auto-generated method stub
        return menuItems.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) 
            convertView = inflater.inflate(R.layout.list_row, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
            NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
            TextView menuTitle = (TextView) convertView.findViewById(R.id.menuTitle);
            TextView inputDate = (TextView) convertView.findViewById(R.id.postDate);

        // Getting movie data for the row
        Menu m = menuItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

        // title
        menuTitle.setText(m.getMenuTitle());

        // input date
        inputDate.setText("Post Date : " + String.valueOf(m.getInputDate().toString()));

        return convertView;
    }

}

And here is my Activity code :

package com.fanjavaid.searchhttprequest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fanjavaid.searchhttprequest.adapter.CustomListAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class ListMenuActivity extends Activity {

    // Log tag
    private static final String TAG = ListMenuActivity.class.getSimpleName();

    // Movie JSON Url
    private List<com.fanjavaid.searchhttprequest.model.Menu> menuList = new ArrayList<com.fanjavaid.searchhttprequest.model.Menu>();
    private ListView listView;
    private CustomListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_menu);

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, menuList);
        listView.setAdapter(adapter);

        Intent intent = getIntent();

        //String jsonData = intent.getStringExtra("json_menu");

        Bundle bundle = intent.getExtras();
        String jsonString = bundle.getString("json_menu");

        try {
            JSONObject objectJson = new JSONObject(jsonString);
            JSONArray arrayJson = objectJson.getJSONArray("results");

            for (int i = 0; i < arrayJson.length(); i++) {

                JSONObject obj = arrayJson.getJSONObject(i);

                com.fanjavaid.searchhttprequest.model.Menu menu = new com.fanjavaid.searchhttprequest.model.Menu();
                menu.setMenuTitle(obj.getString("nama_menu"));
                menu.setThumbnailUrl("http://tempercube/receip_finder/" + obj.getString("gambar"));

                String dateInString = obj.getString("tgl_input");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date parsedDate = formatter.parse(dateInString);
                menu.setInputDate(parsedDate);

                // add to list
                menuList.add(menu);
            }

            // notifying list adapter about data changed
            // so that it renders the list view with data changed
            adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

CustomListAdapter instantiation already added before setAdapter() method in my activity. But i still get that NullPointerException Error.

This is my error logcat

09-18 09:11:34.661: E/AndroidRuntime(24097): FATAL EXCEPTION: main
09-18 09:11:34.661: E/AndroidRuntime(24097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fanjavaid.searchhttprequest/com.fanjavaid.searchhttprequest.ListMenuActivity}: java.lang.NullPointerException
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2311)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.os.Looper.loop(Looper.java:137)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.main(ActivityThread.java:5214)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at java.lang.reflect.Method.invoke(Method.java:525)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at dalvik.system.NativeStart.main(Native Method)
09-18 09:11:34.661: E/AndroidRuntime(24097): Caused by: java.lang.NullPointerException
09-18 09:11:34.661: E/AndroidRuntime(24097):    at com.fanjavaid.searchhttprequest.adapter.CustomListAdapter.<init>(CustomListAdapter.java:34)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at com.fanjavaid.searchhttprequest.ListMenuActivity.onCreate(ListMenuActivity.java:38)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.Activity.performCreate(Activity.java:5133)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
09-18 09:11:34.661: E/AndroidRuntime(24097):    ... 11 more

And this my AppController.java

/**
 * 
 */
package com.fanjavaid.searchhttprequest.app;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.fanjavaid.searchhttprequest.util.LruBitmapCache;

import android.app.Application;
import android.text.TextUtils;

/**
 * @author fanjavaid
 *
 */
public class AppController extends Application {
    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new LruBitmapCache());
        }
        return this.mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

And this one, my MainActivity.java

package com.fanjavaid.searchhttprequest;

import java.util.LinkedList;
import java.util.List;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

    private TextView responseText;
    private EditText searchText;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        responseText = (TextView) findViewById(R.id.textView1);
        searchText = (EditText) findViewById(R.id.searchBox);

        url = "http://tempercube.com/receip_finder/process_cari.php";
    }

    private String addLocationToUrl(String url, String parameter) {
        if (!url.endsWith("?"))
            url += "?";

        List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("bahan", parameter));

        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += paramString;

        return url;
    }

    public void doRequest(String passUrl) {
        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();

        JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Method.GET, passUrl, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        // TODO Auto-generated method stub
                        Log.d("TAG", response.toString());
                        responseText.setText(response.toString());

                        Intent intent = new Intent("com.fanjavaid.searchhttprequest.ListMenuActivity");

                        Bundle extras = new Bundle();
                        extras.putString("json_menu", response.toString());

                        intent.putExtras(extras);

                        startActivity(intent);

                        pDialog.hide();
                    }

                },

                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Log.d("ERROR", "Error : " + error.getMessage());
                        pDialog.hide();
                    }

                });

        // Make request queue
        VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjRequest);

    }

    public void searchMenu(View view) {
        if(view.getId() == R.id.button1){
            String paramSearch = searchText.getText().toString();
            url = addLocationToUrl(url, paramSearch);

            Log.d("URL_INFO", url);

            doRequest(url);

            // reset URL
            url = "http://tempercube.com/receip_finder/process_cari.php";
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

And the last my list_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView 
        android:id="@+id/list" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector">
    </ListView>

</RelativeLayout>

Please help. Thank you very much.

Roofing answered 17/9, 2014 at 18:9 Comment(9)
Error is in which line?Bind
can you post stacktrace from your logcat?Eleonoreeleoptene
What line does it crash at? Show the log please?Arcuate
listView is likely null.Fuss
I bet it has something to do with those not well indented 3 lines after the 'if (imageLoader == null)' statementForzando
I already updated my answer with log information. I think listView is null. But i already instantiate it. Thanks you.Roofing
It broke at line 34 of CustomListAdapter. ImageLoader imageLoader = AppController.getInstance().getImageLoader(); Let us see what happened at AppController.Halvah
Please, see my answer. I already updated. Thank you.Roofing
<application android:name=".AppController" in your manifestRosanarosane
P
21

I think you didn't add AppController to your manifest:

 <application
        android:name=".AppController" //this one
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

That's why onCreate never called and mInstance is null.

Pantomime answered 18/9, 2014 at 3:31 Comment(3)
@delive Use singletons and init them in your main controller, for example.Pantomime
If you can't because have multidex ?Jochbed
Then you have another options to enable multidex. You can find them here developer.android.com/intl/ru/reference/android/support/… . For example, your application class can extends MultiDexApplication or you can override attachBaseContext method and add MultiDex.install(this); .Pantomime
A
0

I noticed that you use private List<com.fanjavaid.searchhttprequest.model.Menu> menuList = new ArrayList<com.fanjavaid.searchhttprequest.model.Menu>();, but you never actually add anything to that list before you call:

listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, menuList);
listView.setAdapter(adapter);

You probably want to put something in that menuList

Arcuate answered 17/9, 2014 at 18:20 Comment(0)
B
0

add AppController(Activity request) to your manifest:

 <application
    android:name=".AppController" //this one
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
Blackguard answered 7/5, 2018 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.