簡體   English   中英

NullPointerException:無法列出目錄中的文件

[英]NullPointerException: Unable to list files in a directory

請看下面的代碼

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的長度。 否則,如果fileNameListnull ,則將拋出NPE因為您首先嘗試訪問其length屬性。

您應該逆轉您的情況:

if(fineNameList != null && fileNameList.length>0)

為什么呢

這些運算符表現出“短路”行為,這意味着僅在需要時才評估第二個操作數。

因此,如果fileNameListnull ,則將不評估第二個操作數,因此避免拋出NPE

你應該用

if(fileNameList!=null && fileNameList.length>0)

否則,由於fileNameList.length而導致NullPointerException ,其中fileNameListNULL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM