0

I am working on a quiz app with multiple domains. At first each domain will have 10 questions. I am working on the logic for the first domain so I will mostly copy the algorithm for the other ones. My problem is that displays the score on screen while taking the quiz but I can't take the value of it to the congrats class, so when that activity should start the app crashes.

Here's my code:

package com.example.iulian.quickquiz;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class info extends AppCompatActivity {

Qinfo qinfo = new Qinfo();
int score = 0;
int number = 0;
TextView intrbinfo;
TextView Score;
ImageView image;
Button ch1;
Button ch2;
Button ch3;
Button ch4;
String answer = qinfo.getAnswer(0);
Intent intent;
congrats congrats = new congrats();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info);
    intrbinfo = findViewById(R.id.intrbinfo);
    Score = findViewById(R.id.score);
    image = findViewById(R.id.image);
    ch1 = findViewById(R.id.ch1);
    ch2 = findViewById(R.id.ch2);
    ch3 = findViewById(R.id.ch3);
    ch4 = findViewById(R.id.ch4);
    intent = new Intent(info.this, congrats.class);

    intrbinfo.setText(qinfo.getQuestion(0));
    ch1.setText(qinfo.getChoice1(0));
    ch2.setText(qinfo.getChoice2(0));
    ch3.setText(qinfo.getChoice3(0));
    ch4.setText(qinfo.getChoice4(0));

    ch1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ch1.getText() == answer){
                number++;
                score++;
                updateScore(score);
                updateQuestion(number);
            }
            else{
                Toast.makeText(info.this, "Wrong answer!", Toast.LENGTH_SHORT).show();
                number++;
            }
        }
    });

    ch2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ch2.getText() == answer){
                number++;
                score++;
                updateScore(score);
                updateQuestion(number);
            }
            else{
                Toast.makeText(info.this, "Wrong answer!", Toast.LENGTH_SHORT).show();
                number++;
                updateQuestion(number);
            }
        }
    });

    ch3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ch3.getText() == answer){
                number++;
                score++;
                updateScore(score);
                updateQuestion(number);
            }
            else{
                Toast.makeText(info.this, "Wrong answer!", Toast.LENGTH_SHORT).show();
                number++;
                updateQuestion(number);
            }
        }
    });

    ch4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ch4.getText() == answer){
                number++;
                score++;
                updateScore(score);
                updateQuestion(number);
            }
            else{
                Toast.makeText(info.this, "Wrong answer!", Toast.LENGTH_SHORT).show();
                number++;
                updateQuestion(number);
            }
        }
    });


}



private void updateQuestion(int x){
    if(x<10){
    intrbinfo.setText(qinfo.getQuestion(x));
    ch1.setText(qinfo.getChoice1(x));
    ch2.setText(qinfo.getChoice2(x));
    ch3.setText(qinfo.getChoice3(x));
    ch4.setText(qinfo.getChoice4(x));
    answer = qinfo.getAnswer(x);}
    if(x >= 10){
        info.this.startActivity(intent);
    }
}

private void updateScore(int point){
    Score.setText(Integer.toString(point));
}

public String getScore(){
  return Score.getText().toString();
}

}

Here's the congratulations class

package com.example.iulian.quickquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class congrats extends AppCompatActivity {

TextView congrats;
TextView urscore;
TextView scoreView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_congrats);
    info info = new info();
    ist ist = new ist();
    geo geo = new geo();
    tele tele = new tele();
    congrats = findViewById(R.id.congrats);
    urscore = findViewById(R.id.urscore);
    scoreView = findViewById(R.id.score);
    scoreView.setText(info.getScore());
    }
    }

Here's what the logcat says at the moment of crashing:

2019-01-10 20:11:58.878 15597-15597/com.example.iulian.quickquiz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.iulian.quickquiz, PID: 15597
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iulian.quickquiz/com.example.iulian.quickquiz.congrats}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2812)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6311)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
    at com.example.iulian.quickquiz.congrats.onCreate(congrats.java:25)
    at android.app.Activity.performCreate(Activity.java:6757)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2704)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2812) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6311) 
    at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)  2019-01-10 20:11:58.926 686-748/? E/ThermalEngine: ========= Youtube Mode :0 ============== 2019-01-10 20:12:00.311 10646-10646/? E/WidgetViewCreator: setBgColorForSmartBulletin called 2019-01-10 20:12:00.313 10646-10646/? E/ActivityThread: Failed to find provider info for com.lge.launcher2.smartbulletin

  • 2
    I can't see any place where you call ```updateScore``` I suppose that's the method that updates the score. – danypata Jan 10 '19 at 10:19
  • i deleted and copied alot while trying to fix this and I probably missed that out, I am opening Android Studio and will add the call for the method.\ – Anghel Iulian Jan 10 '19 at 10:23
  • i added the method but it messes with the app now, pressing the right button doesnt do anything, it also skips questions – Anghel Iulian Jan 10 '19 at 10:36
  • 1
    If you put a breakpoint in your method ```updateScore``` is it triggered when you press the answer ?. Also don't use ```==``` to compare strings. – danypata Jan 10 '19 at 12:20
  • The method works fine during the test but when I have to show the score value in the congratulations class the app crashes. – Anghel Iulian Jan 10 '19 at 12:25
  • 1
    Can you post the crash too. Might be unrelated. – danypata Jan 10 '19 at 12:26
  • ok, i will post what the logcat says – Anghel Iulian Jan 10 '19 at 18:12
  • 1
    From the crash logs, it seems you are calling ```getText``` on a null view. Make sure you initialised all the views inside the app. – danypata Jan 11 '19 at 09:45
  • Thanks for the piece of advice, I verified the algorithm and used the intent class to pass the data from an activity to another and now it works fine! :) – Anghel Iulian Jan 11 '19 at 10:58

0 Answers0