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?
Because color is not initialized and it's default value is null.
Initialize it like
ThreeColors color = ThreeColors.RED; //Or any other value
Your color variable is null. You have to initialize it to use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With