NullPointerException on SetAdapter With My FriendsArrayAdapter-Collection of common programming errors

The exception is occurring on the line “rowTxt.setText” and I can’t, for the life of me, figure out why.

Here’s my code

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

    if(rowView == null)
    {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(mResourceId, null);
    }

    Friend friend = mFriendsList.get(position);
    TextView rowTxt = (TextView) rowView.findViewById(R.id.rowtext);
    Log.i("name", friend.getmName());
    rowTxt.setText(friend.getmName());

    return rowView;
}

Logcat output:

04-24 04:07:14.093: DEBUG/AndroidRuntime(553): Shutting down VM 04-24 04:07:14.093: WARN/dalvikvm(553): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): FATAL EXCEPTION: main 04-24 04:07:14.152: ERROR/AndroidRuntime(553): java.lang.NullPointerException 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at com.friendsapp.FriendsArrayAdapter.getView(FriendsArrayAdapter.java:46) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.AbsListView.obtainView(AbsListView.java:1315) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.ListView.makeAndAddView(ListView.java:1727) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.ListView.fillDown(ListView.java:652) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.ListView.fillFromTop(ListView.java:709) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.ListView.layoutChildren(ListView.java:1580) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.AbsListView.onLayout(AbsListView.java:1147) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:99) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:123) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4627) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:521) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-24 04:07:14.152: ERROR/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method) 04-24 04:07:14.182: WARN/ActivityManager(65): Force finishing activity com.friendsapp/.FriendsActivity 04-24 04:07:14.973: WARN/ActivityManager(65): Activity pause timeout for HistoryRecord{44e9c3c8 com.friendsapp/.FriendsActivity} 04-24 04:07:17.822: DEBUG/dalvikvm(553): GC_FOR_MALLOC freed 5842 objects / 457336 bytes in 113ms 04-24 04:07:18.732: INFO/Process(553): Sending signal. PID: 553 SIG: 9 04-24 04:07:18.772: INFO/WindowManager(65): WIN DEATH: Window{4503df58 AtchDlg:com.friendsapp/com.friendsapp.FriendsActivity paused=false} 04-24 04:07:18.772: INFO/ActivityManager(65): Process com.friendsapp (pid 553) has died. 04-24 04:07:18.782: INFO/WindowManager(65): WIN DEATH: Window{44f78f88 com.friendsapp/com.friendsapp.FriendsActivity paused=false} 04-24 04:07:26.814: WARN/ActivityManager(65): Activity destroy timeout for HistoryRecord{44e9c3c8 com.friendsapp/.FriendsActivity} 04-24 04:12:07.102: DEBUG/SntpClient(65): request time failed: java.net.SocketException: Address family not supported by protocol

EDIT: I figured out the issue with the help of corlettk. In short, I was assigning my listadapter like so:

FriendsArrayAdapter fAdapter = new FriendsArrayAdapter(FriendsActivity.this, android.R.simple_list_item_1, mFriendsList);

I did this because initially I had a generic listadapter that did not accommodate my Friend object; never thought to change it.

So when setAdapter went into my FriendsArrayAdapter it searched the id of simple_list_item_1 for R.id.rowtext, which it did’t find. The fix was to change the above call to

FriendsArrayAdapter fAdapter = new FriendsArrayAdapter(FriendsActivity.this, R.layout.row_layout, mFriendsList);

(row_layout is the name of my XML file)

  1. Fly,

    What’s the output of that Log.i("name", friend.getmName()); statement? If friend is NOT null, then I’d guess that rowTxt is. Debug print it to find out…

    Further I’d guesse that you’ve got yourself a dodgy view-ID; or maybe that view just hasn’t been added to a collection. Are you missing some initialisation code earlier on?

    Cheers. Keith.

Originally posted 2013-11-27 12:09:26.