Reputation: 21
I know that there are lots of threads on Null Pointer Exception in Java here but I still cannot figure out what is going on here
I am trying to come up with a solution for Transitive Dependencies Kata 18 which is posted at http://codekata.pragprog.com/2007/01/kata_eighteen_t.html
add_direct method is supposed to take in a char and a srting of single space separated characters separated by a space into an ArrayList. Then I try to insert into a HashMap of ArrayLists where initial char is the key and my parsed ArrayList is the item. This is the place where my NullPointerException occurs. I have traced all of these data structures and none of them have a Null value. I don't understand what is causing my exception here. Please see my code:
public class Test_Dependencies
{
public static void main(String[] args) {
Dependencies Dep = new Dependencies();
Dep.add_direct('A', "B C");
}
}
public class Dependencies {
HashMap dependenciesList;
public Dependencies()
{
HashMap<Character, ArrayList> dependenciesList = new HashMap<Character, ArrayList>();
}
public void add_direct(char mainItem, String dependentItems)
{
String[] individualItems = dependentItems.split("\\s+");
ArrayList<Character> dependendentItemsArray;
dependendentItemsArray = new ArrayList<Character>();
//put all items into the ArrayList
for(int i = 0; i < individualItems.length; i++)
{
Character c = new Character(individualItems[i].charAt(0));
dependendentItemsArray.add(c);
}
// insert dependency
Character key = new Character(mainItem);
***//NULL POINTER EXCEPTION HERE
dependenciesList.put(key, dependendentItemsArray);***
}
}
I am a bit perprlexed because dependenciesList, key or dependentItemsArray are not null
Upvotes: 1
Views: 674
Reputation: 523
You have failed to initialize your dependenciesList. Please do null check and then set any values as its the best practice.
package com.stackoverflow.examples;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Dependencies {
public void add_direct(char mainItem, String dependentItems) {
String[] individualItems = dependentItems.split("\\s+");
HashMap<Character, ArrayList<Character>> dependenciesList = new HashMap<Character, ArrayList<Character>>();
ArrayList<Character> dependendentItemsArray = new ArrayList<Character>();
// Put all items into the ArrayList
for (int i = 0; i < individualItems.length; i++) {
Character c = new Character(individualItems[i].charAt(0));
dependendentItemsArray.add(c);
}
// Insert dependency
Character key = new Character(mainItem);
// Always check null before put
if (dependenciesList != null)
dependenciesList.put(key, dependendentItemsArray);
System.out.println("dependenciesArray---->" + dependendentItemsArray);
System.out.println("dependenciesList---->" + dependenciesList);
}
}
Output:
dependenciesArray---->[B, C]
dependenciesList---->{A=[B, C]}
Upvotes: -1
Reputation: 7425
You have not initialized your dependenciesList.
Make the following changes
public class Dependencies {
HashMap<Character, ArrayList> dependenciesList;
public Dependencies()
{
dependenciesList = new HashMap<Character, ArrayList>();
}
....
}
You were creating a new HashMap inside constructor, but your class variable dependenciesList
was null
Upvotes: 2
Reputation:
Actually, dependenciesList
is null. The variable you're initializing in your constructor:
HashMap<Character, ArrayList> dependenciesList = new HashMap<Character, ArrayList>();
is only setting a local variable, which gets deallocated at the end of the function. Remove the leading type (HashMap<Character, ArrayList>
) to make it initialize the instance variable instead.
Upvotes: 3
Reputation: 12776
When you specify HashMap<Character, ArrayList> dependenciesList
in your constructor, that is a different map than the class field dependenciesList. Get rid of the type identifier in front of it. When you refer to dependenciesList
in your add_direct
method, that is the uninitialized class field.
You may want to refresh yourself on how block scoping works in Java. Declaring a new variable inside a block such as a constructor with the same name as a variable in a higher scope shadows the existing declaration.
You also will probably want to follow the Java style conventions in the future, to assist other people who will have to read your code.
Upvotes: 3