The operator is undefined

穿精又带淫゛_ 提交于 2019-12-21 04:57:06

问题


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?


回答1:


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




回答2:


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

Integer value = new Integer(a);

while (value != null)
{
    // Do stuff
}



回答3:


you should assign (or) check NULL values only for a pointer. For integer values, initialise it to any number ,like a flag and check for that condition. NULL can be assigned or checked only with a pointer variable



来源:https://stackoverflow.com/questions/16124228/the-operator-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!