0

I tried to do something extremely simple or I thought it was.

In my BluetoothChat, I set

public static boolean potato = false;

In the onCreateBundle of my MainActivity, I have

if(BluetoothChat.potato == false)
{
  tv.setText("it did not crash");
}

My application crashes immediately upon me switching windows from bluetoothchat to mainactivity. I receive java.lang.RuntimeException and java.lang.NullPointerException. However, the application does not crash and switches perfectly if I have the if statement commented out.

4
  • The reason for this is that while the program control enters into onCreate() method, still your BluetoothChat class is Null. Hence it gives NPE. Commented Apr 7, 2014 at 3:41
  • How would I fix that? Commented Apr 7, 2014 at 3:42
  • May be by calling a public constructor before your if condition. Commented Apr 7, 2014 at 3:44
  • give us complete stacktrace.... Commented Apr 7, 2014 at 3:46

1 Answer 1

1

It looks like the error is coming from tv, not BlueToothChat.potato. Have you initialized the "tv" variable at that point in the code?

Sign up to request clarification or add additional context in comments.

2 Comments

I feel really stupid right now. That worked. Thank you.
We all make this mistake once in a while! Whenever you get a NullPointerException, check for uninitialized variables; that's more often than not the problem.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.