简体   繁体   中英

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. I know a NullPointerException comes from trying to access something that's null, but I'm not sure of how to fix it.

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:

An alternative to initialize the Random generator might be:

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.

You call the no-argument constructor to BlurbMaker(), which initializes generator to null. You then call a method on generator, which is null. Hence the NPE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM