Android (Java) Enum null pointer error -


i went out on limb , tried use enum in switch case. obviously, i'm doing wrong.

here's code define enum:

public class workoutstopwatch extends activity implements onclicklistener {      private boolean timer_running = false;     public enum counterstate {         workout, rest     }     counterstate state; 

and overridden onclick method make use of it:

    @override     public void onclick(view v) {         switch(v.getid()) {         case r.id.start_button:             timer_running = true;             // make call timer update class start             break;         case r.id.stop_button:             timer_running = false;             // make call timer update class stop             break;         case r.id.reset_button:              if (!timer_running) {                 switch(state) {                 case workout:                     // todo: reset counter workout time                     break;                 case rest:                     // todo: reset counter rest time                     break;                 }             }             break;         }     } 

when run program onclick calls start , stop buttons work fine, reset button causes application crash "fatal exception: main" , "java.lang.nullpointerexception" error directed @ line of switch. i've been reading seems value of enum null when reading switch, changed enum declaration this:

    private enum counterstate {         unknown(0),         workout(1),          rest(2);          private int statevalue;          counterstate(int state) {             this.statevalue = state;         }          public int getstatevalue() {             return statevalue;         }          public void setstatevalue(int state) {             this.statevalue = state;         }            }      counterstate state; 

which i'll agree, looks more complete definition, still seeing problems. leaving code didn't fix issue, still seems state null when read. adding int declaration of object so:

counterstate state(0); 

gives me eclipse error of "syntax error on token "0", delete token". i'm little bit lost. can tell there's constructor enum, should able pass int to initialize it's value on creation, doesn't seem work.

have tried initializing state variable before using it?

counterstate state = counterstate.unknown; 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -