NullPointer예외:디렉터리의 파일을 나열할 수 없습니다.
-
21-12-2019 - |
문제
다음 코드를 살펴보십시오.
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();
}
}
이 코드를 실행하자마자 다음 오류가 발생합니다.
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)
이 오류는 내가 처리한 여기에서 발생합니다. NULL
또한
if(fileNameList.length>0 && fileNameList!=null)
여기서 무슨 일이 일어나고 있나요?
해결책
평가된 첫 번째 조건은 길이입니다. fileNameList
.아니면 만약에 fileNameList
~이다 null
그것은 던질 것이다 NPE
왜냐하면 당신이 먼저 그것에 접근하려고 하기 때문입니다. length
기인하다.
조건을 반대로 해야 합니다.
if(fineNameList != null && fileNameList.length>0)
왜 ?
이 연산자들은 "단락 회로"동작을 보여 주므로, 두 번째 피연산자는 필요한 경우에만 평가됩니다.
그래서 만약 fileNameList
~이다 null
두 번째 피연산자는 평가되지 않으므로 NPE
.
다른 팁
를 사용해야합니다.
if(fileNameList!=null && fileNameList.length>0)
.
그렇지 않으면 NullPointerException
가 fileNameList.length
인 fileNameList
로 인해 NULL
가 있습니다.
제휴하지 않습니다 StackOverflow