简体   繁体   English

JDBC多对一映射/空指针异常错误

[英]JDBC many-to-one mapping / null pointer exception error

Right so I have been working on this project for awhile, and I am using JDBC + JSF. 正确,因此我已经在这个项目上工作了一段时间,并且正在使用JDBC + JSF。

I currently have 3 DAOs, User, Groups, and UserGroups. 我目前有3个DAO,用户,组和用户组。

My question would be..how do you establish a relationship between the 3 classes , User, Groups and UserGroups?? 我的问题是..您如何在3个类(用户,组和用户组)之间建立关系?

In my database, UserGroup is a associative entity, which has IDUSer and IDGroup as a Primary Composite Key. 在我的数据库中,UserGroup是一个关联实体,它具有IDUSer和IDGroup作为主组合键。

The relationship between User and UserGroup is 1 to Many. 用户和用户组之间的关系是1对多。 The relationship between Groups and UserGroup is also 1 to Many, with UserGroup having a Many to 1 relation with both User and Groups. 组和用户组之间的关系也是一对多的关系,其中用户组与用户和组之间都具有多对一的关系。

Would this be right if I want to create a relationship between User and UserGroup, Group and UserGroup?? 如果我要在用户和用户组,组和用户组之间建立关系,这会正确吗?

public class UsuariousGrupos {

Integer id_usuario;
Integer id_grupo;
Grupos  groups;
Usuarious users;

public UsuariousGrupos() {
}

public UsuariousGrupos(Integer id_usuario, Integer id_grupo) {
    this.id_usuario = id_usuario;
    this.id_grupo = id_grupo;
}

public Integer getId_grupo() {
    return id_grupo;
}

public void setId_grupo(Integer id_grupo) {
    this.id_grupo = id_grupo;
}

public Integer getId_usuario() {
    return id_usuario;
}

public void setId_usuario(Integer id_usuario) {
    this.id_usuario = id_usuario;
}

public Grupos getGroups() {
    return groups;
}

public void setGroups(Grupos groups) {
    this.groups = groups;
}

public Usuarious getUsers() {
    return users;
}

public void setUsers(Usuarious users) {
    this.users = users;
}

Also, I tested my DAO connection, and they are working fine. 另外,我测试了我的DAO连接,并且它们工作正常。 I am able to display name/description from my User database table, and Groups database table. 我可以显示用户数据库表和组数据库表中的名称/说明。 However, since my UserGroup table stores just IDs, I am only able to display the IDs. 但是,由于我的UserGroup表仅存储ID,因此我只能显示ID。 I want to display the name/description of the User/Group that is related to the ID. 我想显示与ID相关的用户/组的名称/说明。 However, when I do the following: 但是,当我执行以下操作时:

 List<UsuariousGrupos> ugdList = ugd.list() ;
    System.out.println("List of users successfully queried: " + ugdList);
    System.out.println("Thus, amount of usergroups in database is: " + ugdList.size());
    for (int i = 0; i < ugdList.size(); i++) {
        System.out.println("Groups in database are: " + ugdList.get(i).getUsers().getNome());
        System.out.println("Users in database are: " + ugdList.get(i).getGroups().getDescricao());
    }

I get a Exception in thread "main" java.lang.NullPointerException. 我在线程“主” java.lang.NullPointerException中得到异常。 Apparently, I am unable to access the Users and Groups object from my UserGroup List : / , which leads me to think that I am missing some sort of relationship among the classes. 显然,我无法从用户组列表/中访问“用户和组”对象,这使我认为我在类之间缺少某种关系。

The null pointer exception occurs at these lines: 空指针异常发生在以下行:

System.out.println("Groups in database are: " + ugdList.get(i).getUsers().getNome());
System.out.println("Users in database are: " + ugdList.get(i).getGroups().getDescricao()

apparently, ugdList.get(i).getUsers() and ugdList.get(i).getGroups() are null. 显然,ugdList.get(i).getUsers()和ugdList.get(i).getGroups()为null。

Any thoughts on this?? 有什么想法吗?

You need to map the name of each user and overload the toString method of the user class like this : 您需要映射每个用户的名称,并重载用户类的toString方法,如下所示:

public String toString()
{
return String.format(""%s %s", this.name, this.lastName);
}

By the way, ugdList.get(i).getUsers().getNome()); 顺便说一句,ugdList.get(i).getUsers()。getNome()); seems wrong to me But using the toString method applied earlier, you can do this : 对我来说似乎是错误的,但是使用先前应用的toString方法,您可以这样做:

for (User user : ugdList.get(i).getUsers())
{
printf(user);
}

BUT you should test ugdList.get(i).getUsers() by steps before : check that get(i).getUsers != null then run the loop. 但是您应该通过以下步骤测试ugdList.get(i).getUsers():检查get(i).getUsers!= null,然后运行循环。

Hope this helps ! 希望这可以帮助 !

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

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