-1

I am following along with the book Learning Java by Building Android Games by John Horton and for its first example, the Math Game, I am getting the following error when ran.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Here is my code:

Game Activity

package com.example.mike.mathgame;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class GameActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Loads up UI
        setContentView(R.layout.activity_main);

        int partA = 2;
        int partB = 3;
        int correctAnswer = partA * partB;
        int wrongAnswer1 = correctAnswer - 1;
        int wrongAnswer2 = correctAnswer + 1;

        TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
        TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
        Button buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
        Button buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
        Button buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);

        textObjectPartA.setText("" + partA);
        textObjectPartB.setText("" + partB);

        buttonObjectChoice1.setText("" + correctAnswer);
        buttonObjectChoice2.setText("" + wrongAnswer1);
        buttonObjectChoice3.setText("" + wrongAnswer2);



    }
}

Main Activity

package com.example.mike.mathgame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(this);
    }

    @Override
    public void onClick(View view){
        Intent i;
        i = new Intent(this, GameActivity.class);
        startActivity(i);
    }
}

Content_game

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.mike.mathgame.GameActivity"
tools:showIn="@layout/activity_game">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="2"
    android:id="@+id/textPartA"
    android:layout_marginTop="83dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="x"
    android:id="@+id/textOperator"
    android:layout_alignTop="@+id/textPartA"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="2"
    android:id="@+id/textPartB"
    android:layout_alignTop="@+id/textOperator"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="="
    android:id="@+id/textView2"
    android:layout_marginTop="64dp"
    android:layout_below="@+id/textOperator"
    android:layout_alignLeft="@+id/textOperator"
    android:layout_alignStart="@+id/textOperator" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="4"
    android:id="@+id/buttonChoice1"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="5"
    android:id="@+id/buttonChoice2"
    android:layout_below="@+id/buttonChoice1"
    android:layout_alignLeft="@+id/buttonChoice1"
    android:layout_alignStart="@+id/buttonChoice1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="6"
    android:id="@+id/buttonChoice3"
    android:layout_below="@+id/buttonChoice2"
    android:layout_centerHorizontal="true" />

activity_game

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_game" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

Risen
  • 19
  • 2

2 Answers2

0

At least one of your views is not found when you are using findViewById()in GameActivity. Check for nulls for each of them.

One thing that pops when looking at your code is that this line in GameActivity is probably wrong: setContentView(R.layout.activity_main);

Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
0

Although you have fixed the problem. You said you can't debug it. So you probably have little experience of debugging. Basically this consists of adding code at points and checking that the code was doing what you wished.

I'm not sure about Eclipse, but certainly Android Studio makes this extremely simple. However, first how you could have done this without.

Obviously Main Activity is called first and when called the onCreate method will be called. Starting from that information, coding a visible (log wise) message could be added. So we add some code as soon as the Main activity starts, that is immediately after the onCreate statement, as per :-

    protected void onCreate(Bundle savedInstanceState) {
        Log.e("MYGAME","B4 super.onCcreate reached OK.");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(this);
    }

If you run the App, then you would get something along the lines of :-

05-26 16:29:04.250 1828-1828/mike092015.app001 E/MYGAME: B4 super.onCcreate reached OK.

in the log, if that line of code where reached. If you didn't then you know that the program failed before that line. That's the core principle of debugging.

Android Studio makes doing this much easier. You can simply insert a break point by clicking in the column to the left of the code, where the line numbers are. Not on the Line Number but between it and the code but within the column. A red circle will appear.

Instead of running the app using the green arrow run it by click the little green bug (next to the Run arrow) (Run / Debug App or Shift F9). The program will be halted before that line of code executes and the debug window will contain information such as the current state of variables. You also have options to control how you progress through the code. Of course if the breakpoint isn't reached then it's back to rethinking you debug strategy.

MikeT
  • 51,415
  • 16
  • 49
  • 68