0

Please could you help me with the error "java.lang.NullPointerException". My application crashes in the emulator when I try to start my QuizActivity.java through the "Neues Spiel" Button from the MainActivity.java. (I cleaned the project and tried a different emulators) I looked for some answers and tried different things but i didn't find my mistake, maybe it is because I am an android-java newbie.

MainActivity.java

package com.example.app_name;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity{

    TextView titel;
    Typeface font_alexbrush;
    long score=0;


    @Override
    public void onBackPressed() {}
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Context context = this;
        final Button Beenden_btn = (Button) findViewById(R.id.Beenden_btn);
        final Button NeuesSpiel = (Button) findViewById(R.id.NeuesSpiel_btn);
        final Button Impressum = (Button) findViewById(R.id.Impressum_btn);

        Typeface font_alexbrush = Typeface.createFromAsset(getAssets(), "Font/alexbrush.ttf");
        TextView titel= (TextView) findViewById(R.id.titel);
        titel.setTypeface(font_alexbrush);

        Impressum.setOnClickListener(new OnClickListener(){     
            public void onClick(View x) {
                    Intent intent = new Intent(getApplicationContext(), Impressum.class);
                    startActivity(intent);}});   

        NeuesSpiel.setOnClickListener(new OnClickListener(){    
            public void onClick(View x) {
                    Intent intent = new Intent(getApplicationContext(), QuizActivity.class);

                    startActivity(intent);
                        finish();}});

            Beenden_btn.setOnClickListener(new OnClickListener(){   
                public void onClick(View arg0) {

                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

                    alertDialogBuilder  .setMessage("Soll HistoryQuiz beendet werden?")
                                        .setCancelable(false)
                                        .setPositiveButton("Ja",new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,int id) {    finish();
                                                                                                    System.exit(0);}})
                                        .setNegativeButton("Nein",new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,int id) {dialog.cancel();}});

                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();}});}}

LogCat

05-17 13:50:22.241: E/AndroidRuntime(3636): Caused by: java.lang.NullPointerException
05-17 13:50:22.241: E/AndroidRuntime(3636):     at com.example.app_name.QuizActivity.onCreate(QuizActivity.java:39)
05-17 13:50:22.241: E/AndroidRuntime(3636):     at android.app.Activity.performCreate(Activity.java:5133)
05-17 13:50:22.241: E/AndroidRuntime(3636):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-17 13:50:22.241: E/AndroidRuntime(3636):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-17 13:50:22.241: E/AndroidRuntime(3636):     ... 11 more
05-17 13:58:15.853: E/OpenGLRenderer(3679): Getting MAX_TEXTURE_SIZE from GradienCache
05-17 13:58:15.857: E/OpenGLRenderer(3679): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
05-17 13:58:16.617: E/AndroidRuntime(3679): FATAL EXCEPTION: main
05-17 13:58:16.617: E/AndroidRuntime(3679): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app_name/com.example.app_name.QuizActivity}: java.lang.NullPointerException
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.os.Looper.loop(Looper.java:137)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at java.lang.reflect.Method.invoke(Method.java:525)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at dalvik.system.NativeStart.main(Native Method)
05-17 13:58:16.617: E/AndroidRuntime(3679): Caused by: java.lang.NullPointerException
05-17 13:58:16.617: E/AndroidRuntime(3679):     at com.example.app_name.QuizActivity.onCreate(QuizActivity.java:39)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.app.Activity.performCreate(Activity.java:5133)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-17 13:58:16.617: E/AndroidRuntime(3679):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-17 13:58:16.617: E/AndroidRuntime(3679):     ... 11 more
Lal
  • 14,726
  • 4
  • 45
  • 70

3 Answers3

0

You are getting an NPE on the line frage.setText(currentQ.getQUESTION()); as currentQ is null in your question.

You will have to initialise currentQ before using it. Thus, to solve the error add ,

currentQ= new Question();

before using it. That is, add the above line,after the line

quesList=db.getAllQuestions();

Thus, it should be like

quesList=db.getAllQuestions();
currentQ= new Question();
Lal
  • 14,726
  • 4
  • 45
  • 70
0

Apparently is failing in this line:

frage.setText(currentQ.getQUESTION());

This will lead to the conclusion that frage or currentQ are null references. Based on that you are getting the instance of frage in this line:

frage = (TextView)findViewById(R.id.frage);

I will go for currentQ as the null reference.

Since you are instantiating this variable based on a condition

if(quesList!= null && quesList.size() !=0){
    currentQ=quesList.get(qid);}
}

So questList is null or empty. You are right, this is a classic noob developer error (not only Android). You must ensure that currentQ variable has a reference before using it or ask for a valid reference and you will be fine

if (currentQ != null) {
    frage.setText(currentQ.getQUESTION());
    a.setText(currentQ.getOPTA());
    b.setText(currentQ.getOPTB());
    c.setText(currentQ.getOPTC());
    d.setText(currentQ.getOPTD());
}
Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
0

The error (NullPointerException) means you are trying to access a null variable at line 39 of QuizActivity. If I counted right, this would be:

frage.setText(currentQ.getQUESTION());

Which means that either frage or currentQ is null at that moment. My guess would be currentQ, but to make sure, you can Log it like this:

Log.d("Tag", "Is frage null? " + (frage == null));
Log.d("Tag", "Is currentQ null? " + (currentQ == null));

You'll see the results in your logcat (which I'll assume you know how to use since it's off topic).

If you find out frage is null, it means findViewById(R.id.frage) returned null. This is the case when the activity can't find a view with the given ID in the layout set in setContentView(). So either the ID of the view is wrong or the layout is wrong.

If you find out currentQ is null, this means you haven't initiated your object before trying a function on it. In this case, you instantiate currentQ on the condition that:

quesList!= null && quesList.size() !=0

Which means it wasn't the case.

Hope this helps

Sunshinator
  • 940
  • 11
  • 23