gpt4 book ai didi

java - DataOutputStream 空指针异常

转载 作者:行者123 更新时间:2023-11-30 11:21:57 24 4
gpt4 key购买 nike

我在 Android 中的 DataOutputStream 上收到了一个 NullPointerException

我首先编写了一些代码,使用 DataOutputStream 在 Java 中写出文件。一切都很完美。我将代码导入到一个 Android 项目中,但出现了一些奇怪的行为。

我更改为特定于 android 的唯一代码是获取目录的方式。

所以到底发生了什么:我正在创建 DataOutputStream 然后关闭它。现在就这些了。我认为测试流是否正确打开的第一步是关闭它。

DataOutputStream 是类实例变量。

当我以相同的方法对其调用关闭时,它会正常关闭。当我从不同的方法对其调用关闭时,它得到 NullPointerException。但是在 Java 中执行这些相同的操作不会导致问题,尽管在 Android 中会出现问题。

public class WavFileOutStream {

private DataOutputStream stream;

public WavFileOutStream(String fileName)
{

// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_RINGTONES), fileName);
if (!file.mkdirs()) {
Log.e("trace", "Directory not created");
}

try {
stream = new DataOutputStream(new FileOutputStream(file));

if(stream==null)
{
Log.i("trace","Null Stream!");
}

//<--PROBLEM LINE 1-->
//If I call close from this line I get NO nullPointer
//stream.close();


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//<--PROBLEM LINE 1-->
//If I call close from this line I get a nullPointer
//I comment out other close() first
close();

}

public void close()
{
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

错误:

02-24 12:49:39.229: E/AndroidRuntime(11539): FATAL EXCEPTION: main
02-24 12:49:39.229: E/AndroidRuntime(11539): Process: com.example.ringtonegenerator, PID: 11539
02-24 12:49:39.229: E/AndroidRuntime(11539): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ringtonegenerator/com.example.ringtonegenerator.MainActivity}: java.lang.NullPointerException
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.os.Handler.dispatchMessage(Handler.java:102)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.os.Looper.loop(Looper.java:136)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-24 12:49:39.229: E/AndroidRuntime(11539): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:49:39.229: E/AndroidRuntime(11539): at java.lang.reflect.Method.invoke(Method.java:515)
02-24 12:49:39.229: E/AndroidRuntime(11539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-24 12:49:39.229: E/AndroidRuntime(11539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-24 12:49:39.229: E/AndroidRuntime(11539): at dalvik.system.NativeStart.main(Native Method)
02-24 12:49:39.229: E/AndroidRuntime(11539): Caused by: java.lang.NullPointerException
02-24 12:49:39.229: E/AndroidRuntime(11539): at com.example.ringtonegenerator.WavFileOutStream.close(WavFileOutStream.java:178)
02-24 12:49:39.229: E/AndroidRuntime(11539): at com.example.ringtonegenerator.WavFileOutStream.<init>(WavFileOutStream.java:43)
02-24 12:49:39.229: E/AndroidRuntime(11539): at com.example.ringtonegenerator.WavFileWriter.writeWave(WavFileWriter.java:10)
02-24 12:49:39.229: E/AndroidRuntime(11539): at com.example.ringtonegenerator.MainActivity.onCreate(MainActivity.java:23)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.app.Activity.performCreate(Activity.java:5231)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-24 12:49:39.229: E/AndroidRuntime(11539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-24 12:49:39.229: E/AndroidRuntime(11539): ... 11 more

最佳答案

如果在创建流时抛出异常,流仍为空,您仍将调用关闭。那里没有检查 null,所以它会抛出 NPE。

public void close()
{
if(stream != null){
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

关于java - DataOutputStream 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com