-5

So I'm getting the error java.lang.NumberFormatException: s == null and I don't know why.. I posted a question earlier and it ended up being a typo, but for this one I think I can say that it's not. If you need more info don't hesitate to say so. Thanks in advance!

Here's the full error code

E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.flagquiz, PID: 3343

java.lang.NumberFormatException: s == null
    
    at java.lang.Integer.parseInt(Integer.java:577)
    at java.lang.Integer.parseInt(Integer.java:650)
    at com.example.flagquiz.MainActivityFragment.updateGuessRows(MainActivityFragment.java:104)
    at com.example.flagquiz.MainActivity.onStart(MainActivity.java:65)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
    at android.app.Activity.performStart(Activity.java:8018)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

and here's the affected code:

MainActivity:

      protected void onStart(){
        super.onStart();

        if(preferencesChanged){
            MainActivityFragment quizFragment = (MainActivityFragment)
                    getSupportFragmentManager().findFragmentById(
                            R.id.quizFragment);

            result = quizFragment;

            quizFragment.updateGuessRows(
                    PreferenceManager.getDefaultSharedPreferences(this));
            quizFragment.updateRegions(
                    PreferenceManager.getDefaultSharedPreferences(this));
            quizFragment.resetQuiz();
            preferencesChanged = false;
        }
    }

And MainActivityFragment:

public void updateGuessRows(SharedPreferences sharedPreferences){
        String choices =
                sharedPreferences.getString(MainActivity.CHOICE, null);
        guessRows = Integer.parseInt(choices) / 2;

        for(LinearLayout layout : guessLinearLayouts)
            layout.setVisibility(View.GONE);
        for(int row = 0; row < guessRows; row++)
            guessLinearLayouts[row].setVisibility(View.VISIBLE);
    }
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Ayy Lmao
  • 1
  • 1

1 Answers1

0

The exception message tells us that at the line guessRows = Integer.parseInt(choices) / 2; the variable choices must be null.

You read that value in the line before:

String choices = sharedPreferences.getString(MainActivity.CHOICE, null);

This line can only set choices to null if there is no key MainActivity.CHOICE.

Instead of using a default value of null you probably should use "0" as default value:

String choices = sharedPreferences.getString(MainActivity.CHOICE, "0");

But, as Martin Zeitler has noted in his comment: if your preference value is a number value that fits in the range of the int data type it is better to store it as int value in the shared preferences.

That would allow to replace

    String choices = sharedPreferences.getString(MainActivity.CHOICE, null);
    guessRows = Integer.parseInt(choices) / 2;

with

    guessRows = sharedPreferences.getInt(MainActivity.CHOICE, 0) / 2;
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34