Question: This is my snippet of my model class:

```
public class OrganisationalStructure {
    Integer pk;
    String parentId;
    String childId;
    String description;
    ArrayList<OrganisationalStructure> children = new ArrayList<OrganisationalStructure>();

```

This is a snippet of my service class:

```
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);
}

```

From what I understand, the children variable in my model class has been properly initialized but when "parent.getChildren" line of the service class is encountered, Tomcat throws a NullPointerException.

Any ideas on how to fix this?

I've searched the net for NullPointerException and various replies/answers say the variable must be initialized. I thought I've done that already in my model class.

staffDao.getOrganisationStructure is a class that returns DB records. I am using hibernate to retrieve the data.

Answer: 
Change this

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

```

to

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

```

Every `Iterator#next()` should be checked with `hasNext()`. You're calling `next()` twice within the loop.
Perhaps your `parent` is not initialized. It seems likely to me, based on what you've shown, that `organisationalStructure.getParentId()` is not a key in your `HashMap`. But we would have to see the code where you populate it.

In answer to your question: yes, you have initialized the `ArrayList` properly. So assuming `getChildren()` returns that `ArrayList`, it should not return `null` (unless the reference is modified somewhere else in your code).

In response to your edited post:

Your code structure makes no sense. The line below will fetch *two* new records, which certainly isn't what you want. Essentially, all of your keys will match to the incorrect `OrganizationalStructure`, and there will only be keys for every other entry.

```
hashMap.put(iterator.next().getChildId(), iterator.next());

```

Instead, you need to store a reference to what `iterator.next()` returns, such as:

```
OrganizationalStructure next = iterator.next();
hashMap.put(next.getChildId(), next);

```

Now the keys will line up with the values, and you will legitimately have a reference for every `OrganizationalStructure` returned from your DAO.

Given this, your mistake is in thinking that the `NullPointerException` on this line was from the `getChildren()` call, when your `parent` reference is what's really `null`.

```
parent.getChildren().add(organisationalStructure);

```