尝试在空对象引用 [Android] 上调用虚拟方法“int java.util.Random.nextInt(int)" [英] Attempt to invoke virtual method 'int java.util.Random.nextInt(int)' on a null object reference [Android]

查看:45
本文介绍了尝试在空对象引用 [Android] 上调用虚拟方法“int java.util.Random.nextInt(int)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击一个按钮时,我试图使用 java 中的 rng,但每次我单击它时,程序都会崩溃并出现以下错误:

I'm trying to use the rng from java when I click a button but each time I click it the program crashes and gives me the following error:

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 值

Int value from RollProgress class

public int rollProgress = 1;

预先感谢您尝试解决/解决方案.

Thanks in advance for any attempts at solving/solutions.

推荐答案

private Random dice;

dice.nextInt(rollProgressMA); 中使用它之前,我没有看到任何初始化 dice 变量的代码.

I don't see any code that initializes the dice variable before you use it in dice.nextInt(rollProgressMA);.

只需将声明更改为:

私有随机骰子 = new Random();

这篇关于尝试在空对象引用 [Android] 上调用虚拟方法“int java.util.Random.nextInt(int)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆