[英]NullPointerException: Unable to list files in a directory
Please have a look at the following code 请看下面的代码
private class OpenFileEvent implements OnClickListener
{
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final Dialog openFileDialog = new Dialog(Notes.this);
openFileDialog.setTitle("Open File");
openFileDialog.setContentView(R.layout.open_dialog);
//First, list all the available Files
File folder = new File(Environment.getExternalStorageDirectory()+"/Main Notes/Notes");
File[] fileNameList = folder.listFiles();
if(fileNameList.length>0 && fileNameList!=null)
{
for(int i=0;i<fileNameList.length;i++)
{
//Get the sub views first
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View openThisFileView = inflater.inflate(R.layout.open_dialog_file, null);
Button openThisFileButton = (Button)openThisFileView.findViewById(R.id.open_this_file_button);
Button appendThisFileButton = (Button)openThisFileView.findViewById(R.id.append_note_this_file);
TextView openThisFileNameTxt = (TextView)openThisFileView.findViewById(R.id.open_this_file_name);
//Set the Text
openThisFileNameTxt.setText(fileNameList[i].getName());
//Set the Listeners
//Add the View
openFileDialogView.addView(openThisFileView);
}
}
//Show the Dialog
openFileDialog.show();
}
}
As soon as I run this code, I get the following error 一旦运行此代码,就会出现以下错误
11-17 17:31:18.681: E/AndroidRuntime(4868): FATAL EXCEPTION: main
11-17 17:31:18.681: E/AndroidRuntime(4868): java.lang.NullPointerException
11-17 17:31:18.681: E/AndroidRuntime(4868): at com.a.Notter.Notes$OpenFileEvent.onClick(Notes.java:203)
11-17 17:31:18.681: E/AndroidRuntime(4868): at android.view.View.performClick(View.java:4204)
11-17 17:31:18.681: E/AndroidRuntime(4868): at android.view.View$PerformClick.run(View.java:17355)
11-17 17:31:18.681: E/AndroidRuntime(4868): at android.os.Handler.handleCallback(Handler.java:725)
11-17 17:31:18.681: E/AndroidRuntime(4868): at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 17:31:18.681: E/AndroidRuntime(4868): at android.os.Looper.loop(Looper.java:137)
11-17 17:31:18.681: E/AndroidRuntime(4868): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-17 17:31:18.681: E/AndroidRuntime(4868): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 17:31:18.681: E/AndroidRuntime(4868): at java.lang.reflect.Method.invoke(Method.java:511)
11-17 17:31:18.681: E/AndroidRuntime(4868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-17 17:31:18.681: E/AndroidRuntime(4868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-17 17:31:18.681: E/AndroidRuntime(4868): at dalvik.system.NativeStart.main(Native Method)
This error occurs in here, where I have handled the NULL
as well 在这里,我也处理了NULL
,因此会发生此错误
if(fileNameList.length>0 && fileNameList!=null)
What is happening here? 这是怎么回事
The first condition evaluated will be the length of fileNameList
. 评估的第一个条件将是fileNameList
的长度。 Or if fileNameList
is null
it will throw a NPE
because you try first to access its length
attribute. 否则,如果fileNameList
为null
,则将抛出NPE
因为您首先尝试访问其length
属性。
You should inverse your condition : 您应该逆转您的情况:
if(fineNameList != null && fileNameList.length>0)
Why ? 为什么呢
These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed. 这些运算符表现出“短路”行为,这意味着仅在需要时才评估第二个操作数。
So if fileNameList
is null
the second operand won't be evaluated and hence you avoid throwing a NPE
. 因此,如果fileNameList
为null
,则将不评估第二个操作数,因此避免抛出NPE
。
You should use 你应该用
if(fileNameList!=null && fileNameList.length>0)
otherwise you have a NullPointerException
due to fileNameList.length
where fileNameList
is NULL
. 否则,由于fileNameList.length
而导致NullPointerException
,其中fileNameList
为NULL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.