0

Problem: When user starts the game, he is given 6 words to choose from, these 6 words are randomly chosen from a list. The first time the activity is interrupted, (such as screen rotates), everything works fine. If the screen rotates again, the app crashes and the logcat spits out the following result:

  java.lang.NullPointerException: Attempt to invoke virtual method '
java.lang.Object java.util.LinkedList.get(int)' on a null object reference

Here is the relevant code:

 public class GameActivity extends Activity {
    protected String rWord1;
    protected String rWord2;
    protected String rWord3;
    protected TextView word1;
    protected LinkedList<String> mCopy;
    protected TextView word2;
     /// all the way to word6

    @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    if (savedInstanceState != null) {
         rWord1 = savedInstanceState.getString("word1");
         rWord2 = savedInstanceState.getString("word2");
         rWord3 = savedInstanceState.getString("word3");


         word1 = (TextView) findViewById(R.id.word1);
         word1.setText(mSavedList.get(1));

        word2 = (TextView) findViewById(R.id.word2);
        word2.setText(mSavedList.get(2));
         //up until word6

     }

   else {
    //take the list of all the words in the LinkedList mCopy, 
     randomize it, and pick the first         six.

   }

}// end of onCreate method 

  @Override
   public void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putString("word1", mCopy.get(1));
        savedInstanceState.putString("word2", mCopy.get(2));
        savedInstanceState.putString("word3", mCopy.get(3));


    super.onSaveInstanceState(savedInstanceState);
}

I think I know what the problem is: When the activity is created for the first time and the screen rotates, the list(mCopy) is saved in the bundle. However, when the screen rotates a second time, this time mCopy is null because it has not had the chance to be created (because of the if (savedInstanceState != null) condition). I am not sure how to get around this error. I tried creating Strings to reference the list,

 String sWord1 = mCopy.get(1),  

and then store this String in the bundle instead, but then when I run the app and rotate the screen twice, the app does not crash, but all the words disappear from the view.

EDIT I tried the methods suggested by some of the commentators below, the updated code is:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_activity);


        Collection<String> wordList = new LinkedList<String>();

        wordList.add("ant");
        wordList.add("almond");
        /// lots of words

     mCopy = new LinkedList<String>(wordList);
    if (savedInstanceState != null) {

            rWord1 = savedInstanceState.getString("word1");
            rWord2 = savedInstanceState.getString("word2");


            word1 = (TextView) findViewById(R.id.word1);
            word1.setText(rWord1);

            word2 = (TextView) findViewById(R.id.word2);
            word2.setText(rWord2);

        }

        else {

            Collections.shuffle(mCopy);

        word1 = (TextView) findViewById(R.id.word1);
        word1.setText(mCopy.get(1));

Now, there is a new problem: The first time the screen rotates, the activity retains the words. IF the screen rotates again, the activity changes the words. However, any subsequent change will result in the data being maintained.

For example: on activity start: word appearing on-screen is "pain" on the first screen rotation: "pain" on the second screen rotation: "blue" on third rotation: "blue"

Basically, it seems the code is running after the first screen rotation instead of before.

Also, if the user has 3 games open against 3 different players, it doesn't matter what the initial word was in any of the game. On the second screen rotation, the word always appears as "blue" against all 3 players.

Frosty619
  • 1,381
  • 4
  • 23
  • 33
  • Where are you initializing your list ? – user1841702 Sep 28 '15 at 02:34
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Sep 28 '15 at 02:42
  • I am initializing it in the onCreate method, right after if (savedInstanceState != null), I think I should do it before this if statement, right? – Frosty619 Sep 28 '15 at 02:49

2 Answers2

1

Add this configChanges properties into your androidManifest.xml to retain all activity state and resources even if the screen rotates. This will help because every time the screen is rotated old activity is closed and recreated that means there might be instances other resources won't be instantly recreated as a result it will show null pointer.

  <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize|screenLayout"
Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
  • 1
    Excellent suggestion, I will def. resort to this if the other solutions are not working. – Frosty619 Sep 28 '15 at 05:08
  • @Frosty619 those simple codes works like magic right :-) we have the same hesitation before but sometimes making things uncomplicated is the answer :-) – Cristiana Chavez Sep 29 '15 at 03:58
0

As your logcat, you should add it in onCreate(...) method

  mCopy = new LinkedList();

Hope this help

Linh
  • 57,942
  • 23
  • 262
  • 279