卡在 Java 中的 2 个 NullPointerException 错误
Stuck on 2 NullPointerException errors in Java
我正在尝试将文件列表加载到数组中,然后获取这些数组并从中逐行提取信息。我已经尝试解决这个问题将近 4 个小时了,但我不明白 NullPointerException 错误要我做什么。我在 initializeLocations 方法中的 for 循环声明处以及调用 burns.initializeLocations() 时的主要错误中收到错误。有什么建议么?
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class LocationNode
{
private String name;
private String hoursText;
private String descriptionText;
//add image for location for info page as an attribute later
public LocationNode(){
name = "";
hoursText = "";
descriptionText = "";
}
//constructor
public LocationNode(String initName, String initHours, String initDescription){
name = initName;
hoursText = initHours;
descriptionText = initDescription;
}
public void setName(String textName){
name = textName;
}
public String getName(){
return name;
}
public void setHours(String textHours){
hoursText = textHours;
}
public String getHours(){
return hoursText;
}
public void setDescription(String textDescription){
descriptionText = textDescription;
}
public String getDescription(){
return descriptionText;
}
public void initializeLocations() throws IOException
{
File folder = null;
File[] listOfFiles;
//creates "path to files" variable
folder = new File(".Tiger Map App/src/LocationTextFiles");
//creates array of all the files in the folder
listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) { //runs for loop to "number of files"
//takes current file in loop iteration and sticks it in a temp file
File temp = listOfFiles[i];
//checks if file is valid
if (temp.isFile() && temp.getName().endsWith(".txt"))
{
//takes contents of file and places them into a String(can replace "content" variable w/ array)
String content = FileUtils.readFileToString(temp, "UTF-8");
//test printing if contents are actually pulling from file
System.out.println(content);
}
}
}
public static void main(String[] args) throws IOException
{
LocationNode burns = new LocationNode();
burns.initializeLocations();
}
}
//creates "path to files" variable
folder = new File(".Tiger Map App/src/LocationTextFiles");
//creates array of all the files in the folder
listOfFiles = folder.listFiles();
您确定这是正确的目录吗?如果该目录中没有文件或者您指定的目录名称不正确,我在此处复制的第二行可以将 listOfFiles 分配给 null。然后在你的循环中
listOfFiles.length
将导致空指针,因为您正在尝试获取空实例的长度。
根据the documentation,“.Tiger Map App/src/LocationTextFiles”很可能不是文件夹。
Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
listOfFiles 可以为空。检查这是正确的 folder = new File(".Tiger Map App/src/LocationTextFiles");
在循环之前输出 listofffiles 的值,我怀疑它将为空,因此 listoffiles.length 抛出异常。
我正在尝试将文件列表加载到数组中,然后获取这些数组并从中逐行提取信息。我已经尝试解决这个问题将近 4 个小时了,但我不明白 NullPointerException 错误要我做什么。我在 initializeLocations 方法中的 for 循环声明处以及调用 burns.initializeLocations() 时的主要错误中收到错误。有什么建议么?
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class LocationNode
{
private String name;
private String hoursText;
private String descriptionText;
//add image for location for info page as an attribute later
public LocationNode(){
name = "";
hoursText = "";
descriptionText = "";
}
//constructor
public LocationNode(String initName, String initHours, String initDescription){
name = initName;
hoursText = initHours;
descriptionText = initDescription;
}
public void setName(String textName){
name = textName;
}
public String getName(){
return name;
}
public void setHours(String textHours){
hoursText = textHours;
}
public String getHours(){
return hoursText;
}
public void setDescription(String textDescription){
descriptionText = textDescription;
}
public String getDescription(){
return descriptionText;
}
public void initializeLocations() throws IOException
{
File folder = null;
File[] listOfFiles;
//creates "path to files" variable
folder = new File(".Tiger Map App/src/LocationTextFiles");
//creates array of all the files in the folder
listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) { //runs for loop to "number of files"
//takes current file in loop iteration and sticks it in a temp file
File temp = listOfFiles[i];
//checks if file is valid
if (temp.isFile() && temp.getName().endsWith(".txt"))
{
//takes contents of file and places them into a String(can replace "content" variable w/ array)
String content = FileUtils.readFileToString(temp, "UTF-8");
//test printing if contents are actually pulling from file
System.out.println(content);
}
}
}
public static void main(String[] args) throws IOException
{
LocationNode burns = new LocationNode();
burns.initializeLocations();
}
}
//creates "path to files" variable
folder = new File(".Tiger Map App/src/LocationTextFiles");
//creates array of all the files in the folder
listOfFiles = folder.listFiles();
您确定这是正确的目录吗?如果该目录中没有文件或者您指定的目录名称不正确,我在此处复制的第二行可以将 listOfFiles 分配给 null。然后在你的循环中
listOfFiles.length
将导致空指针,因为您正在尝试获取空实例的长度。
根据the documentation,“.Tiger Map App/src/LocationTextFiles”很可能不是文件夹。
Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
listOfFiles 可以为空。检查这是正确的 folder = new File(".Tiger Map App/src/LocationTextFiles");
在循环之前输出 listofffiles 的值,我怀疑它将为空,因此 listoffiles.length 抛出异常。