0

I wrote this code that keeps triggering this Exception. code:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

enum mathoperators{
MULTIPLY, DIVIDE, ADD, SUBTACT, EQUALS
}

public class binarydecimal {

    static boolean state; 
    static List<mathoperators> mathpos = new ArrayList<mathoperators>();
    
    @SuppressWarnings("null")
    public static void main(String[] args) throws InterruptedException {
        Random r = new Random();
        int intman = r.nextInt(5);
        
        String nani = null;
        
        
        switch (intman + 1) {
        case  1:
            state = true;
            mathpos.add(mathoperators.MULTIPLY);
            System.out.println(intman * 2/420);
            TimeUnit.SECONDS.sleep(2);
        case 2: 
            state = false;
            mathpos.add(mathoperators.DIVIDE);
            System.out.println(intman/2 * 420);
            TimeUnit.SECONDS.sleep(2);
        case 3:
            state=true;
            mathpos.add(mathoperators.ADD);
            System.out.println(intman + 2/420);
            TimeUnit.SECONDS.sleep(2);
        case 4:
            if (intman < 2) {
                intman+=2;
                
            }
            state=false;
            System.out.println(intman - 2/420);
            TimeUnit.SECONDS.sleep(2);
        default:
            state = (Boolean) null;
            mathpos.clear();                    
        }
        
        
    }}
        
        

error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()" because "null" is null

How can this code function the same just without triggering that exception.

jhamon
  • 3,603
  • 4
  • 26
  • 37

1 Answers1

1

Just change the primitive

static boolean state; 

to

static Boolean state; 

primitives can't be null.

Incidentally, you may want to add a break; to the end of each of your case blocks