Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The operator is undefined

I just tried to make a simple class that lets me figure out the length of a file:

public class Size {

    long s = 0;
    int a;

    public static void main(String[]args){
        new Size();
    }

    Size(){

        try{
        FileInputStream str = new FileInputStream("E:/Eclipse/Resources/smile.jpg");

        while(a != null){
            s++;
        }
        }catch (IOException e){
            e.printStackTrace();
        }

    }


}

I run into a problem with

while(a != null)

I get the Error:

The operator != is undefined for the argument type(s) int, null

Any ideas why it's blocking the condition?

like image 760
Syntic Avatar asked Apr 20 '13 19:04

Syntic


2 Answers

Primitive types in Java cannot be null. If you want to check for 0, do a != 0.

like image 188
sonOfRa Avatar answered Nov 11 '22 02:11

sonOfRa


Put a into an Integer object, which can be compared to null:

Integer value = new Integer(a);

while (value != null)
{
    // Do stuff
}
like image 45
syb0rg Avatar answered Nov 11 '22 02:11

syb0rg