0

I am trying to pull a random string from an ArrayList. I make sure to populate it before pulling from it yet it still acts as if it empty. I even printed the list out after populating it and it is indeed populated, leaving me to believe the problem must be elsewhere in my code. Any ideas?

Oh, and yes, my code is hilarious. I'm creating a console game for fun about a gross dude who works in my office. (Might as well make practice fun, right?) Enjoy. :P

here is the object class

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;
    }

}

And here is my main...

public class Main {

    public static void main(String args[]){

        Meg.populateRandomBoringTopics();
        Meg.startBoringConversation();
    }
}
Stevo
  • 85
  • 3
  • 11

4 Answers4

4

What you are doing here is guaranteed to throw a 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);  
}

You need to actually create a Random object to get your random numbers from:

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);  
}
azurefrog
  • 10,785
  • 7
  • 42
  • 56
2
Random randomGenerator = null;
int index = randomGenerator.nextInt(randomBoringTopics.size());

I sure don't know what else you were expecting to happen here...

(randomGenerator is null)

Just like for any other object, you have to instantiate it before you use it:

Random randomGenerator = new Random();
int index = randomGenerator.nextInt(randomBoringTopics.size());
awksp
  • 11,764
  • 4
  • 37
  • 44
1

Here is how you fix it.

    Random randomGenerator = new Random();
merlin2011
  • 71,677
  • 44
  • 195
  • 329
1

Strange program aside, think you have to instantiate Random...

Random randomGenerator = new Random();

versus what you have...

Random randomGenerator = null;
This 0ne Pr0grammer
  • 2,632
  • 14
  • 57
  • 81