{"id":1626,"date":"2022-08-30T15:18:06","date_gmt":"2022-08-30T15:18:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/nullpointerexception-on-setadapter-with-my-friendsarrayadapter-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:06","modified_gmt":"2022-08-30T15:18:06","slug":"nullpointerexception-on-setadapter-with-my-friendsarrayadapter-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/nullpointerexception-on-setadapter-with-my-friendsarrayadapter-collection-of-common-programming-errors\/","title":{"rendered":"NullPointerException on SetAdapter With My FriendsArrayAdapter-Collection of common programming errors"},"content":{"rendered":"<p>The exception is occurring on the line &#8220;rowTxt.setText&#8221; and I can&#8217;t, for the life of me, figure out why.<\/p>\n<p>Here&#8217;s my code<\/p>\n<pre><code>    @Override\npublic View getView(int position, View convertView, ViewGroup parent)\n{\n    View rowView = convertView;\n\n    if(rowView == null)\n    {\n        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);\n        rowView = inflater.inflate(mResourceId, null);\n    }\n\n    Friend friend = mFriendsList.get(position);\n    TextView rowTxt = (TextView) rowView.findViewById(R.id.rowtext);\n    Log.i(\"name\", friend.getmName());\n    rowTxt.setText(friend.getmName());\n\n    return rowView;\n}\n<\/code><\/pre>\n<p>Logcat output:<\/p>\n<blockquote>\n<p>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<\/p>\n<\/blockquote>\n<p>EDIT: I figured out the issue with the help of corlettk. In short, I was assigning my listadapter like so:<\/p>\n<pre><code>FriendsArrayAdapter fAdapter = new FriendsArrayAdapter(FriendsActivity.this, android.R.simple_list_item_1, mFriendsList);\n<\/code><\/pre>\n<p>I did this because initially I had a generic listadapter that did not accommodate my Friend object; never thought to change it.<\/p>\n<p>So when setAdapter went into my FriendsArrayAdapter it searched the id of simple_list_item_1 for R.id.rowtext, which it did&#8217;t find. The fix was to change the above call to<\/p>\n<pre><code>FriendsArrayAdapter fAdapter = new FriendsArrayAdapter(FriendsActivity.this, R.layout.row_layout, mFriendsList);\n<\/code><\/pre>\n<p>(row_layout is the name of my XML file)<\/p>\n<ol>\n<li>\n<p>Fly,<\/p>\n<p>What&#8217;s the output of that <code>Log.i(\"name\", friend.getmName());<\/code> statement? If <em>friend<\/em> is NOT null, then I&#8217;d guess that <em>rowTxt<\/em> is. Debug print it to find out&#8230;<\/p>\n<p>Further I&#8217;d guesse that you&#8217;ve got yourself a dodgy view-ID; or maybe that view just hasn&#8217;t been added to a collection. Are you missing some initialisation code earlier on?<\/p>\n<p>Cheers. Keith.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:09:26. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The exception is occurring on the line &#8220;rowTxt.setText&#8221; and I can&#8217;t, for the life of me, figure out why. Here&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1626","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1626"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1626\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}