java.lang.NullPointerException:尝试获取空数组的长度
我不熟悉Java和android开发。我有兴趣开发一个应用程序,可以将我的android手机(Nexus5)的文件夹同步到亚马逊网络服务(AWS)。我正试图建立在一个开源项目的基础上。但是,我发现这个代码段有以下错误,无法找到我的错误。此时,您的反馈将非常有用java.lang.NullPointerException:尝试获取空数组的长度,java,android,nullpointerexception,Java,Android,Nullpointerexception,我不熟悉Java和android开发。我有兴趣开发一个应用程序,可以将我的android手机(Nexus5)的文件夹同步到亚马逊网络服务(AWS)。我正试图建立在一个开源项目的基础上。但是,我发现这个代码段有以下错误,无法找到我的错误。此时,您的反馈将非常有用 private void loadFileList(File path) { this.currentPath = path; List<String> r = new ArrayList<String&
private void loadFileList(File path) {
this.currentPath = path;
List<String> r = new ArrayList<String>();
if (path.exists()) {
if (path.getParentFile() != null) r.add(PARENT_DIR);
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String filename) {
File sel = new File(dir, filename);
if (!sel.canRead()) return false;
if (selectDirectoryOption) return sel.isDirectory();
else {
boolean endsWith = fileEndsWith != null ? filename.toLowerCase().endsWith(fileEndsWith) : true;
return endsWith || sel.isDirectory();
}
}
{
FileDialog a = FileDialog.this;
//a.super();
}
};
String[] fileList1 = path.list(filter);
for (String file : fileList1) { <-- Error in this line
r.add(file);
}
}
fileList = (String[]) r.toArray(new String[0]);
}
private void loadFileList(文件路径){
this.currentPath=path;
列表r=新的ArrayList();
if(path.exists()){
if(path.getParentFile()!=null)r.add(PARENT_DIR);
FilenameFilter筛选器=新建FilenameFilter(){
公共布尔接受(文件目录,字符串文件名){
文件选择=新文件(目录,文件名);
如果(!sel.canRead())返回false;
如果(selectDirectoryOption)返回sel.isDirectory();
否则{
布尔值endsWith=fileEndsWith!=null?filename.toLowerCase().endsWith(fileEndsWith):true;
返回endsWith | | sel.isDirectory();
}
}
{
FileDialog a=FileDialog.this;
//a、 超级();
}
};
字符串[]fileList1=path.list(过滤器);
对于(字符串文件:fileList1){此行的目的
String[]fileList1=path.list(filter);用于列出目录中的子文件
这里您将看到fileList1[]为空,因为路径文件夹中没有与筛选器匹配的子文件
您可以在此行之前添加空检查
for (String file : fileList1) { <-- Error in this line
r.add(file);
}
for(字符串文件:fileList1){path.list(筛选器)可能返回null。
for (String file : fileList1) { <-- Error in this line
r.add(file);
}
if(fileList1!=null)
{
for (String file : fileList1) { <-- Error in this line
r.add(file);
}
}