简体   繁体   English

null 检查语句是否在 java 中不起作用

[英]null check if statement isn't working in java

i want to check a instanciate object is null in java when i press enter, i have a class with a constructor and i pass a parameter我想检查 object 是 java 中的 null 的实例化 object 是 java 当我按下回车键时,我有一个 ZA2F2 构造函数和一个构造函数和一个参数 AB6C1DC

private String codigo;
    private String nombre;
    private String descripcion;
    private String foto;
    private String stock;

public Producto(String codigo) {
   this.codigo = codigo;
        this.nombre = nombre;
        this.descripcion = descripcion;
}
/*then getters and setters


*/


//this is in a java form 
private void txtCodigoProductoActionPerformed(java.awt.event.ActionEvent evt) { 
Producto producto = new Producto(txtCodigoProducto.getText());
System.out.println(producto);


if (producto==null) {
            JOptionPane.showMessageDialog(this, "No existe este producto");

        }else{
            lbl1.setText(producto.getNombre());
            lbl2.setText(producto.getDescripcion());
         }

}

System.out.println(producto) shows null when i press enter since i wrote an incorrect number so all the attributes of producto are null but then why it doesn't check if is null and doesn't show me the the message dialog? System.out.println(producto) 显示 null 当我按 Enter 时,因为我写了一个不正确的数字,所以 producto 的所有属性都是 null 但是为什么它不检查 null 是否显示对话框? I change the condition to one of the attributes since all of them are null:我将条件更改为属性之一,因为它们都是 null:

if (producto.getStock()==null) {
            JOptionPane.showMessageDialog(this, "No existe este producto");

        }else{
            lbl1.setText(producto.getNombre());
            lbl2.setText(producto.getDescripcion());
         }
}

and it worked and showed me the messageDialog, so why producto == null doesn't work?它工作并向我展示了 messageDialog,那么为什么 producto == null 不起作用?

In Java, a variable of object type, such as producto in your example, can either refer to an object, or be null.在 Java 中,object 类型的变量(例如您的示例中的producto )可以引用 object,也可以是 Z20B9A6259CC0C8DDZA7。 If you set it to an object, like you did when you wrote producto = new Producto(txtCodigoProducto.getText());如果您将其设置为 object,就像您在编写producto = new Producto(txtCodigoProducto.getText());时所做的那样then it's not null any more.那么它不再是 null 了。

Inside an object there can be other variables, called fields.在 object 内部可以有其他变量,称为字段。 Each of these (if it has object type) can also either refer to an object, or be null.其中每一个(如果它具有 object 类型)也可以指 object,或者是 null。 The object that you created with new Producto(txtCodigoProducto.getText());您使用new Producto(txtCodigoProducto.getText()); has five variables inside.里面有五个变量。 The one called codigo refers to an object.所谓的codigo指的是 object。 The other four are null.其他四个是 null。

Checking if (producto.getStock()==null) will only work if producto refers to an object.仅当producto引用 object 时,检查if (producto.getStock()==null)是否有效。 It (presumably) asks that object for the value of its stock field, then checks whether that value is null, or refers to an object.它(可能)向 object 询问其stock字段的值,然后检查该值是 null,还是引用 object。

That's a completely different check from if(producto == null) , which only checks the producto variable itself.这是与if(producto == null)完全不同的检查,它只检查producto变量本身。

Producto producto = new Producto(...); absolutely guarantees that producto is not null .绝对保证producto不是null System.out.println(producto) therefore calls producto.toString() . System.out.println(producto)因此调用producto.toString()

It is the toString() implementation of Producto that returns null or the text "null" . ProductotoString()实现返回null或文本"null"

if (producto == null) is a meaningless statement, since producto cannot be null . if (producto == null)是一个毫无意义的语句,因为producto不能是null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM