-1

I getting error as null pointer exception but as per my check, cycle flow is ok. i get error on this code

bitmap = BitmapFactory.decodeResource(getResources(), MapId.getId(mapName));
mIndiaBitmapLoadedListener.onIndiaBimapLoaded(bitmap);
map.setImageBitmapReset(bitmap, true);

This is my MapId class

 public class MapId 
    {
        public static HashMap<String,Integer> mapids = new HashMap<String, Integer>();
        public static void setHashMap()
        {       
            mapids.put("Political Map",R.drawable.indiapoliticalbig);
            mapids.put("Physical Map",R.drawable.physicalmap);
            mapids.put("Railway Map", R.drawable.railwaymap);
            mapids.put("Road Map", R.drawable.roadmap);
            mapids.put("Tourist Map", R.drawable.touristmap);
        }
        public static int getId(String stateName)
        {
            return mapids.get(stateName);
        }
    }

This is my Error log

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
    at com.compare.IndiaAtlas.MapId.getId(MapId.java:58)
    at com.compare.IndiaAtlas.IndiaFragment.onCreateView(IndiaFragment.java:62)
    at android.app.Fragment.performCreateView(Fragment.java:2053)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
    at android.app.BackStackRecord.run(BackStackRecord.java:834)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

we add three new entry in sethashmap() Railway Map, Road Map & Tourist Map these are get error but previous entry Physical and Political Map is Running properly.

Hardik Parmar
  • 712
  • 2
  • 13
  • 28
Yuvaan Chauhan
  • 344
  • 1
  • 3
  • 12
  • 3
    I like the fact that your error states `Integer.intValue()' on a null object reference`, however the relevant code exists nowhere in the post. Voting to close as duplicate, and if I could, for lack of context. – Matt Clark Aug 09 '16 at 15:44

1 Answers1

1

I think you should initialize your map like this:

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MapId {
    private static Map<String, Integer> mapids;
    static {
        mapids = new HashMap<String, Integer>();
        mapids.put("Political Map", R.drawable.indiapoliticalbig);
        mapids.put("Physical Map", R.drawable.physicalmap);
        mapids.put("Railway Map", R.drawable.railwaymap);
        mapids.put("Road Map", R.drawable.roadmap);
        mapids.put("Tourist Map", R.drawable.touristmap);
        mapids = Collections.unmodifiableMap(mapids);
    }
    public static int getId(String stateName) {
        if (mapids.containsKey(stateName)) {
            return mapids.get(stateName);
        }
        return 0;
    }
}

And then just use: MapId.getId("Tourist Map");

fernandospr
  • 2,976
  • 2
  • 22
  • 43