Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum NullPointerException

Could anyone explain why

public class Testabut{
    enum ThreeColors{RED, BLUE, green;
        public void woowoo(){
        System.out.println("woo");}
    }
     ThreeColors color;



    class Innerclass{
        Innerclass(){
             color.woowoo();
        }
    }

generates a null pointer exception at the invocation of woowoo() ? The instance of color should be reachable, no?

like image 621
Stumbler Avatar asked Dec 02 '25 01:12

Stumbler


2 Answers

Because color is not initialized and it's default value is null.
Initialize it like

ThreeColors color = ThreeColors.RED;  //Or any other value
like image 172
Ajinkya Avatar answered Dec 03 '25 13:12

Ajinkya


Your color variable is null. You have to initialize it to use it.

like image 28
Kevin Workman Avatar answered Dec 03 '25 15:12

Kevin Workman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!