Question

So I am trying to rebuilt my ListView using a custom adapter so I can make it look a bit better and add some more features to it. Anyways, here is my adapter

HistoryAdapter.java

package com.this.app;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class HistoryAdapter extends BaseAdapter {

    private Context mContext;
    Cursor cursor;

    public HistoryAdapter(Context context, Cursor cur){
        super();
        mContext = context;
        cursor = cur;
    }

    public int getCount(){
        // return the number of records in cursor
        return cursor.getCount();
    }

    // getView method is called for each item of ListView
    public View getView(int position, View view, ViewGroup parent){

        // inflate the layout for each item of listView
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.history_list_item, null);

        // move the cursor to required position
        cursor.moveToPosition(position);

        // fetch the information for each card
        String pricePerGallon = cursor.getString(cursor.getColumnIndex("pricePerGallon"));
        String gallons = cursor.getString(cursor.getColumnIndex("gallons"));
        String odometer = cursor.getString(cursor.getColumnIndex("odometer"));
        String date = cursor.getString(cursor.getColumnIndex("date"));
        String filledOrNot = cursor.getString(cursor.getColumnIndex("filledOrNot"));
        String comments = cursor.getString(cursor.getColumnIndex("comments"));
        //String milesPerGallon = cursor.getString(cursor.getColumnIndex("miledPerGallon"));
        String totalSpent = cursor.getString(cursor.getColumnIndex("totalSpent"));

        // get the reference of TextViews
        TextView textViewPricePerGallon = (TextView) view.findViewById(R.id.pricePerGallon);
        TextView textViewGallons = (TextView) view.findViewById(R.id.gallons);
        TextView textViewOdometer = (TextView) view.findViewById(R.id.odometer);
        TextView textViewDate = (TextView) view.findViewById(R.id.date);
        TextView textViewFilledOrNot = (TextView) view.findViewById(R.id.cardFilledOrNot);
        TextView textViewComments = (TextView) view.findViewById(R.id.comments);
        //TextView textViewMilesPerGallon = (TextView) view.findViewById(R.id.mpg);
        TextView textViewTotalSpent = (TextView) view.findViewById(R.id.totalSpentOnFillups);

        // Set the data to each TextView
        Log.d("HistoryAdapter", pricePerGallon);
        textViewPricePerGallon.setText(pricePerGallon);
        textViewGallons.setText(gallons);
        textViewOdometer.setText(odometer);
        textViewDate.setText(date);
        textViewFilledOrNot.setText(filledOrNot);
        textViewComments.setText(comments);
        //textViewMilesPerGallon.setText(milesPerGallon);
        textViewTotalSpent.setText(totalSpent);

        return view;
    }

    public Object getItem(int position){
        return position;
    }

    public long getItemId(int position){
        return position;
    }

}

I get a NullPointerException at the very first setText

textViewPricePerGallon.setText(pricePerGallon);

Now if I comment out all of the setText, it loads the list just fine (it just doesnt attach all the data.. instead it shows all the default values set in the XML)

Here is a copy of the debug

02-21 11:21:06.156: E/AndroidRuntime(30769): FATAL EXCEPTION: main
02-21 11:21:06.156: E/AndroidRuntime(30769): Process: com.this.app, PID: 30769
02-21 11:21:06.156: E/AndroidRuntime(30769): java.lang.NullPointerException
02-21 11:21:06.156: E/AndroidRuntime(30769):    at com.this.app.HistoryAdapter.getView(HistoryAdapter.java:60)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.ListView.makeAndAddView(ListView.java:1790)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.ListView.fillDown(ListView.java:691)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.ListView.fillFromTop(ListView.java:752)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.ListView.layoutChildren(ListView.java:1630)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.AbsListView.onLayout(AbsListView.java:2091)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2016)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1773)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1022)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5708)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.Choreographer.doFrame(Choreographer.java:532)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.os.Handler.handleCallback(Handler.java:733)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.os.Looper.loop(Looper.java:137)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at android.app.ActivityThread.main(ActivityThread.java:5083)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at java.lang.reflect.Method.invokeNative(Native Method)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at java.lang.reflect.Method.invoke(Method.java:515)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-21 11:21:06.156: E/AndroidRuntime(30769):    at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

It looks like the call: view.findViewById(R.id.pricePerGallon) is returning a null. Are you sure pricePerGallon exists as it should in the project?

OTHER TIPS

Make sure you have the id

R.id.pricePerGallon

in your XML

layout.history_list_item

If you say that the string is not null, it leaves the TextView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top