3

I use if(pause == null) to do something when pause is null. But I get the error

the operator == is undefined for the argument type(s) long,null

Here is the code,

public class Timer extends CountDownTimer {
    long pause = (Long) null;

    public Timer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        content.setText("Tijd over: " + millisUntilFinished / 100);
    }

    public void onPause(long millisUntilFinished) {
        if(pause == null) {
            pause = millisUntilFinished;
            content.setText("Tijd over: " + millisUntilFinished / 100);
            this.cancel();
        }
        else {
            this.start();   
        }
    }

    @Override
    public void onFinish() {
        content.setText("Tijd is op!");
    }
}

This class isn't finished, so ignore the rest of the code.

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
Wietse de Vries
  • 695
  • 1
  • 5
  • 18

6 Answers6

7
long pause = (Long) null;

should be

Long pause = null;
^

long is the primitive type but Long is the wrapper object of type long.

You may use a sentinel value instead of wrapping it as object.

long pause = -1;
...
if(pause == -1 ) {
   //do something
}
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
  • 1
    As extra comment on _long pause = (Long) null;_, running it would be fun. Due to auto-unboxing you'd get a NullPointerException right there :) – extraneon Oct 29 '11 at 09:43
4

long with a lowecase 'l' is a primitive type in java, and you can not store null in it. Null can be stored only in references to classes. replace your long with Long, and it will work, because Long is a wrapper for the primitive, actually storing a reference, which can be null.

Please read: http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

EliK
  • 103
  • 1
  • 6
4

long is primitive. It cannot be null. Only object references can be. If you really want to use null value for numbers change the type from 'long' to Long.

AlexR
  • 114,158
  • 16
  • 130
  • 208
3

The variable pause is a long, so pause can never be null, and the compiler is confused. Either make it a Long, or use some in-band indicator of invalidity.

Dave
  • 10,964
  • 3
  • 32
  • 54
1

long is a primitive type, it is not a class type and therefore pause is not a "reference" to an object, which means it cannot be null. If you want to be able to tell when the timer is paused, add a flag variable; a boolean.

You could change long to Long but that seems unnecessary, your code could be something like this and utilise a boolean:

public class Timer extends CountDownTimer {
    boolean isPaused = true;
    long pause = 0; // not sure what this is meant to store

    public Timer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        content.setText("Tijd over: " + millisUntilFinished / 100);
    }

    public void onPause(long millisUntilFinished) {
        if(!isPaused) {
            isPaused = true;
            pause = millisUntilFinished; // don't know what this is for
            content.setText("Tijd over: " + millisUntilFinished / 100);
            this.cancel();
            }
        else {
            this.start();
            isPaused = false;
        }
    }

    @Override
    public void onFinish() {
        content.setText("Tijd is op!");
    }
}
AusCBloke
  • 18,014
  • 6
  • 40
  • 44
1

The java language doesn't support the notion of nullable version of primitive types as C# does (see this question on How to present the nullable primitive type int in Java?). So, you'll have to use the Long class wrapper instead of the long primitive type.

public class Timer extends CountDownTimer {
    Long pause = null;

    // Rest of the code ...
}

This will have a cost because the java runtime will have to box and unbox your value every time you access or update it. Depending on the usage of your code, this may degrade the performance of your application. This will need profiling.

Another option is to use a value that is "impossible" to represent an uninitialized pause field. If you know that pause will always be positive, then you can use -1 to represent an uninitialized field.

Community
  • 1
  • 1
Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85