1

Brief description about what I am trying to do:

  1. It's a simple messaging app

  2. In my first activity I have a list-view and few buttons.

    • User presses any of the button and the information about which button was pressed was stored in shared preferences.
    • When user presses any list item, an intent is triggered that takes him to new activity where which list item was pressed is analyzed by putExtra() from intent.
  3. In second activity, depending on which value came with intent, a string value for newString is decided.

Problem When screen rotates, app crashes with this error -

Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference

Following is the code in second activity -

String newString = "Default_Channel";

public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;

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

    .
    .
    .

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    String restoredText = sharedpreferences.getString("CurrentUser", null);
    if (restoredText != null) {
        currentUserName = sharedpreferences.getString("CurrentUser", "unknown");//"unknown" is the default value.
        .
        .
        .
    }
    else
    {
        currentUser.setText("not signed in");
    }


    if (savedInstanceState == null)
    {
        Bundle extras = getIntent().getExtras();
        if(extras == null)
        {
            newString= "Default_Channel";
        }
        else
        {
            newString= extras.getString("LIST_ITEM_CLICKED");
        }
    }
    else
    {
        newString= (String) savedInstanceState.getSerializable("LIST_ITEM_CLICKED");
    }
     // *** GETTING ERROR HERE On this line *** // 

    switch (newString)
    {
        case "User_1":
            .
            .
            .
            break;
        case "User_2":
            .
            .
            .
            break;
        case "User_3":
            .
            .
            .
            break;
        case "Default_Channel":
            .
            .
            .
            break;
    }

I am getting NULL pointer exception where I have mentioned it in code ! Its Line between Else statement and Switch.

Also, I understand what is NULL pointer exception and this is not what I am asking. I am getting NULL pointer when screen rotates, i.e. when activity restarts without intent.

I am new to android programming and not able to figure out what's going wrong here.

Edit: This is not duplicate question with "What is NULL pointer exception".

halfer
  • 19,824
  • 17
  • 99
  • 186
Nis
  • 449
  • 4
  • 13
  • 1
    Thanks! I appreciate you posting the link. But I already had looked at it and This is not what I am looking for. I know what is Null pointer exception. I am trying to figure out what is causing it 'after screen rotation' . – Nis Aug 16 '15 at 10:23

2 Answers2

2

I see you use this:

newString= (String) savedInstanceState.getSerializable("LIST_ITEM_CLICKED");

So do you override onSaveInstanceState?
If not, override it and save your current value of "LIST_ITEM_CLICKED".

I think your problem is because when rotate, activity is recreated with a savedInstanceState, but you don't save your value, so a NPE occurs.

justHooman
  • 3,044
  • 2
  • 17
  • 15
  • Yeah! You got it right :) Thanks a lot! I did not override onSaveInstanceState, it was causing the error and crash. – Nis Aug 16 '15 at 10:39
0

Basically you are getting NullPointerException.The code you posted is not clear where actually you are getting this. You need to check whether object on which you are performing operation is null or not and proceed based on that. See What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314