来自填充数组列表的随机索引的NullPointerException?

时间:2014-05-29 21:45:28

标签: java arraylist nullpointerexception

我试图从ArrayList中提取一个随机字符串。我确保在拉出它之前填充它,但它仍然像空的一样。我甚至在填充之后打印出列表,它确实填充了,让我相信问题必须在我的代码中的其他地方。有什么想法吗?

哦,是的,我的代码很有趣。我正在创建一个控制台游戏,以获得在我办公室工作的粗鲁家伙的乐趣。 (不妨让练习变得有趣,对吗?)享受。 :P

这是对象类

import java.util.ArrayList;
import java.util.Random;

public class Meg {

    private String name = "Meg";
    private int weight = 300;
    private int annoyanceLevel = 1000;

    static ArrayList<String> randomBoringTopics = new ArrayList<String>();

    public static void populateRandomBoringTopics(){
    randomBoringTopics.add("women's underwear");
    randomBoringTopics.add("men with backhair");
    randomBoringTopics.add("tucked in shirts");
    randomBoringTopics.add("shaving at one's desk");
    randomBoringTopics.add("pot bellies");
    }

    public void Fart(){
        System.out.println("Meg let's out a huge fart with zero consideration for who hears it");
    }

    public static void startBoringConversation(){
        Random randomGenerator = null;
        int index = randomGenerator.nextInt(randomBoringTopics.size());
        String randomBoringTopic = randomBoringTopics.get(index);
        System.out.println("Meg begins to talk to you about his thoughts regarding " + randomBoringTopic);  
    }

    public void creep(){
    System.out.println("Meg suddenly appears behind you, breathing heavily and rubbing his stomach while fingering his belly button");  
    }

    public void interject(){
    System.out.print("Meg hears you talking and butts into your conversation, making it instantly boring"); 
    }

    public void disappear(){
    System.out.print("Meg leaves his desk and is gone for an extended period of time, relieving those in his row but "
    + "tormenting the rest of the office");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getAnnoyanceLevel() {
        return annoyanceLevel;
    }

    public void setAnnoyanceLevel(int annoyanceLevel) {
        this.annoyanceLevel = annoyanceLevel;
    }

}

这是我的主要......

public class Main {

    public static void main(String args[]){

        Meg.populateRandomBoringTopics();
        Meg.startBoringConversation();
    }
}

4 个答案:

答案 0 :(得分:4)

你在这里做的事情肯定会抛出NPE:

public static void startBoringConversation(){
    Random randomGenerator = null;
    // randomGenerator can only be null at this location
    int index = randomGenerator.nextInt(randomBoringTopics.size());
    String randomBoringTopic = randomBoringTopics.get(index);
    System.out.println("Meg begins to talk to you about his thoughts regarding " + randomBoringTopic);  
}

您需要实际创建一个Random对象,以便从以下位置获取随机数:

public static void startBoringConversation(){
    Random randomGenerator = new Random();
    int index = randomGenerator.nextInt(randomBoringTopics.size());
    String randomBoringTopic = randomBoringTopics.get(index);
    System.out.println("Meg begins to talk to you about his thoughts regarding " + randomBoringTopic);  
}

答案 1 :(得分:2)

Random randomGenerator = null;
int index = randomGenerator.nextInt(randomBoringTopics.size());

我当然不知道你还期待在这里发生什么......

randomGeneratornull

就像任何其他对象一样,您必须在使用它之前对其进行实例化:

Random randomGenerator = new Random();
int index = randomGenerator.nextInt(randomBoringTopics.size());

答案 2 :(得分:1)

以下是解决问题的方法。

    Random randomGenerator = new Random();

答案 3 :(得分:1)

除了奇怪的程序,认为你必须实例化随机...

Random randomGenerator = new Random();

与你拥有的......

Random randomGenerator = null;