(java)for循环让一个字符串数组得到一个文件夹下所有文件名却报空指针异常
(java)for循环让一个字符串数组得到一个文件夹下所有文件名却报空指针错误
代码是:
String[] absoluteFilePath = null;
try {
File file = new File(filePath);
if( file.isDirectory()) {
System.out.println("文件夹");
String[] fileList = file.list();
for (int i = 0; i < fileList.length; i++) {
File fullFile = new File(filePath + "\\" + fileList[i]);
absoluteFilePath[i] = fullFile.getAbsolutePath();
System.out.println("absoluteFilePath=" + absoluteFilePath[i]);
}
}
}
然后会在 absoluteFilePath[i] = fullFile.getAbsolutePath(); 这一行报java.lang.NullPointerException
------解决思路----------------------
从 fullFile.getAbsolutePath() 到
再到
fullFile.getAbsolutePath(); 会先去获取fileFile.getPath()
然后再 做后续的操作,这里的fileFile.getPath() 为null
------解决思路----------------------
absoluteFilePath 就没被创建啊,你在String[] fileList = file.list();下增加一句代码,变成这样就OK了
代码是:
String[] absoluteFilePath = null;
try {
File file = new File(filePath);
if( file.isDirectory()) {
System.out.println("文件夹");
String[] fileList = file.list();
for (int i = 0; i < fileList.length; i++) {
File fullFile = new File(filePath + "\\" + fileList[i]);
absoluteFilePath[i] = fullFile.getAbsolutePath();
System.out.println("absoluteFilePath=" + absoluteFilePath[i]);
}
}
}
然后会在 absoluteFilePath[i] = fullFile.getAbsolutePath(); 这一行报java.lang.NullPointerException
------解决思路----------------------
从 fullFile.getAbsolutePath() 到
public String getAbsolutePath() {
return fs.resolve(this);
}
再到
public String resolve(File f) {
String path = f.getPath();
int pl = f.getPrefixLength();
if ((pl == 2) && (path.charAt(0) == slash))
return path; /* UNC */
if (pl == 3)
return path; /* Absolute local */
if (pl == 0)
return getUserPath() + slashify(path); /* Completely relative */
if (pl == 1) { /* Drive-relative */
String up = getUserPath();
String ud = getDrive(up);
if (ud != null) return ud + path;
return up + path; /* User dir is a UNC path */
}
if (pl == 2) { /* Directory-relative */
String up = getUserPath();
String ud = getDrive(up);
if ((ud != null) && path.startsWith(ud))
return up + slashify(path.substring(2));
char drive = path.charAt(0);
String dir = getDriveDirectory(drive);
String np;
if (dir != null) {
/* When resolving a directory-relative path that refers to a
drive other than the current drive, insist that the caller
have read permission on the result */
String p = drive + (':' + dir + slashify(path.substring(2)));
SecurityManager security = System.getSecurityManager();
try {
if (security != null) security.checkRead(p);
} catch (SecurityException x) {
/* Don't disclose the drive's directory in the exception */
throw new SecurityException("Cannot resolve path " + path);
}
return p;
}
return drive + ":" + slashify(path.substring(2)); /* fake it */
}
throw new InternalError("Unresolvable path: " + path);
}
fullFile.getAbsolutePath(); 会先去获取fileFile.getPath()
然后再 做后续的操作,这里的fileFile.getPath() 为null
------解决思路----------------------
absoluteFilePath 就没被创建啊,你在String[] fileList = file.list();下增加一句代码,变成这样就OK了
String[] fileList = file.list();
absoluteFilePath = new String[fileList.length];