-1

My problem is that one specific activity in my app can't be launched for some reason, and it throws:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

All other launching code that looks like this work fine, the problem is ONLY when I'm launching Player1. The class surely exists, and I can't understand what is the problem.

I'm launching an activity called Player1 from an activity called ChooseLevel. When some button is pressed, the call is:

Intent intent = new Intent(ChooseLevel.this, Player1.class);
Bundle b = new Bundle();
b.putInt("game_level", 1);
intent.putExtras(b);
startActivity(intent);
finish();

The logs show that the error is in: Player1.onCreate(Player1.java:50)

The code in line Player1:50 is:

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

    changeIndex = 0;
    cards = new ArrayList<Card>(8);
    score = (TextView) findViewById(R.id.score_text);
    message = (TextView) findViewById(R.id.msg_text);
    timer = (TextView) findViewById(R.id.timer);
    timer.setText(String.valueOf(definitions.TIMER_START)); # LINE 50

    Bundle b = getIntent().getExtras();

    ... 
}

When this launching code runs, I get this in my logs:

33:04.338 18302-18302/com... D/AndroidRuntime: Shutting down VM
01-18 10:33:04.338 18302-18302/com... E/AndroidRuntime: FATAL EXCEPTION: main
           Process: com..., PID: 18302
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.../com....Player1}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
        at com....Player1.onCreate(Player1.java:50)




EDIT:

I found my answer, it had to do with some object that I had in my Player1.onCreate() method. In a wierd way, android-studio raised the exception regarding getClass(). If someone knows why this happend, please share :)

Ofer Arial
  • 1,129
  • 1
  • 10
  • 25
  • 1
    `Player1.onCreate(Player1.java:50)` - That number is the line number where the Exception happens. Look on line 50. Btw, if you'd gone through the answers in the suggested duplicate, you would know that. – Mike M. Jan 18 '17 at 08:53
  • 1
    You're not showing the line throwing the exception. – shmosel Jan 18 '17 at 08:54
  • Check Your Manifest file , have you declare Player1 in Manifest ? – h_patel Jan 18 '17 at 08:55
  • The line throwing the exception is showed, I copied the beginning of the onCreate function. @h_patel Yes, I checked it now. – Ofer Arial Jan 18 '17 at 08:58
  • @GhostCat Please explain why it is a duplicate, I can't understand why and I don't get my answer from the regular answer. – Ofer Arial Jan 18 '17 at 09:01
  • @OferArial : just make sure that you timer is addressing to the right id in right layout and then if so, try to clean your project and try once again. – Amit Upadhyay Jan 18 '17 at 09:06

1 Answers1

0

use context,getApplicationContext()orgetActivity()`

Intent intent = new Intent(context, Player1.class);
Bundle b = new Bundle();
b.putInt("game_level", 1);
intent.putExtras(b);
startActivity(intent);
finish();
Ashish Gupta
  • 737
  • 11
  • 18