1
votes

I have two activities MainActivity and SecondActivity

Code in activity MainActivity .java

public class MainActivity extends FragmentActivity implements OnMarkerClickListener {

@Override
 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getWindow().requestFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.activity_main_screen);
      databaseobj = new AppDatabase(MainActivity.this, null);
      listview = (ListView) findViewById(R.id.store_listview);
      noSavedMsg = (TextView) findViewById(R.id.no_msg);
      ..
      ..
      ..//Other code logic
      ..
 }

 @Override
 protected void onResume() {
      if (databaseobj.GetAll() != -1) { //NULL POINTER EXCEPTION
      listview.setVisibility(View.GONE);  //NULL POINTER EXCEPTION
      noSavedMsg.setVisibility(View.VISIBLE); //NULL POINTER EXCEPTION
      }
      super.onResume();
  }
}

I am getting the null pointer exception after returning from another activity;

MainActivity -> new intent to SecondActivity

SecondActivity->finish() then MainActivity-> onResume() NULL POINTER EXCEPTION

Please help!

3
what is NoSavedMsg? - Shayan Pourvatan
1. How are the variables declared? 2. Usually super.onResume() is first and not last in the overridden method. - laalto
did any of the given solution solved your problem? If did then you should accept the best solution. - Hamid Shatu

3 Answers

0
votes

Your TextView name is noSavedMsg but you are trying set visibility to a view named NoSavedMsg...where these two are not same.

Change this line...

NoSavedMsg.setVisibility(View.VISIBLE);

to...

noSavedMsg.setVisibility(View.VISIBLE);
0
votes

You sure you need that capital N there?

NoSavedMsg.setVisibility(View.VISIBLE);

And not:

noSavedMsg.setVisibility(View.VISIBLE);
0
votes
databaseobj = new AppDatabase(MainActivity.this, null);
listview = (ListView) findViewById(R.id.store_listview);
noSavedMsg = (TextView) findViewById(R.id.no_msg);

Add this code to the onResume method and see if it works.