Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有ArrayList的java.lang.NullPointerException_Java - Fatal编程技术网

带有ArrayList的java.lang.NullPointerException

带有ArrayList的java.lang.NullPointerException,java,Java,这是我的模型类的片段: public class OrganisationalStructure { Integer pk; String parentId; String childId; String description; ArrayList<OrganisationalStructure> children = new ArrayList<OrganisationalStructure>(); 公共类组织结构{ 整数pk

这是我的模型类的片段:

public class OrganisationalStructure {
    Integer pk;
    String parentId;
    String childId;
    String description;
    ArrayList<OrganisationalStructure> children = new ArrayList<OrganisationalStructure>();
公共类组织结构{
整数pk;
字符串parentId;
字符串childId;
字符串描述;
ArrayList子项=新的ArrayList();
这是我的服务类的一个片段:

HashMap<String, OrganisationalStructure> hashMap = new HashMap<String, OrganisationalStructure>();
Iterator<OrganisationalStructure> iterator = staffDao.getOrganisationalStructure().iterator();

while (iterator.hasNext()) {
    hashMap.put(iterator.next().getChildId(), iterator.next());
}

for (OrganisationalStructure organisationalStructure: hashMap.values()) {
    OrganisationalStructure parent = hashMap.get(organisationalStructure.getParentId());
    parent.getChildren().add(organisationalStructure);
}
HashMap HashMap=newhashmap();
迭代器迭代器=staffDao.getOrganizationalStructure().Iterator();
while(iterator.hasNext()){
put(iterator.next().getChildId(),iterator.next());
}
for(OrganizationalStructure组织结构:hashMap.values()){
OrganizationalStructure parent=hashMap.get(OrganizationalStructure.getParentId());
parent.getChildren().add(OrganizationalStructure);
}
据我所知,我的模型类中的children变量已经正确初始化,但是当遇到服务类的“parent.getChildren”行时,Tomcat抛出一个NullPointerException

有没有办法解决这个问题

我在网上搜索过NullPointerException,各种回复/回答都说变量必须初始化。我想我已经在我的模型类中这样做了


staffDao.GetOrganizationStructure是一个返回DB记录的类。我正在使用hibernate检索数据。

可能您的
父级
未初始化。根据您所展示的内容,我认为
OrganizationalStructure.getParentId()可能是
不是您的
HashMap
中的一个键。但是我们必须查看填充它的代码

回答您的问题:是的,您已经正确初始化了
ArrayList
。因此,假设
getChildren()
返回
ArrayList
,它不应该返回
null
(除非在代码的其他地方修改了引用)

回应您编辑的帖子:

您的代码结构毫无意义。下面的一行将获取两条新记录,这肯定不是您想要的。实际上,您的所有密钥都将与不正确的
组织结构匹配,并且每个其他条目都只有密钥

hashMap.put(iterator.next().getChildId(), iterator.next());
相反,您需要存储对iterator.next()返回的内容的引用,例如:

OrganizationalStructure next = iterator.next();
hashMap.put(next.getChildId(), next);
现在,键将与值对齐,您将合法地拥有从DAO返回的每个
组织结构的引用

有鉴于此,您的错误在于认为此行的
NullPointerException
来自
getChildren()
调用,而您的
parent
引用实际上是
null

parent.getChildren().add(organisationalStructure);
改变这个

while (iterator.hasNext()) {
    hashMap.put(iterator.next().getChildId(), iterator.next());
}

每个
迭代器#next()
都应该使用
hasNext()
进行检查。您在循环中调用了两次
next()

while (iterator.hasNext()) {
    OrganisationalStructure oStruct = iterator.next();
    hashMap.put(oStruct.getChildId(), oStruct);
}