-3

My application stopped when I finished working on the quiz application. I'm learning to make an Android application quiz using MySQL database, but I get the problem when the last question will move to the scoring page.

10-28 01:02:25.816 31874-31874/com.example.nuvo.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nuvo.myapplication, PID: 31874 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nuvo.myapplication/com.example.nuvo.myapplication.Complete}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) at android.app.ActivityThread.access$800(ActivityThread.java:166) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.nuvo.myapplication.Complete.onCreate(Complete.java:18) at android.app.Activity.performCreate(Activity.java:5442) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)  at android.app.ActivityThread.access$800(ActivityThread.java:166)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5584)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)  at dalvik.system.NativeStart.main(Native Method)

Complate.java

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class Complete extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete);
        int score = getIntent().getExtras().getInt("score");


        AlertDialog.Builder builder = new AlertDialog.Builder(Complete.this);
        builder.setTitle("Quiz Complete");
        builder.setMessage("Your Score is: " +score+ " out of 10");
        builder.show();


        builder.setPositiveButton("Play Again", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //do things
                Intent intent = new Intent(Complete.this, QuizActivity.class);
                startActivity(intent);

            }
        });

        builder.setNegativeButton("Done", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //do things
            }
        });
        AlertDialog alert = builder.create();
        alert.show();

    }
    public void restartQuiz(View view)
    {
        Intent intent = new Intent(Complete.this, QuizActivity.class);
        startActivity(intent);
    }
    public void mainMenu(View view)
    {
        Intent intent = new Intent(Complete.this, MainActivity.class);
        startActivity(intent);
    }

}

QuizActivity.java

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class Complete extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete);
        int score = getIntent().getExtras().getInt("score");


        AlertDialog.Builder builder = new AlertDialog.Builder(Complete.this);
        builder.setTitle("Quiz Complete");
        builder.setMessage("Your Score is: " +score+ " out of 10");
        builder.show();


        builder.setPositiveButton("Play Again", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //do things
                Intent intent = new Intent(Complete.this, QuizActivity.class);
                startActivity(intent);

            }
        });

        builder.setNegativeButton("Done", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //do things
            }
        });
        AlertDialog alert = builder.create();
        alert.show();

    }
    public void restartQuiz(View view)
    {
        Intent intent = new Intent(Complete.this, QuizActivity.class);
        startActivity(intent);
    }
    public void mainMenu(View view)
    {
        Intent intent = new Intent(Complete.this, MainActivity.class);
        startActivity(intent);
    }

}
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52

1 Answers1

0

This line is problematic in your code:

int score = getIntent().getExtras().getInt("score");

It seems that your Intent, which opened your Complete activity did not have an integer "score" and hence returned null, which caused NullPointerException. To avoid such problems consider using another method getInt(String key, int defaultValue) s.a. this:

int score = getIntent().getExtras().getInt("score", 0);

This way you'll not get an exception if your Intent arrives without a value, but it will be set to 0 by default.

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52