【问题标题】:NullPointerException: Unable to list files in a directoryNullPointerException:无法列出目录中的文件
【发布时间】:2013-11-17 17:37:13
【问题描述】:

请看下面的代码

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)

这里发生了什么?

【问题讨论】:

  • 试试这个 if(fileNameList != null && fileNameList.lenght > 0) 你也可以使用 try-catch 块

标签: java android io nullpointerexception layout-inflater


【解决方案1】:

评估的第一个条件将是fileNameList 的长度。或者如果fileNameListnull,它会抛出一个NPE,因为你首先尝试访问它的length 属性。

你应该反转你的条件:

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

为什么?

这些运算符表现出“短路”行为,这意味着 仅在需要时才评估第二个操作数。

因此,如果 fileNameListnull,则不会计算第二个操作数,因此您可以避免抛出 NPE

【讨论】:

    【解决方案2】:

    你应该使用

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

    否则你有一个NullPointerException,因为fileNameList.length 其中fileNameListNULL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 2023-03-03
      • 2017-09-29
      • 1970-01-01
      • 2022-10-25
      相关资源
      最近更新 更多