0

So I just started learning Java, and I am facing this problem. My main is trying to call the method getOperator but it's giving me the [Exception in thread "main" java.lang.NullPointerException] error. How do I fix this?

    //get operator +, - or *
    public static char getOperator() 
    {
        //set operator as random to return different types
        //of operators from cases 0 to 2

        int oper =  rand.nextInt(3);// **error occurs on this line**


        switch (oper)
        {
            case 0: return '+'; //return as + if case is 0
            case 1: return '-'; //return as - if case is 1
            case 2: return '*'; //return as * if case is 2
            default: return '+'; //default will return as +
        }
    }
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305

1 Answers1

0
    int oper =  rand.nextInt(3);// **error occurs on this line**

This will almost certainly mean rand is null. You need to assign a non-null value to rand. Perhaps something like:

private static final Random rand = new SecureRandom();
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305