-2

please I am new to android development.I am working on the flag quiz app on ditel's android 6 for programmers. Although I followed the source code meticulously, I don't seem to be able to run the app successfully, as it crashes almost immediately after coming up. This is the exception I am getting. I would appreciate any help that will solve this problem.

     FATAL EXCEPTION: main
                                                                       Process: com.example.israel.flagquiz, PID: 4644
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.israel.flagquiz/com.example.israel.flagquiz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.israel.flagquiz.MainActivityFragment.updateGuessRows(android.content.SharedPreferences)' on a null object reference
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                           at android.os.Looper.loop(Looper.java:194)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5637)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.israel.flagquiz.MainActivityFragment.updateGuessRows(android.content.SharedPreferences)' on a null object reference
                                                                           at com.example.israel.flagquiz.MainActivity.onStart(MainActivity.java:62)
                                                                           at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1244)
                                                                           at android.app.Activity.performStart(Activity.java:6140)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                           at android.os.Looper.loop(Looper.java:194) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5637) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

pertinent code in MainActivity

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

        if (preferencesChanged){
                MainActivityFragment quizFragment = (MainActivityFragment)
                        getSupportFragmentManager().findFragmentById(R.id.quizFragment);
            quizFragment.updateGuessRows(
                    PreferenceManager.getDefaultSharedPreferences(this));
            quizFragment.updateRegions(
                    PreferenceManager.getDefaultSharedPreferences(this));
            quizFragment.resetQuiz();
            preferencesChanged = false;
        }

pertinent code in Main Activity Fragment

              public void updateGuessRows(SharedPreferences sharedPreferences){
       String choices = new String();
     choices = sharedPreferences.getString(MainActivity.CHOICES, 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);
}
Itoooo
  • 1
  • 3
  • 1
    SO is a _terrible_ debugger! https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –  Aug 08 '17 at 19:20
  • Read this post: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Ivan Pronin Aug 08 '17 at 19:20

2 Answers2

1

sharedPreferences is null at this line

 choices = sharedPreferences.getString(MainActivity.CHOICES, null);

initialize sharedPreferences in on Start

akshay_shahane
  • 4,423
  • 2
  • 17
  • 30
  • 1
    That is not what the stack trace is saying - `Attempt to invoke virtual method 'void com.example.israel.flagquiz.MainActivityFragment.updateGuessRows(android.content.SharedPreferences)' on a null object reference`. – Mike M. Aug 08 '17 at 21:54
0

Add the below line to initialize your shared preferences

If using in an Activity

sharedPreferences = getSharedPreferences("MainActivity", Context.MODE_PRIVATE);

If using in a Fragment

sharedPreferences = getActivity().getSharedPreferences("MainActivity", Context.MODE_PRIVATE);

Once it is initialized, you can read data from shared preferences file.

Mitali Bhokare
  • 177
  • 1
  • 2
  • 11