0

Hello this is my 1st app I made on android and I finished my app with no issues but suddenly I have this error and I have no clue why it is not working because it was working fine earlier. I do not understand how to read the log cat so please help me out!

07-24 12:00:15.170: D/AndroidRuntime(1866): Shutting down VM
07-24 12:00:15.170: W/dalvikvm(1866): threadid=1: thread exiting with uncaught exception (group=0xb1cefb20)
07-24 12:00:15.190: E/AndroidRuntime(1866): FATAL EXCEPTION: main
07-24 12:00:15.190: E/AndroidRuntime(1866): Process: com.sachinda.myfirstapp, PID: 1866
07-24 12:00:15.190: E/AndroidRuntime(1866): java.lang.NullPointerException
07-24 12:00:15.190: E/AndroidRuntime(1866):     at com.sachinda.myfirstapp.TwoPlayerActivity.onClick(TwoPlayerActivity.java:157)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at android.view.View.performClick(View.java:4438)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at android.view.View$PerformClick.run(View.java:18422)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at android.os.Handler.handleCallback(Handler.java:733)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at android.os.Looper.loop(Looper.java:136)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at java.lang.reflect.Method.invokeNative(Native Method)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at java.lang.reflect.Method.invoke(Method.java:515)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 12:00:15.190: E/AndroidRuntime(1866):     at dalvik.system.NativeStart.main(Native Method)
07-24 12:00:21.151: I/Process(1866): Sending signal. PID: 1866 SIG: 9

Edit: here is my code and commented on top of line 157

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.empty_button:

        if (one.isEnabled()) {
            oneChk = true;
            if (counter == 0) {
                v.setBackgroundResource(R.drawable.redsquarecircle);
                counter++;
                v.setTag(circle);
                if (v.getTag().equals(two.getTag())
                        && v.getTag().equals(three.getTag())) {
                    p1Wins.startAnimation(p1winner);
                    delayingDialog();
                } else if (v.getTag().equals(four.getTag())
                        && v.getTag().equals(seven.getTag())) {
                    p1Wins.startAnimation(p1winner);
                    delayingDialog();
                } else if (v.getTag().equals(five.getTag())
                        && v.getTag().equals(nine.getTag())) {
                    p1Wins.startAnimation(p1winner);
                    delayingDialog();
                } else {
                    if (!oneChk || !twoChk || !threeChk || !fourChk
                            || !fiveChk || !sixChk || !sevenChk
                            || !eightChk || !nineChk) {
                        //line 157
                        p2.startAnimation(p2turn);
                    } else {
                        draw.startAnimation(itsdraw);
                        delayingDialog();
                    }

                }
4
  • So what's happening at line 157 of TwoPlayerActivity.java? Since you've shown no code, we can't really help you.
    – Marc B
    Jul 24, 2014 at 16:06
  • java.lang.NullPointerException 07-24 12:00:15.190: E/AndroidRuntime(1866): at com.sachinda.myfirstapp.TwoPlayerActivity.onClick(TwoPlayerActivity.java:157) is the line you should be looking at
    – tyczj
    Jul 24, 2014 at 16:07
  • basically it is telling you that in TwoPlayerActivity.java on line 157 there is a null object you are trying to reference
    – erik
    Jul 24, 2014 at 16:12
  • 1
    We need to know what line 157 is, but an obvious candidate would be that you are (sometimes) getting to this code at a point in time when the apparently global "one" object is not initialized. Jul 24, 2014 at 16:14

1 Answer 1

0

Log Cat files are a bit tricky to follow but keep at it and you'll gradually learn what to look for.

First the type of error should normally be quite obvious:

java.lang.NullPointerException

This is telling you it has tried to reference some variable or view or whatever and it's null.

Next you'll need to scroll through the log cat and look for the line number that references your file. The com.sachinda.... is the dead give away:

com.sachinda.myfirstapp.TwoPlayerActivity.onClick(TwoPlayerActivity.java:157)

You can double click this line and it should open your file. Anyway looking at that line will hopefully shed some light on the problem.

Looking at your edit, where are p2 and p2turn defined? You should make sure they are member variables and not defined inside another method:

int mp2 = 42; //Member vairable, available to all methods in class 

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

    int p2 = 42; // private variable, only available to onCreate
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.