41794

Android custom adapter getView() NullPointerException out of nowhere!

Question:

I've got a custom Adapter to supply data for ListView. Everything worked fine, but 'suddenly'(I don't have another word for what's now happening) my app started crashing while loading my Activity with listview:

W/dalvikvm( 228): threadid=3: thread exiting with uncaught exception (group=0x4001b188) E/AndroidRuntime( 228): Uncaught handler: thread main exiting due to uncaught exception E/AndroidRuntime( 228): java.lang.NullPointerException E/AndroidRuntime( 228): at AdvertisementListAdapter.getView(AdvertisementListAdapter.java:72) E/AndroidRuntime( 228): at android.widget.AbsListView.obtainView(AbsListView.java:1274) E/AndroidRuntime( 228): at android.widget.ListView.measureHeightOfChildren(ListView.java:1147) E/AndroidRuntime( 228): at android.widget.ListView.onMeasure(ListView.java:1060) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888) E/AndroidRuntime( 228): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350) E/AndroidRuntime( 228): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888) E/AndroidRuntime( 228): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350) E/AndroidRuntime( 228): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) E/AndroidRuntime( 228): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) E/AndroidRuntime( 228): at android.view.View.measure(View.java:7964) E/AndroidRuntime( 228): at android.view.ViewRoot.performTraversals(ViewRoot.java:763) E/AndroidRuntime( 228): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633) E/AndroidRuntime( 228): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 228): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime( 228): at android.app.ActivityThread.main(ActivityThread.java:4363) E/AndroidRuntime( 228): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 228): at java.lang.reflect.Method.invoke(Method.java:521) E/AndroidRuntime( 228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) E/AndroidRuntime( 228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) E/AndroidRuntime( 228): at dalvik.system.NativeStart.main(Native Method)

My getView method:

@Override public View getView(int position, View convertView, ViewGroup parent) { View view=convertView; ViewHolder holder; if(convertView==null){ view=inflater.inflate(R.layout.list_item, null); holder=new ViewHolder(); holder.imgItemImage=(ImageView)view.findViewById(R.id.imgItemImage); holder.tvItemTitle=(TextView)view.findViewById(R.id.tvItemTitle); holder.tvItemDetails=(TextView)view.findViewById(R.id.tvItemDetails); view.setTag(holder); }else holder=(ViewHolder)view.getTag(); holder.tvItemTitle.setText(data.get(position).getYear()+" "+data.get(position).getTitle()); StringBuilder sb=new StringBuilder(); sb.append(data.get(position).getPrice()); sb.append("\n"); sb.append(data.get(position).getCity()); holder.tvItemDetails.setText(sb.toString()); String imageUrl="http://myuri/"+data.get(position).getUrlList().get(0)+"/p-t.jpeg"; holder.imgItemImage.setTag(imageUrl); imageLoader.displayImage(imageUrl, activity, holder.imgItemImage); return view; }

When getView is first called, convertView=null, so ViewHolder object is created. But after calling findViewById(...) all members of ViewHolder class are still null. So, here are the origins NullPointerException. View inflating is successul as I can see during debug, view object contains RelativeLayout with it's children. The worst thing is that I haven't changed this part of app before it started crashing! This is driving me nuts!

Answer1:

Have you tried to rebuild the application from scratch? Project -> Clean

Answer2:

Try rebuilding the R class. In eclipse it's as simple as changing something in the layout XML and saving again to get the R.id.* up to date. Sometimes they get screwed up.

Answer3:

So, that's solved. I tried rebulding, cleaning project. Then I deleted it from workspace and reopened - it worked. That is Eclipse bug, that's horrible, I wasted couple of hours! Thanks to all for responses! Sorry for disturbing...

Recommend

  • Getting Force Close while running app on Emulator
  • Error in Jumping to method after some delay time
  • Android Listview Position is 0 on item selection
  • WearableListView ClassNotFoundException Support v4 ArrayMap
  • java.lang.outofmemoryerror exception while trying to display images in gridview
  • Android phone and bluetooth device communication error
  • Using ViewGroup throws NullPointerException
  • Android IntentService triggered with null intent
  • APS Cloud Push - Payload is null
  • Svd recomposition with Mathnet numerics library seems wrong
  • MultiDexExtractor in wrong dex file
  • Display charecters in textview with time delay - android
  • App crashing on clicking on EditText with latest updates (gradle 4.4 - android studio 3.1)
  • Android: posting message to HandlerThread makes UI thread unresponsive or gives IllegalStateExceptio
  • parsing XML configuration file using Etree in python
  • Unmarshalling unknown type code exception while resuming
  • Calling a constructor through reflection in scala 2.10
  • setContentView() is not enough to switch between layouts?
  • Android onKey w/ virtual keyboard
  • Enumerating Controls on a Form
  • Scanner nextInt() and hasNextInt() problems
  • Shouldn't else be indented in the below code
  • What causes the runtime difference in this trivial fortran code?
  • In loopback documentation what does variable 'cb' stands for?
  • calculate gradient output for Theta update rule
  • Conversion from string “a” to type 'Boolean' is not valid
  • Detect when Facebook like button is clicked
  • Marklogic : Query response time is very high
  • AJAX Html Editor Extender upload image appearing blank
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Obtain ObjectIdHex value from mgo query
  • what is the difference between the asp.net mvc application and asp.net web application
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Apache 2.4 - remove | delete | uninstall
  • Cannot Parse HTML Data Using Android / JSOUP
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Reading document lines to the user (python)