我知道有很多关于Java的空指针异常的线程在这里,但我仍然无法弄清是怎么回事是什么原因造成我的空指针异常
我试图想出一个解决方案传递相关性发布在http://codekata.pragprog.com/2007/01/kata_eighteen_t.html
的传递相关性add_direct方法应该将char和由空格分隔的单个空格分隔的字符放入ArrayList中。然后我尝试插入一个ArrayLists的HashMap,其中初始char是关键字,我的解析ArrayList是项目。这是我的NullPointerException发生的地方。我追踪了所有这些数据结构,并没有一个具有空值。我不明白是什么导致我的例外。请参阅我的代码:
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);***
}
}
我有点perprlexed因为dependenciesList,关键还是dependentItemsArray不是空