iYeager
iYeager

Reputation: 57

Android (Java) Enum null pointer error

I went out on a limb and tried to use an Enum in a switch case. But obviously, I'm doing something wrong.

Here's the code where I define the Enum:

public class WorkoutStopwatch extends Activity implements OnClickListener {

    private boolean timer_running = false;
    public enum CounterState {
        WORKOUT, REST
    }
    CounterState state;

and the overridden onClick method where I make use of it:

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.start_button:
            timer_running = true;
            // Make call to timer update class to start
            break;
        case R.id.stop_button:
            timer_running = false;
            // Make call to timer update class to stop
            break;
        case R.id.reset_button:

            if (!timer_running) {
                switch(state) {
                case WORKOUT:
                    // TODO: Reset the counter to the workout time
                    break;
                case REST:
                    // TODO: Reset the counter to the rest time
                    break;
                }
            }
            break;
        }
    }

When I run my program the onClick calls for the Start and Stop buttons work just fine, but the Reset button causes the application to crash with "FATAL EXCEPTION: main" and "java.lang.NullPointerException" error directed at the line of the switch. From what I've been reading it seems likely that the value of the enum is null when reading the switch, so I changed my enum declaration to 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 like a more complete definition, but still seeing problems. Leaving the code like this didn't fix the issue, it still seems that the state was null when read. Adding an int to the declaration of the object like so:

CounterState state(0);

Gives me an Eclipse error of "Syntax error on token "0", delete this token". So I'm a little bit lost. From what I can tell there's now a constructor for the enum, which I should be able to pass an int to to initialize it's value on creation, but that doesn't seem to work.

Upvotes: 3

Views: 2806

Answers (3)

Seda T.
Seda T.

Reputation: 161

Probably your error cause is :
"v.getId()" is null.

Upvotes: 0

micha
micha

Reputation: 49552

Have you tried initializing your state variable before using it?

CounterState state = CounterState.UNKNOWN;

Upvotes: 4

user1287975
user1287975

Reputation:

null pointer exception comes only when you use the variable before or not initialize it. So first check all variables which you used here and its initialization step. do it yourself, so in future you will never do this kind of mistake.

Upvotes: 0

Related Questions