-1

I have an issue I need help with. I am running into a NullPointerException no matter what I do with this app, and at this point it is seriously holding up this project. I have reviewed the logcat multiple times, made changes and re run the app and every time it turns out a nullpointerexception. For the life of me I can't seem to figure out what is causing it. Can someone point out where it is, and if possible, why it occurred since as far as I can tell I have everything assigned a value.

GameStart.java:

package com.saphiric.simproject;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Saphiric on 11/12/14.
 */
public class GameStart extends Activity {

    /**
     * Necessary activity variables
     */
    int clickCount = 0;
    TextView storyText;
    Button advanceGame;
    DecisionFragment decisionFragment;
    ControlsFragment controlsFragment;

    public View.OnClickListener decisionTime = new View.OnClickListener(){
        public void onClick(View v) {
            clickCount = clickCount + 1; // Increments clickCount by 1 per click

//            /**
//             * Will handle checking how far into the story the player is
//             * and update components as necessary.
//             */
//            switch (clickCount) {
//                case 1:
//                    storyText.setText(R.string.story_opening_two);
//                    break;
//
//                case 2:
//                    FragmentTransaction decisionTime = fragmentManager.beginTransaction();
//                    // Replace the fragment currently inside of the fragmentContainer
//                    decisionTime.replace(R.id.fragmentContainer, decisionFragment);
//                    decisionTime.commit();
//                    break;
//
//                default:
//                    System.out.println("An error occurred");
//            }
//            System.out.println(clickCount);
        }
    };

    /**
     * Android activity methods
     * @param savedInstanceState is the apps savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_start);

        // UI element handles
        advanceGame = (Button) findViewById(R.id.advanceButton);
        storyText = (TextView) findViewById(R.id.storyText);

        decisionFragment = new DecisionFragment();
        controlsFragment = new ControlsFragment();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        // UI will add the ControlsFragment in it's starting state for that activity.
        fragmentTransaction.add(R.id.fragmentContainer, controlsFragment).commit();

        // Sets the advanceButton to run decisionTime method
        advanceGame.setOnClickListener(decisionTime);
    }
}

LogCat:

12-07 13:16:10.142  19915-19915/com.saphiric.simproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.saphiric.simproject, PID: 19915
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.saphiric.simproject/com.saphiric.simproject.GameStart}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            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:1069)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.saphiric.simproject.GameStart.onCreate(GameStart.java:75)
            at android.app.Activity.performCreate(Activity.java:5312)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            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:1069)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
            at dalvik.system.NativeStart.main(Native Method)
Saphiric
  • 81
  • 1
  • 8

4 Answers4

3

The null pointer happens at line 75 , i.e.

advanceGame.setOnClickListener(decisionTime);

Here, the only thing that can be null is advanceGame. This means that

advanceGame = (Button) findViewById(R.id.advanceButton);

is not able to find your button from your view. Check the id of the button if its exactly the same as you defined it in your android xml.

Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • So if the id is assigned in the One.xml, and the layout for Activity2 is activity2.xml, then the id wont be found for the activity, resulting in the NullPointer, am I understanding that right? – Saphiric Dec 07 '14 at 18:28
  • Problem solved thank you. It led to another issue but I'll handle that separately. Much appreciated – Saphiric Dec 07 '14 at 19:42
1

I believe that advanceGame is null.

You can't find button by findViewById(R.id.advanceButton);

DontRelaX
  • 744
  • 4
  • 10
0

NPE is at line 75

advanceGame.setOnClickListener(decisionTime);

it is clear from

at com.saphiric.simproject.GameStart.onCreate(GameStart.java:75)

Looks like at

advanceGame = (Button) findViewById(R.id.advanceButton);

view was not found. You can debug that or add check at 75

if(null != advanceGame) advanceGame = (Button) findViewById(R.id.advanceButton);
gio
  • 4,950
  • 3
  • 32
  • 46
0

I think just check @+id of your activity_game_start.xml layout of advanceGame button it might have different name

sandy
  • 203
  • 1
  • 7