当我点击按钮时,我正在尝试使用java中的rng,但每次单击它时程序崩溃并给我以下错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.Random.nextInt(int)' on a null object reference
at me.test.first.MainActivity.onGenPress(MainActivity.java:25)
按键按下方法
public void onGenPress(View v){
TextView tv = (TextView) findViewById(R.string.copper);
int number = 1 + dice.nextInt(rollProgressMA);
co.copper += number;
tv.setText("Copper: " + co.copper + " + " + number);
}
课程中涉及的对象
CoinTracker co = new CoinTracker();
RollProgress rpo = new RollProgress();
private Random dice;
private int rollProgressMA = rpo.rollProgress;
RollProgress类的Int值
public int rollProgress = 1;
提前感谢任何解决/解决方案的尝试。
答案 0 :(得分:7)
private Random dice;
在dice
中使用dice.nextInt(rollProgressMA);
变量之前,我没有看到任何初始化private Random dice = new Random();
变量的代码。
只需将声明更改为:
{{1}}