0

This is on Android.

In one of my bug report I often see this

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference at java.util.concurrent.ConcurrentHashMap.replaceNode(ConcurrentHashMap.java:942) at java.util.concurrent.ConcurrentHashMap.remove(ConcurrentHashMap.java:933) at xxxx.MyActivity$1.handleMessage(MyActivity.java:261) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

The code at line 261 is

handler = new Handler(Looper.getMainLooper()) {
      @Override
      public void handleMessage(Message inputMessage) {

         switch (inputMessage.what) {
           case 'test': 
              Object[] args = (Object[]) inputMessage.obj;
              LatLng latLng = (LatLng) args[0];

              // This is the line where the error is thrown
              Circle circle = mapCircles.remove(latLng);
         }

}

Basically my threads send a message to the UI handler. mapCircles is a ConcurentHashMap

I know it is hard to help, but does any one know what causes the java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' ?

Does the latLng object becomes null at some points in its lifetime? if latLng WAS null the errorwould have been something else mapCircles is definitely not null

Johny19
  • 5,364
  • 14
  • 61
  • 99
  • 1
    An object can never 'become null'. A variable/field can only change from evaluating from non-null (an object) to null (lack of an object) if it has been re-assigned a null value. – user2864740 Aug 24 '16 at 01:23
  • Have you debugged your code? Is `mapCircles` or `latLng` null? – Tim Biegeleisen Aug 24 '16 at 01:23
  • 1
    @ScaryWombat An object can never become null. The variable `a` briefly *names* the object (or "stores a non-null reference to the object such that it can be found later") before it is re-assigned the null value. – user2864740 Aug 24 '16 at 01:25
  • `latLng` is null ... it's up to you to find out why. – Tom Aug 24 '16 at 01:26
  • It is not null when calling .remove. If I call .remove(null) I have a totally different NullPointException. So this is why I was thinking when .remove(latlng) is called latlng is not null but sometimes before hitting the hashcode something somewhere either set that object to null. Could that explain the error? – Johny19 Aug 24 '16 at 11:51
  • @ ScaryWombat please re-read my question. LatLng is NOT null (at least when calling remove in the first place) – Johny19 Aug 24 '16 at 11:52

0 Answers0