[英]How to fix this NullPointerException?
package blurbProject;
import java.util.Random;
import java.util.Scanner;
public class BlurbMaker
{
Random generator = new Random(); //"Random" number generator for the Whatzits
public BlurbMaker()//constructor
{
generator = null;
}//close constructor
public BlurbMaker(Random iniGenerator)//initialize constructor
{
generator = iniGenerator;
}//close initialization
private String getWhoozitYs(){
StringBuffer sb = new StringBuffer();
boolean stop = generator.nextBoolean(); //NullPointerException here
if(stop == true)
{
sb.append("y");
getWhoozitYs();
}
return sb.toString();
}
private String getWhozit()
{
StringBuffer sb = new StringBuffer();
sb.append("x");
sb.append(getWhoozitYs()); //NullPointerException here
return sb.toString();
}
private String getWhatzit()
{
StringBuffer sb = new StringBuffer();
sb.append("q");
boolean stop = generator.nextBoolean();
if(stop == true)
{
sb.append("z");
}
else
{
sb.append("d");
}
sb.append(getWhozit()); //NullPointerException here
return sb.toString();
}
private String getMultipleWhatzits()
{
StringBuffer sb = new StringBuffer();
sb.append(getWhatzit());
boolean stop = generator.nextBoolean();
if(stop == true)
{
sb.append(getWhatzit());
}
else
{
sb.append("");
}
return sb.toString();
}
public String generateBlurb()
{
StringBuffer sb = new StringBuffer();
sb.append(getWhozit());
sb.append(getMultipleWhatzits());
return sb.toString();
}
public static void main(String[] args)
{
BlurbMaker blurbmaker = new BlurbMaker();
Scanner scanner = new Scanner(System.in);
System.out.print("How many blurbs would you like? ");
int blurbNumber = scanner.nextInt();
if(blurbNumber > 0)
for(int i = 0; i < blurbNumber; i++){
System.out.println("Blurb: " + blurbmaker.generateBlurb());
//NullPointerException on line above
}
else
System.out.println("My work here is done.");
scanner.close();
}//close main
}//close class
I'm working on a project for a programming class and after attempting to run this program I'm getting NullPointerExceptions on the four lines listed in the code here. 我正在为一个编程类设计一个项目,尝试运行该程序后,我在此处代码中列出的四行上得到了NullPointerExceptions。 I know a NullPointerException comes from trying to access something that's null, but I'm not sure of how to fix it.
我知道NullPointerException来自尝试访问null的内容,但是我不确定如何解决它。
Remove this line in the no-argument constructor. 在无参数构造函数中删除此行。
generator = null;
You are "resetting" a previously initialized Random
instance and causing an NPE
to be thrown on the first attempted method call using that instance: 您正在“重置”先前初始化的
Random
实例,并使使用该实例的第一次尝试方法调用引发NPE
:
An alternative to initialize the Random
generator
might be: 初始化
Random
generator
的替代方法可能是:
private final Random generator; // no init
public BlurbMaker() {
generator = new Random();
}
public BlurbMaker(Random iniGenerator) {
generator = iniGenerator;
}
This will create an instance of Random
only when it's required. 仅在需要时,这将创建
Random
的实例。
You call the no-argument constructor to BlurbMaker(), which initializes generator to null. 您将无参数构造函数调用到BlurbMaker(),它将生成器初始化为null。 You then call a method on generator, which is null.
然后,您在生成器上调用一个方法,该方法为null。 Hence the NPE.
因此是NPE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.