简体   繁体   English

getter / setter名称格式上的空指针

[英]Null pointer on getter/ setter name format

I have an entity . 我有一个实体。

@Entity
@Table(name = "TABLE_NAME")
public class someName implements Serializable{
....
...
@ManyToOne
@JoinColumn(name="some", referencedColumnName="some_ID", updatable = false)
private Data data;
//corresponding getter and setter

public Data getData() {
    return data;
   }

public void setDataTYPE(Data data) { //this name is not in proper format
    this.data = data;
   }

}

Now when i do getData on this entity object, I get a null pointer exception although the object is not null. 现在,当我对此实体对象执行getData时,尽管对象不为null,但我得到了null指针异常。 Code that generated the null pointer 生成空指针的代码

if(someNameOBJ==null){
//Do something
}else{
 sysout(someNameOBJ.getData.getId);// It generates NPE
}

But when i changed my getter/setter to proper format (generate it from eclipse), the null pointer is gone. 但是当我将getter / setter更改为正确的格式(从eclipse生成)时,空指针消失了。

public Data getData() {
   return data;
}
public void setData(Data data) { //this name is  in proper format
    this.data = data;
}

Why is the null pointer gone in this case? 为什么在这种情况下空指针不可用? Does naming of getter/setter should be in proper format? getter / setter的命名应采用正确的格式吗? If so why? 如果可以,为什么? I am aware of Java bean naming conventions, but what difference does a function name makes. 我知道Java Bean的命名约定,但是函数名称有什么区别。 (As I am always concerned about what a function does rather then what its name is). (因为我一直担心函数的功能,而不是函数的名称)。 I am using hibernate and spring 3, java 1.6. 我正在使用hibernate和spring 3,java 1.6。 I think it is a hibernate issue and something to do with reflection. 我认为这是一个休眠的问题,与反思有关。 Help me to find the root 帮我找到根

Does naming of getter/setter should be in proper format?

Yes, Spring will search for getter/setter in proper format with variable name. 是的, Spring将以正确的格式搜索带有变量名的getter / setter。 Because there are many possibles ways to declared getter/setter and is depend on user, how he/she wants to give names. 因为有多种可能的方法来声明getter / setter且取决于用户,所以他/她希望如何命名。

But its difficult to search for all possible getter/setter method for Spring. 但是很难为Spring搜索所有可能的getter / setter方法。 So It use standard naming convention to access variable. 因此,它使用标准命名约定来访问变量。

Basically, Hibernate will use gette and setter methods to reade and write data into/from database. 基本上,Hibernate将使用gette和setter方法从数据库读取数据或将数据写入数据库。 Hibernate assumes that anything that it is writing to the database will eventually need to be read from the database. Hibernate假定最终写入数据库的所有内容都需要从数据库中读取。

now in first case when you try to save object into database hibernate call 现在在第一种情况下,当您尝试将对象保存到数据库休眠调用时

public void setData(Data data)

method throw reflection( Hibernate will use reflection to access the setter ) but hibernate will not get this method and save null into database (check your table) now when you call 方法抛出反射( Hibernate将使用反射来访问setter ),但是当您调用时,hibernate不会获取此方法并将空值保存到数据库中(检查表)

getData()

method this will retun null value saved into database and throws Null Pointer Exception . 方法,它将重新调整保存到数据库中的null值并引发Null Pointer Exception

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

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