show error \. java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

Marsad :

I'm creating quiz app. When all the questions are answered. I set an intent to open score activity. But the app crash and show error in set activity structure (MainActivity -> CategoryActivity -> SetsActivity -> QuestionActivity -> ScoreActivity)

here is the logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marsad.quiz/com.marsad.quizapp.SetsActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2890)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2968)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6711)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
        at com.marsad.quizapp.SetsActivity.onCreate(SetsActivity.java:68)
        at android.app.Activity.performCreate(Activity.java:7181)
        at android.app.Activity.performCreate(Activity.java:7172)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2968) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:192) 
        at android.app.ActivityThread.main(ActivityThread.java:6711) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826) 

pass from CategoryActivity

    public static List<CategoryModel> list;

Error is in this line

        sets = CategoryActivity.list.get(getIntent().getIntExtra("position", 0)).getSets();

Here is the SetsActivity.java

public class SetsActivity extends AppCompatActivity {

    private GridView gridView;
    private List<String> sets;
    private InterstitialAd mInterstitialAd;
    //Theme
    Constant constant;
    SharedPreferences app_preferences;
    int appTheme;
    int themeColor;
    int appColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        appColor = app_preferences.getInt("color", 0);
        appTheme = app_preferences.getInt("theme", 0);
        //defaultColor = app_preferences.getInt("default", 0);
        themeColor = appColor;
        constant.color = appColor;

        if (themeColor == 0){
            setTheme(Constant.theme);
        }else if (appTheme == 0){
            setTheme(Constant.theme);
        }else{
            setTheme(appTheme);
        }

        setContentView(R.layout.activity_sets);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(getIntent().getStringExtra("title"));
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        gridView = findViewById(R.id.gridView);



        sets = CategoryActivity.list.get(getIntent().getIntExtra("position", 0)).getSets();



        GridAdapter adapter = new GridAdapter(sets, getIntent().getStringExtra("title"), mInterstitialAd);

        gridView.setAdapter(adapter);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            finish();
        }

        return super.onOptionsItemSelected(item);
    }


}


Dipankar Baghel :

you are correct issue in line sets = CategoryActivity.list.get(getIntent().getIntExtra("position", 0)).getSets();

CategoryActivity.list is null actually

please check the object is initialize or not in list so before accessing check -

if(CategoryActivity.list != null){
   sets = CategoryActivity.list.get(getIntent().getIntExtra("position", 0)).getSets();
   GridAdapter adapter = new GridAdapter(sets, getIntent().getStringExtra("title"), mInterstitialAd);

    gridView.setAdapter(adapter);
}

Hope this will help you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=237962&siteId=1