我学习一个android的文件浏览器,运用时出现了一个空指针异常,百思不得其解。运行程序时,可以浏览部分文件夹和文件,但是访问某些文件夹,程序发生异常提示空指针,具体指向的代码我也看了,但是始终没有搞懂原因,也没能解决这个问题,求大虾指点如何解决,万分感激,以下贴出源代码和错误的代码FANAL EXCEPTION :main
Java. lang.nullPointException
    at com.XX.fileBroswer.selectfile.fill(selectfile.java:198)源码:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.select_book);
if (isSDCardNull() == true) {
browseToRoot();
this.setSelection(0);
} else {
new AlertDialog.Builder(this).setTitle("提示").setMessage("请插入SD卡")
.create().show();
return; } } // sdcard是否存在
public boolean isSDCardNull() {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
} // 打开根目录
private void browseToRoot() {
// TODO Auto-generated method stub
browseTo(new File("/")); } // 返回上级目录
private void upOneLevel() {
if (this.currentDirectory.getParent() != null)
this.browseTo(this.currentDirectory.getParentFile());
} // 打开目录
private void browseTo(final File aDirectory) {
// TODO Auto-generated method stub
if (this.displayMode == DISPLAYMODE.RELATIVE)
this.setTitle(aDirectory.getAbsolutePath());
if (aDirectory.isDirectory()) {
this.currentDirectory = aDirectory;
// 列出文件
fill(aDirectory.listFiles());


}
// 只打开文本类型的文件
else if (aDirectory.isFile()) {// 文本类文件则打开
if (checkEndsWithInStringArray(aDirectory.getName().toString(),
getResources().getStringArray(R.array.fileEndingText))) {// 跳转到readFromFileBroswer打开文件,需传递文件绝对路径
Intent intent = new Intent();
intent.setClass(selectBook.this, readFromFileBroswer.class);
intent.putExtra("filepath", aDirectory.getAbsolutePath());
startActivity(intent); } else {
new AlertDialog.Builder(this).setTitle("提示")
.setMessage("不支持打开该类文件").create().show(); } }
} /**
 * @param files
 */
private void fill(File[] files) {----就是这里报的错误
try{
// //先清空
this.directoryEntries.clear();
// BitmapFactory.Options options = new BitmapFactory.Options();
// Add the "." == "current directory"
// 显示一个当前目录
this.directoryEntries.add(new IconifiedText(
getString(R.string.current_dir), getResources().getDrawable(
R.drawable.folder)));
// and the ".." == 'Up one level'
// 如果当前文件夹的父文件夹不为空,则显示返回上级目录这个项
if (this.currentDirectory.getParent() != null)
// &&this.currentDirectory.getPath().endsWith("/sdcard")==false)
this.directoryEntries.add(new IconifiedText(
getString(R.string.up_one_level), getResources()
.getDrawable(R.drawable.uponelevel))); Drawable currentIcon = null;
for (File currentFile : files) {
if (currentFile.isDirectory()) {
currentIcon = getResources().getDrawable(R.drawable.folder);
} else {
// 得到文件名
String fileName = currentFile.getName();
/*
 * Determine the Icon to be used, depending on the FileEndings
 * defined in: res/values/fileendings.xml.
 */
// 根据文件名中包括的后缀名的不同,设置不同的图片
// 文件为图片类型
if (checkEndsWithInStringArray(fileName, getResources()
.getStringArray(R.array.fileEndingImage))) {
currentIcon = getResources().getDrawable(R.drawable.image);
}
// 文件为Webtext类型
else if (checkEndsWithInStringArray(fileName, getResources()
.getStringArray(R.array.fileEndingWebText))) {
currentIcon = getResources()
.getDrawable(R.drawable.webtext);
}
// 文件为压缩包类型
else if (checkEndsWithInStringArray(fileName, getResources()
.getStringArray(R.array.fileEndingPackage))) {
currentIcon = getResources().getDrawable(R.drawable.packed);
}
// 文件为多媒体类型
else if (checkEndsWithInStringArray(fileName, getResources()
.getStringArray(R.array.fileEndingAudio))) {
currentIcon = getResources().getDrawable(R.drawable.audio);
}
// 文件为文本类型
else if (checkEndsWithInStringArray(fileName, getResources()
.getStringArray(R.array.fileEndingText))) {
currentIcon = getResources().getDrawable(R.drawable.text);
} // 其他设置为文本类型
else {
currentIcon = getResources().getDrawable(R.drawable.unknow);
}
}
// 判定显示模式
switch (this.displayMode) {
case ABSOLUTE:
/* On absolute Mode, we show the full path */
this.directoryEntries.add(new IconifiedText(currentFile
.getPath(), currentIcon));
break;
case RELATIVE:
/*
 * On relative Mode, we have to cut the current-path at the
 * beginning
 */
int currentPathStringLenght = this.currentDirectory
.getAbsolutePath().length();
this.directoryEntries.add(new IconifiedText(currentFile
.getAbsolutePath().substring(currentPathStringLenght),
currentIcon)); break;
}

}
Collections.sort(this.directoryEntries); IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
itla.setListItems(this.directoryEntries);
this.setListAdapter(itla);
}
catch (Exception e) {
// TODO: handle exception Toast.makeText(selectBook.this,"你没有权限访问此文件夹",20);
Intent intent=new Intent();
intent.setClass(selectBook.this, homeActivity.class);
startActivity(intent);
return;
}
} // 重写listview的item单击事件
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// 选中的行id,即对应于选中的listview的item项
int selectionRowID = (int) l.getItemIdAtPosition(position);
// 得到选中项的文本字符串
String selectedFileString = this.directoryEntries.get(selectionRowID)
.getText();
// 如果选中的是当前目录
if (selectedFileString.equals(getString(R.string.current_dir))) {
// Refresh
this.browseTo(this.currentDirectory);
// 返回上级目录
} else if (selectedFileString.equals(getString(R.string.up_one_level))) {
this.upOneLevel();
} else {
File clickedFile = null;
switch (this.displayMode) {
case RELATIVE:
clickedFile = new File(this.currentDirectory.getAbsolutePath()
+ this.directoryEntries.get(selectionRowID).getText());
break;
case ABSOLUTE:
clickedFile = new File(this.directoryEntries
.get(selectionRowID).getText());
break;
}
if (clickedFile != null)
this.browseTo(clickedFile);
}

}