jeudi 22 février 2018

Picking Random number gives NullPointerException

I am trying to get a random integer to use as index so i can get a random element from a string array. I have a function that does it,checked online and its similar to what other people are using. What am I doing wrong and I'm getting a NullPointerException?

This is the function I am using:

    public static String getRandom(String[] array) {
    int rnd = new Random().nextInt(array.length);
    return array[rnd];
    }

I am getting the Null pointer exception on the 2nd line. An example of one of the Strings is:

       String[] president = {"Jefferson","Johnson","Filmore","Obama","Trump"};




Aucun commentaire:

Enregistrer un commentaire