NullPointerException在使用ArrayList的contains方法时发生。

huangapple go评论75阅读模式
英文:

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&lt;String&gt; 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;
     }
   }

huangapple
  • 本文由 发表于 2020年7月27日 23:23:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63118432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定