Sharang Gupta Sharang Gupta - 29 days ago 7
Android Question

I am getting a null pointer exception in my custom adapter getView() method

java.lang.NullPointerException: name is null

@Override
public View getView(int position, View convertView, ViewGroup parent) {

Holder holder;
if (convertView == null) {
holder = new Holder();
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.history_item, parent, false);

holder.tvCat = (TextView)convertView.findViewById(R.id.tv_category);
holder.tvAmt = (TextView)convertView.findViewById(R.id.tv_amount);
holder.tvDate = (TextView)convertView.findViewById(R.id.tv_date);
holder.img = (ImageView)convertView.findViewById(R.id.iv);
convertView.setTag(holder);

}

holder=(Holder)convertView.getTag();
//Holder holder = new Holder();
//View rowView;
HistoryItem historyItem;
//rowView=inflater.inflate(R.layout.history_item,null);

historyItem = historyItems.get(position);

holder.tvCat.setText(historyItem.getCategory());
holder.tvAmt.setText(historyItem.getAmount()+"");

DateFormat df = new SimpleDateFormat("yyyy/MM/dd kk:mm:ss");
Calendar c = Calendar.getInstance();
c.setTimeInMillis(historyItem.getTimestamp());
Date day = c.getTime();

holder.tvDate.setText(df.format(day));

Resources res = context.getResources();
String mDrawableName = iconMapper.get(historyItem.getCategory());

//This line is giving the null pointer exception...
int resID = res.getIdentifier(mDrawableName , "drawable", context.getPackageName());

holder.img.setImageResource(resID);

return convertView;
}


I checked mDrawableName and it holds the correct value.

Also, the context is coming from my ListView class.

Any help would be appreciated.

Logcat :

java.lang.NullPointerException: name is null
at android.content.res.ResourcesImpl.getIdentifier(ResourcesImpl.java:214)
at android.content.res.Resources.getIdentifier(Resources.java:1866)
at com.example.sharang.wheresmymoney.HistoryCustomAdapter.getView(HistoryCustomAdapter.java:92)
at com.baoyz.swipemenulistview.SwipeMenuAdapter.getView(SwipeMenuAdapter.java:49)
at android.widget.AbsListView.obtainView(AbsListView.java:2369)
at android.widget.ListView.makeAndAddView(ListView.java:1970)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillFromTop(ListView.java:765)
at android.widget.ListView.layoutChildren(ListView.java:1744)
at android.widget.AbsListView.onLayout(AbsListView.java:2161)
at android.view.View.layout(View.java:17526)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17526)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17526)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
at android.view.View.layout(View.java:17526)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17526)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17526)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
at android.view.View.layout(View.java:17526)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6307)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Answer

You can replace context with getContext()

And make sure not use getApplicationContext when you create the adapter

You'll have to figure out why iconMapper doesn't contain the historyItem category value

If all that fails, then add a null check around setting the image resource