-1

New to Android developing, using Android Studio. The first activity of my app is a simple main menu with a "Start" and a "Quit" button. When i press the "Start" button, the app should take me to the second activity named "EncounterScreen". Instead, the app crashes. enter image description here. I am trying to display the current health of the player object, of class Player. Apparently the problem is with "TextView healthText=findViewById(R.id.healthText);". This is the error: "Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference"

public class EncounterScreen extends AppCompatActivity implements View.OnClickListener {
  Player player=new Player();
  TextView healthText=findViewById(R.id.healthText); 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_encounter_screen);
    Player player=new Player();
    TextView healthText=findViewById(R.id.healthText);
    healthText.setText("Health "+player.getCurrentHP());

    Button attackButton=findViewById(R.id.attackBtn);
    Button drinkPotButton=findViewById(R.id.drinkPotBtn);
    Button runButton=findViewById(R.id.runBtn);
    attackButton.setOnClickListener(this);
    drinkPotButton.setOnClickListener(this);
    runButton.setOnClickListener(this);

}

@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.drinkPotBtn:
            player.drinkPotion(player);
            healthText.setText("Health "+player.getCurrentHP());
        break;
        }

    }
BenBoozle
  • 5
  • 1

2 Answers2

0

Make sure the TextView id that you are trying to find has the correct id. You are using "R.id.healthText"

Does the TextView in your activity_ecounter_screen have a different id?

-1
Player player=new Player();
  TextView healthText=findViewById(R.id.healthText); 

I believe the issue is with the above line. remove =findViewById(R.id.healthText).

  • Deleting that part worked. Did the same for the play object. Now it switches the activity without crashing. But now when i click the "drink potion" button, which is supposed to increase the value of health, and to display the change on the screen, it crashes. Now it says: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.adventuregamealpha.EncounterScreen.onClick(EncounterScreen.java:42). It refers to this line: healthText.setText("Health "+player.getCurrentHP()); – BenBoozle Aug 22 '20 at 13:45
  • Deleting the findViewById simply creates a new bug, this is not an acceptable answer – Jacob Kaddoura Aug 24 '20 at 16:45
  • I believe deleting the findViewById from the statement before onCreate method is called will not create a new BUG. @JacobKaddoura. – Charan Kumar Aug 25 '20 at 07:26
  • Deleting it is not the answer though, maybe moving it could be a solution but removing it will create issues since OP wants to call setText() on the view reference later on. @CharanKumar – Jacob Kaddoura Aug 25 '20 at 17:10