java - 为什么这个 FileOutputStream 会给出 NullPointerException?

标签 java android fileoutputstream

所以我正在运行一个应用程序,当我尝试写入文件时,该应用程序会创建 NullPointerException。

当用户按下新的设置按钮时,我的第一个 Activity (见下文)调用 NewSet Activity 。当我尝试在 NewSet Activity 中写入输入时,它抛出 NullPointerException。打印出 fileOut 显示它的值为 null,但它不应该为 null。 fileOut 是全局声明的,但在 onCreate 中初始化。两个类如下。 (...表示由于不相关而省略的代码。)

public class MainActivity extends Activity {
    static ArrayList sets;
    FileOutputStream fileOut;
    BufferedReader read;
    String line;
    ArrayAdapter<String> adapter;
    ListView listView;
    Intent newSetIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        newSetIntent=new Intent(this,NewSet.class);

        listView=(ListView)findViewById(R.id.setList);
        sets=new ArrayList();
        try {
            read=new BufferedReader(new BufferedReader(new InputStreamReader(openFileInput("SETS.txt"))));
            fileOut=openFileOutput("SETS.txt",Context.MODE_APPEND);
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            try {

                fileOut.write("".getBytes());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOE");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        getSets();
        toList();
        super.onCreate(savedInstanceState);
    }

    ...

    public void newSet(String newSetName){
        System.out.println(newSetName);
        sets.add(newSetName);

        try {
            fileOut=openFileOutput("SETS.txt",Context.MODE_APPEND);
            fileOut.write(newSetName.getBytes());
            fileOut.write("\n".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (NullPointerException NPE){
            System.out.println(fileOut);
            NPE.printStackTrace();
        }


    }
}





public class NewSet extends Activity {

    EditText input;
    String setName;
    MainActivity main;
    ...
    public void submit(View view){
        setName=input.getText().toString();
        System.out.println("CREATING SET:"+setName);
        main.newSet(setName);
        finish();

    }

}

完整的堆栈跟踪

03-03 23:08:42.183: W/System.err(8435): java.lang.NullPointerException
03-03 23:08:42.183: W/System.err(8435):     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:173)
03-03 23:08:42.183: W/System.err(8435):     at com.ollien.flashcards.MainActivity.newSet(MainActivity.java:112)
03-03 23:08:42.183: W/System.err(8435):     at com.ollien.flashcards.NewSet.submit(NewSet.java:35)
03-03 23:08:42.193: W/System.err(8435):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 23:08:42.193: W/System.err(8435):     at java.lang.reflect.Method.invoke(Method.java:511)
03-03 23:08:42.193: W/System.err(8435):     at android.view.View$1.onClick(View.java:3594)
03-03 23:08:42.193: W/System.err(8435):     at android.view.View.performClick(View.java:4204)
03-03 23:08:42.193: W/System.err(8435):     at android.view.View$PerformClick.run(View.java:17355)
03-03 23:08:42.193: W/System.err(8435):     at android.os.Handler.handleCallback(Handler.java:725)
03-03 23:08:42.203: W/System.err(8435):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 23:08:42.203: W/System.err(8435):     at android.os.Looper.loop(Looper.java:137)
03-03 23:08:42.203: W/System.err(8435):     at android.app.ActivityThread.main(ActivityThread.java:5226)
03-03 23:08:42.203: W/System.err(8435):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 23:08:42.203: W/System.err(8435):     at java.lang.reflect.Method.invoke(Method.java:511)
03-03 23:08:42.203: W/System.err(8435):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-03 23:08:42.203: W/System.err(8435):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-03 23:08:42.203: W/System.err(8435):     at dalvik.system.NativeStart.main(Native Method)

最佳答案

android.content.ContextWrapper 类中抛出异常。方法是:

public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException
{
    return mBase.openFileOutput(name, mode); // Line number 173
}

在这里,我看到可能导致 NullPointerException 的唯一可能性是 Context 类型的数据成员 mBase 为 NULL。

您能否检查Context数据成员是否为非空。

关于java - 为什么这个 FileOutputStream 会给出 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15194471/

相关文章:

android - 如何将 imageView 放在 cardview 前面?当两者都是相对布局的 child 时

java - Java 中的 ConnectFour 程序问题

java - Vaadin CheckBox 对象变为 null

android - 使音频播放器搜索栏可点击

java - 如何计算总项目的总和

Java FileOutputStream 字符串写入

java - 如何扩展范围\w\d+-\w\d+

java.lang.ClassCastException : androidx. appcompat.widget.AppCompatImageButton 无法转换为 android.widget.Button

java - 我可以使字节数组的大小动态化吗?

android - 我怎样才能 "mount"Android 中的虚拟驱动器?