返回一个新的 ArrayList,正在返回的列表被填充但仍然给出空指针异常
Returning a new ArrayList, the list that is being returned is populated but still gives a null pointer exception
我正在为一个方法编写一个 JUnit 测试用例,该方法将我引向另一个默认方法,它 returns 在调用该默认方法时创建一个新的 ArrayList。
测试方法
public List getTagDataForImage(String tagType, String imageType) {
List tagRules = getTagRulesForUpdateOrQueryImage(tagType, imageType);
List tagData = getTagData(tagRules);
return tagData;
}
在方法中,堆栈跟踪将我指向 getTagRulesForUpdateOrQueryImage(tagType, imageType);
,这将我引向此处
List getTagRulesForUpdateOrQueryImage(String tagType, String imageType) {
List commonList = tagList.getTagRulesForUpdateOrQueryImage(tagType
+ "," + imageTypes[0]); //This line
commonList.addAll(tagList.getTagRulesForUpdateOrQueryImage(tagType
+ "," + imageType));
return commonList;
}
definition for tagList.getTagRulesForUpdateOrQueryImage(tagType
+ "," + imageTypes[0]);
defined in another class
List getTagRulesForUpdateOrQueryImage(String tagType) {
return new ArrayList((List) tagRulesMap.get(tagType));//gives NPE
}
tagRulesMap
是一个在构造函数调用时自动填充的 HashMap
测试用例
@InjectMocks
TagDataFilter tagDataFilter;
@Test
public void testGetTagDataForImage()
{
List get=tagDataFilter.getTagDataForImage("QueryImages", "Common");
}
堆栈跟踪
java.lang.NullPointerException
at java.util.ArrayList.<init>(Unknown Source)
at data.TagList.getTagRulesForUpdateOrQueryImage(TagList.java:107)
at data.TagDataFilter.getTagRulesForUpdateOrQueryImage(TagDataFilter.java:92)
at data.TagDataFilter.getTagDataForImage(TagDataFilter.java:73)
at data.test.TagDataFilterTest.testGetTagDataForImage(TagDataFilterTest.java:54)
我不知道我去了哪里wrong.Please帮我解决这个问题。
仅提供评论中的答案:
List getTagRulesForUpdateOrQueryImage(String tagType) {
return new ArrayList((List) tagRulesMap.get(tagType));//gives NPE
}
未找到关键 tagType,因此它 returns null 然后尝试将其转换为 returns NullPointerException。
我正在为一个方法编写一个 JUnit 测试用例,该方法将我引向另一个默认方法,它 returns 在调用该默认方法时创建一个新的 ArrayList。
测试方法
public List getTagDataForImage(String tagType, String imageType) {
List tagRules = getTagRulesForUpdateOrQueryImage(tagType, imageType);
List tagData = getTagData(tagRules);
return tagData;
}
在方法中,堆栈跟踪将我指向 getTagRulesForUpdateOrQueryImage(tagType, imageType);
,这将我引向此处
List getTagRulesForUpdateOrQueryImage(String tagType, String imageType) {
List commonList = tagList.getTagRulesForUpdateOrQueryImage(tagType
+ "," + imageTypes[0]); //This line
commonList.addAll(tagList.getTagRulesForUpdateOrQueryImage(tagType
+ "," + imageType));
return commonList;
}
definition for tagList.getTagRulesForUpdateOrQueryImage(tagType
+ "," + imageTypes[0]);
defined in another class
List getTagRulesForUpdateOrQueryImage(String tagType) {
return new ArrayList((List) tagRulesMap.get(tagType));//gives NPE
}
tagRulesMap
是一个在构造函数调用时自动填充的 HashMap
测试用例
@InjectMocks
TagDataFilter tagDataFilter;
@Test
public void testGetTagDataForImage()
{
List get=tagDataFilter.getTagDataForImage("QueryImages", "Common");
}
堆栈跟踪
java.lang.NullPointerException
at java.util.ArrayList.<init>(Unknown Source)
at data.TagList.getTagRulesForUpdateOrQueryImage(TagList.java:107)
at data.TagDataFilter.getTagRulesForUpdateOrQueryImage(TagDataFilter.java:92)
at data.TagDataFilter.getTagDataForImage(TagDataFilter.java:73)
at data.test.TagDataFilterTest.testGetTagDataForImage(TagDataFilterTest.java:54)
我不知道我去了哪里wrong.Please帮我解决这个问题。
仅提供评论中的答案:
List getTagRulesForUpdateOrQueryImage(String tagType) {
return new ArrayList((List) tagRulesMap.get(tagType));//gives NPE
}
未找到关键 tagType,因此它 returns null 然后尝试将其转换为 returns NullPointerException。