0

I have created an ArrayList with count

 private ArrayList<GeoJsonResponse> localGeoJsonResponse;
FeatureCollection featureCollection;
GeoJsonResponse data = null;
Integer number = localGeoJsonResponse.size();
String str = Integer.toString(number);
String event= str + " total event";

I have to set a value of the count on sub title of action bar and this is my code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    ActionBar ab = getActionBar();
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setHomeButtonEnabled(true);
    ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.theme_color)));
    ab.setTitle(R.string.mappa);
    ab.setSubtitle(event);

I receive an error when i call this view. In the setSubtitle I can set only String, example

ab.setSubtitle("events");

rest of the view works well. Any help please? Thanks

this is the log of error

Caused by: java.lang.NullPointerException
        at com.###.###.###.MyMapActivity.onCreate(MyMapActivity.java:164)
        at android.app.Activity.performCreate(Activity.java:5133)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at

thanks

dev_
  • 395
  • 1
  • 3
  • 16

1 Answers1

0

There are two possible places in your code where NullPointerException may occur:

  1. Your localGeoJsonResponse wasn't initialised but you call

    localGeoJsonResponse.size(); -> which may result in null pointer
    

    You have to do as @Teodor Liv's answer:

    ArrayList<GeoJsonResponse> localGeoJsonResponse = new ArrayList<GeoJsonResponse>();
    
  2. Another one is that you don't really get the ActionBar object, so when you call:

    ab.setDisplayHomeAsUpEnabled(true); -> This may cause null pointer
    

    For checking, you can do like this:

    ActionBar ab = getAcionBar();
    if (ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeButtonEnabled(true);
        ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.theme_color)));
        ab.setTitle(R.string.mappa);
        ab.setSubtitle(event);
    } else {
        Log.e(TAG, "fail to get ActionBar");
    }       
    
pptang
  • 1,931
  • 1
  • 15
  • 18