1

I use Android studio and I only know basic java. I know I need to declare the Widgets on Class to use it in another function but it still gives nullreference error.

package com.uruskan.shock.kultureventbase;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {
TextView tv;
Button bYemek, bSleep, bAttack;
int sayac = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.event_date);
    Button bYemek = (Button) findViewById(R.id.button); //yemek ye
    Button bSleep = (Button) findViewById(R.id.button2); //uyu
    Button bAttack = (Button) findViewById(R.id.attack); // saldır
    bYemek.setOnClickListener(this);
    bSleep.setOnClickListener(this);
    bAttack.setOnClickListener(this);
    Karakter k = new Karakter();
    k.hareketSayisi = 10;
    k.kilo = 10;
    k.saldiriGücü = 100;

}

@Override
public void onClick(View v) {
    if (v.getId() == bAttack.getId()) {
        //Attack Code
        tv.setText("Attack ");
    } else if (v.getId() == bSleep.getId()) {
        //sleep code
        tv.setText("Sleeping Rrrrrr !");
    } else if (v.getId() == bYemek.getId()) {
        //eat code
        tv.setText("Narcissus get her, for a dinner..");
    }
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Blasanka Oct 01 '17 at 10:19
  • Remove `Button` from `Button bAttack` with your code you defined a local variable which shadows the field and your field remains null.(the same applies to `bSleep` and `bYemek`) – Oleg Oct 01 '17 at 10:19

2 Answers2

0

Update your code:

bYemek = (Button) findViewById(R.id.button); //yemek ye
bSleep = (Button) findViewById(R.id.button2); //uyu
bAttack = (Button) findViewById(R.id.attack); // saldır
Mukesh M
  • 2,242
  • 6
  • 28
  • 41
0

You're initializing your Button variables locally which is out of scope when they are being referenced from the onClick function and this is why you're getting the NullPointerException.

So as you've declared the buttons as public variables, you need to assign the variables like this.

bYemek = (Button) findViewById(R.id.button); //yemek ye
bSleep = (Button) findViewById(R.id.button2); //uyu
bAttack = (Button) findViewById(R.id.attack); // saldır

And in the onClick function, you can simply do this.

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.button:
            tv.setText("Narcissus get her, for a dinner..");
            return;
        case R.id.button2:
            tv.setText("Sleeping Rrrrrr !");
            return;
        case R.id.attack:
            tv.setText("Attack ");
            return;
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98