• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android app Math game Crashes

miltostar

Lurker
I am new to Android Development and I am trying to build an app for a school project. I searched the Internet and found some guides/codes for the Math app that I want to create. I copy/pasted some of them and I managed to create the preferable UI. When I launch the game everything is fine, but when I play the game crashed. The game is about basic mathematical operations. The answers are random (eg. 5x5=?) and the user must answer. If the answer is right a tick appears and when it is wrong a cross appears. When I use the app I manage to enter the answer but when i click enter or C button the app crashes..Any ideas? (I am new to this and I hope I understand what you say to me :p ) Thanks!
The error appears to be in this line of code:
int enteredNum = Integer.parseInt(question.getText().toString());
but i cant figure it out!

Main xml:

Code:
  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_display_message"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.miltos.maths.DisplayMessageActivity">

    <include
        android:id="@id/toolbar"
        layout="@layout/tool_bar"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:eek:rientation="vertical"
        android:background="#84b3e7">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:eek:rientation="horizontal">

            <ImageView
                android:id="@+id/response"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:contentDescription="result"
                android:src="@drawable/tick"
                android:layout_gravity="bottom|left" />





        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:eek:rientation="horizontal" >


            <TextView
                android:id="@+id/question"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="2dp"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:background="#dfeaf1"
                android:gravity="center|right"
                android:padding="5dp"
                android:text="0 + 0"
                android:textColor="#ff333333"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/answer"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:background="#dfeaf1"
                android:gravity="center|left"
                android:padding="5dp"
                android:text="= ?"
                android:textColor="#ff333333"
                android:textSize="30sp" />

            <Button
                android:id="@+id/clear"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#dfeaf1"
                android:gravity="center"
                android:padding="5dp"
                android:text="C"
                android:textColor="#ff333333"
                android:textSize="30sp"
                android:textStyle="bold" />



        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:eek:rientation="horizontal" >

            <Button
                android:id="@+id/btn7"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="7"
                android:text="7"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn8"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="8"
                android:text="8"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn9"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="9"
                android:text="9"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:eek:rientation="horizontal" >

            <Button
                android:id="@+id/btn4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="4"
                android:text="4"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn5"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="5"
                android:text="5"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn6"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="6"
                android:text="6"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:eek:rientation="horizontal" >

            <Button
                android:id="@+id/btn1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="1"
                android:text="1"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="2"
                android:text="2"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="3"
                android:text="3"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:eek:rientation="horizontal" >

            <Button
                android:id="@+id/btn0"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:tag="0"
                android:text="0"
                android:textColor="#ffcccccc"
                android:textSize="30sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/enter"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="2"
                android:background="#86959f"
                android:gravity="center"
                android:padding="5dp"
                android:text="Enter"
                android:textColor="#ff333333"
                android:textSize="30sp"
                android:textStyle="bold" />
        </LinearLayout>





    </LinearLayout>

    </RelativeLayout>[/INDENT]
Java code:
Code:
package com.example.miltos.maths;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import java.util.Random;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class DisplayMessageActivity extends AppCompatActivity implements OnClickListener

{
    private int level = 0,answer = 0, operator = 0, operand1 = 0, operand2 = 0;
    private final int ADD_OPERATOR = 0, SUBTRACT_OPERATOR = 1, MULTIPLY_OPERATOR = 2, DIVIDE_OPERATOR = 3;
    private String[] operators = {"+", "-", "x", "/"};
    private int[][] levelMax = {
            {10, 25, 50},
            {10, 20, 30},
            {5, 10, 15},
            {10, 50, 100}};
    private Random random;
    private TextView question, answerTxt;
    private ImageView response;
    private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, enterBtn, clearBtn;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);



        question =  (TextView)findViewById(R.id.question);
        answerTxt = (TextView)findViewById(R.id.answer);
        response =  (ImageView)findViewById(R.id.response);
        response.setVisibility(View.INVISIBLE);
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        btn4 = (Button)findViewById(R.id.btn4);
        btn5 = (Button)findViewById(R.id.btn5);
        btn6 = (Button)findViewById(R.id.btn6);
        btn7 = (Button)findViewById(R.id.btn7);
        btn8 = (Button)findViewById(R.id.btn8);
        btn9 = (Button)findViewById(R.id.btn9);
        btn0 = (Button)findViewById(R.id.btn0);
        enterBtn = (Button)findViewById(R.id.enter);
        clearBtn = (Button)findViewById(R.id.clear);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn0.setOnClickListener(this);
        enterBtn.setOnClickListener(this);
        clearBtn.setOnClickListener(this);

        btn1.setTag('1');
        btn2.setTag('2');
        btn3.setTag('3');
        btn4.setTag('4');
        btn5.setTag('5');
        btn6.setTag('6');
        btn7.setTag('7');
        btn8.setTag('8');
        btn9.setTag('9');
        btn0.setTag('0');


        Bundle extras = getIntent().getExtras();
        if(extras != null)
        {
            int passedLevel = extras.getInt("level", -1);
            if(passedLevel>=0) level = passedLevel;
        }

        random = new Random();
        chooseQuestion();
    }
    private void chooseQuestion(){
        answerTxt.setText("= ?");
        operator = random.nextInt(operators.length);
        operand1 = getOperand();
        operand2 = getOperand();

        if(operator == SUBTRACT_OPERATOR){
            while(operand2>operand1){
                operand1 = getOperand();
                operand2 = getOperand();
            }
        }

        else if(operator==DIVIDE_OPERATOR){
            while((((double)operand1/(double)operand2)%1 > 0) || (operand1==operand2))
            {
                operand1 = getOperand();
                operand2 = getOperand();
            }
        }
        switch(operator)
        {
            case ADD_OPERATOR:
                answer = operand1+operand2;
                break;
            case SUBTRACT_OPERATOR:
                answer = operand1-operand2;
                break;
            case MULTIPLY_OPERATOR:
                answer = operand1*operand2;
                break;
            case DIVIDE_OPERATOR:
                answer = operand1/operand2;
                break;
            default:
                break;
        }
        question.setText(operand1+" "+operators[operator]+" "+operand2);
    }
    private int getOperand(){
        return random.nextInt(levelMax[operator][level] - levelMax[operator][level] + 1)
                + levelMax[operator][level];

    }




    @Override
    public void onClick(View view) {

        if(view.getId()==R.id.enter){
            String answerContent = answerTxt.getText().toString();
            if(!answerContent.endsWith("?"))
            {
                int enteredAnswer = Integer.parseInt(answerContent.substring(2));
                if(enteredAnswer==answer){

                    response.setImageResource(R.drawable.tick);
                    response.setVisibility(View.VISIBLE);

                }
                else{
                    response.setImageResource(R.drawable.cross);
                    response.setVisibility(View.VISIBLE);
                }
                chooseQuestion();
            }

        }
        else if(view.getId()==R.id.clear){
            answerTxt.setText("= ?");
        }
        else {
            response.setVisibility(View.INVISIBLE);
        }
 int enteredNum = Integer.parseInt(view.getTag().toString());

        if(answerTxt.getText().toString().endsWith("?"))
            answerTxt.setText("= "+enteredNum);
        else
            answerTxt.append(""+enteredNum);
    }
}
Error log:
Code:
01-17 09:36:09.244 5665-5709/com.example.miltos.maths E/EGL_emulation: tid 5709: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-17 09:36:11.516 5665-5665/com.example.miltos.maths E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.miltos.maths, PID: 5665
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                            at com.example.miltos.maths.DisplayMessageActivity.onClick(DisplayMessageActivity.java:173)
                                                                            at android.view.View.performClick(View.java:5610)
                                                                            at android.view.View$PerformClick.run(View.java:22265)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
01-17 09:36:11.542 1249-1304/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
01-17 09:36:11.598 1560-5811/system_process E/EGL_emulation: tid 5811: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
01-17 09:36:11.660 1560-1574/system_process E/ActivityManager: Found activity ActivityRecord{33ffeca u0 com.example.miltos.maths/.MainActivity t92 f} in proc activity list using null instead of expected ProcessRecord{364342e 5665:com.example.miltos.maths/u0a26}
01-17 09:36:11.699 1560-1614/system_process E/InputDispatcher: channel 'b217aa5 com.example.miltos.maths/com.example.miltos.maths.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-17 09:36:11.699 1560-1614/system_process E/InputDispatcher: channel 'ee125b8 com.example.miltos.maths/com.example.miltos.maths.DisplayMessageActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-17 09:36:11.752 2140-2220/com.android.launcher3 E/EGL_emulation: tid 2220: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
 
Last edited:
Please read this

http://androidforums.com/threads/please-read-me-before-posting.987318/

The stack trace tells you exactly what happened

Code:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.miltos.maths.DisplayMessageActivity.onClick(DisplayMessageActivity.java:156)

It's really difficult to read your code because it's not correctly formatted in [code][/code] tags, but at a guess, I would say that this line is causing the problem, most likely because the view has no tag associated with it.

Code:
int enteredNum = Integer.parseInt(view.getTag().toString());

You see the problem with pasting together a load of code from the Internet, is that when it goes wrong, you have virtually no chance of understanding why. If this is for a school project, then its purpose is to make you learn.
What I would suggest is first reading about the basics of the Java programming language. Then read about Android development. Then learn about debugging techniques, such as setting breakpoints in your code.
 
Please read this

http://androidforums.com/threads/please-read-me-before-posting.987318/

The stack trace tells you exactly what happened

Code:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.miltos.maths.DisplayMessageActivity.onClick(DisplayMessageActivity.java:156)

It's really difficult to read your code because it's not correctly formatted in [code][/code] tags, but at a guess, I would say that this line is causing the problem, most likely because the view has no tag associated with it.

Code:
int enteredNum = Integer.parseInt(view.getTag().toString());

You see the problem with pasting together a load of code from the Internet, is that when it goes wrong, you have virtually no chance of understanding why. If this is for a school project, then its purpose is to make you learn.
What I would suggest is first reading about the basics of the Java programming language. Then read about Android development. Then learn about debugging techniques, such as setting breakpoints in your code.

I know that this line has the problem. I mentioned it above in my original post. But do we know any way to fix it? I am trying to learn Android development from school but the learning experience isn't the best there. So I have to find things on my own and I am stuck with errors like these. Thanks for the answer though!
 
Can you please clarify what is at line 156? The code you pasted doesn't correspond to the stack trace.
 
Can you please clarify what is at line 156? The code you pasted doesn't correspond to the stack trace.
line 156 is:
int enteredNum = Integer.parseInt(question.getText().toString());
As you mentioned before something there makes the problem!
 
So that means question.getText() returns null. So there must be no text in the question Textview in your UI.

But I'm still confused because that line doesn't appear in the code you show. Is it out of date? Please make the code and stack trace make sense, otherwise there's little chance of answering your question.
 
So that means question.getText() returns null. So there must be no text in the question Textview in your UI.

But I'm still confused because that line doesn't appear in the code you show. Is it out of date? Please make the code and stack trace make sense, otherwise there's little chance of answering your question.
Java code and error log updated! Hope you figure it out now. Sorry for the inconvenience
 
Well the probable cause of your problem is that you set tags on most Button views, but the ones below don't have tags

Code:
enterBtn.setOnClickListener(this);
clearBtn.setOnClickListener(this);

So when you click on the enter, or clear Button, then this line

Code:
int enteredNum = Integer.parseInt(view.getTag().toString());

Throws a null pointer exception, because view.getTag() returns null.
Your program is executing the above line, regardless of what button has been pressed. Think about where that line should be placed so that it only gets executed for the digit buttons (i.e. the ones which have tags).
Alternatively, place some defensive code around it, to first check that view.getTag() isn't null.
 
Back
Top Bottom