mercredi 27 mai 2015

Initialize Random Object on constructor? [duplicate]

This question already has an answer here:

I am writing a class which has as field a Random object. In my constructor, I am trying to initialize my Random Object but, when I try to run, it gives Null Pointer Exception. Here is how I wrote this piece of code:

public class Class1 {
Random randNum;

public Class1 (long seed){
    randNum = new Random(seed);
    // rest of code
}

What is wrong with this approach? What is happening is when I call the method that needs this randNum, it gives null pointer!

Thanks!

@Edit: Also, I've tried to initialize the variable and only set the seed on the constructor. Didn't work.




Aucun commentaire:

Enregistrer un commentaire