英文:
NullPointerException occur when using contains method on an ArrayList
问题
I'm writing this helper method which will be used in another method where several files can be selected. One of these files will be passed as an argument to this helper method. This helper method uses an object from FileResource class which is used to read each word or each line of a file through a for each loop. The words from the files work as a key to an initialized hashmap and the value to this key is an ArrayList which contains the filename as String.
In summary, every word in the file is a key to a hashmap and the key is mapped to an ArrayList containing the file name. This helper method is called several times with different filenames.
the problem is when I use this code, the line "if(!fileList.contains(file))" throws a NullPointerException. This line checks if the ArrayList already contains the String or not.
Is there any problem with my algorithm? what causes this problem to occur?
the code is given below:
private void addWordsFromFile(File fi){
FileResource f = new FileResource(fi);
String file = fi.getName();
System.out.println(file);
for(String s : f.words()){
System.out.println(s);
ArrayList<String> fileList = myMap.get(s);
if(!fileList.contains(file)){
fileList.add(file);
myMap.put(s, fileList);
}
else{
break;
}
}
}
This is the image to this method. It is written in BlueJ environment
英文:
I'm writing this helper method which will be used in another method where several files can be selected. One of these files will be passed as an argument to this helper method. This helper method uses an object from FileResource class which is used to read each word or each line of a file through a for each loop. The words from the files work as a key to an initialized hashmap and the value to this key is an ArrayList which contains the filename as String.
In summary, every word in the file is a key to a hashmap and the key is mapped to an ArrayList containing the file name. This helper method is called several times with different filenames.
the problem is when I use this code, the line "if(!fileList.contains(file))" throws a NullPointerException. This line checks if the ArrayList already contains the String or not.
Is there any problem with my algorithm? what causes this problem to occur?
the code is given below:
private void addWordsFromFile(File fi){
FileResource f = new FileResource(fi);
String file = fi.getName();
System.out.println(file);
for(String s : f.words()){
System.out.println(s);
ArrayList<String> fileList = myMap.get(s);
if(!fileList.contains(file)){
fileList.add(file);
myMap.put(s, fileList);
}
else{
break;
}
}
}
This is the image to this method. It is written in BlueJ environment
答案1
得分: 1
myMap.get(s)
返回 NULL
。
检查条件 if (fileList == null) { ....抛出异常.... }
或者
使用 myMap.getOrDefault(Object key, V defaultValue)
,这样如果请求的 key
未找到,它将分配默认值。
英文:
myMap.get(s)
returns NULL
.
Check the condition if (fileList == null) { ....throw Exception.... }
or
Use myMap.getOrDefault(Object key, V defaultValue)
, so if the requested key
is not found it will assign default value.
答案2
得分: 0
The null pointer exception can happen due to myMap.get(s) returning null.
Before putting contains check in if , you can put a null check like
if(null!=fileList){
if(!fileList.contains(file)){
fileList.add(file);
myMap.put(s, fileList);
} else{
break;
}
}
英文:
The null pointer exception can happen due to myMap.get(s) returning null.
Before putting contains check in if , you can put a null check like
if(null!=fileList){
if(!fileList.contains(file)){
fileList.add(file);
myMap.put(s, fileList);
} else{
break;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论