Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试将文件写入内部数据时,请继续获取NullPointerException_Java_Android_Nullpointerexception_Writer - Fatal编程技术网

Java 尝试将文件写入内部数据时,请继续获取NullPointerException

Java 尝试将文件写入内部数据时,请继续获取NullPointerException,java,android,nullpointerexception,writer,Java,Android,Nullpointerexception,Writer,我正在使用以下代码将json文件从我的资产文件夹复制到数据文件夹,以便在首次启动时使用此数据,直到加载真实数据: AssetManager am = myContext.getAssets(); InputStream is = null; try { is = am.open("settings.json"); } catch (IOException e) { e.printStackTrace(

我正在使用以下代码将json文件从我的资产文件夹复制到数据文件夹,以便在首次启动时使用此数据,直到加载真实数据:

AssetManager am = myContext.getAssets();
        InputStream is = null;
        try {
            is = am.open("settings.json");
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader r = new BufferedReader(new InputStreamReader(is));
        StringBuilder total = new StringBuilder();
        String line = "";
        try {
            while ((line = r.readLine()) != null) {
                total.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                    myContext.openFileOutput("settings.json", Context.MODE_PRIVATE));
            outputStreamWriter.write(line);
            outputStreamWriter.close();
        } catch (IOException e) {
            Log.e("Test", "File write failed: " + e.toString());
        }
但我一直得到以下错误:

06-08 14:07:24.576: E/AndroidRuntime(10509): Caused by: java.lang.NullPointerException
06-08 14:07:24.576: E/AndroidRuntime(10509):    at     java.io.Writer.write(Writer.java:141)
06-08 14:07:24.576: E/AndroidRuntime(10509):    at     com.test.app.MainActivity.onCreate(MainActivity.java:370)
06-08 14:07:24.576: E/AndroidRuntime(10509):    at     android.app.Activity.performCreate(Activity.java:5243)
06-08 14:07:24.576: E/AndroidRuntime(10509):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
这是哪一行:

outputStreamWriter.write(行)


这是因为
在该点为空。
看看循环:
while((line=r.readLine())!=null){
合计.追加(行);
}

循环结束时,
为空。 当您尝试运行以下代码时:
outputStreamWriter.write(行)
您正在将空值传递给
write
方法。
java.io.Writer.write(字符串str)
实现如下:

write(str,0,str.length())


对null对象调用length()方法会导致NullPointerException。

这是因为
在该点为null。
看看循环:
while((line=r.readLine())!=null){
合计.追加(行);
}

循环结束时,
为空。 当您尝试运行以下代码时:
outputStreamWriter.write(行)
您正在将空值传递给
write
方法。
java.io.Writer.write(字符串str)
实现如下:

write(str,0,str.length())

对null对象调用length()方法会导致NullPointerException。

我们有一个以下循环:

while ((line = r.readLine()) != null) {
    total.append(line);
}
因此,很明显,
line
在它的末尾等于
null
。现在,让我们来看看实现。它调用三参数版本的
write()
函数,如下所示:

public void write(String str, int offset, int count) throws IOException {
    if ((offset | count) < 0 || offset > str.length() - count) {
        throw new StringIndexOutOfBoundsException(str, offset, count);
    }
    char[] buf = new char[count];
    str.getChars(offset, offset + count, buf, 0);
    synchronized (lock) {
        write(buf, 0, buf.length);
    }
}
public void write(字符串str、int offset、int count)引发IOException{
如果((偏移量|计数)<0 | |偏移量>str.length()-count){
抛出新的StringIndexOutOfBoundsException(str、offset、count);
}
char[]buf=新字符[计数];
str.getChars(偏移量,偏移量+计数,buf,0);
已同步(锁定){
写入(基本单位,0,基本单位长度);
}
}
如果
str
null
,则
str.length()
将因
NullPointerException
而失败


我认为您需要的是
outputStreamWriter.write(total.toString()),而不是
outputStreamWriter.write(行)

我们有一个以下循环:

while ((line = r.readLine()) != null) {
    total.append(line);
}
因此,很明显,
line
在它的末尾等于
null
。现在,让我们来看看实现。它调用三参数版本的
write()
函数,如下所示:

public void write(String str, int offset, int count) throws IOException {
    if ((offset | count) < 0 || offset > str.length() - count) {
        throw new StringIndexOutOfBoundsException(str, offset, count);
    }
    char[] buf = new char[count];
    str.getChars(offset, offset + count, buf, 0);
    synchronized (lock) {
        write(buf, 0, buf.length);
    }
}
public void write(字符串str、int offset、int count)引发IOException{
如果((偏移量|计数)<0 | |偏移量>str.length()-count){
抛出新的StringIndexOutOfBoundsException(str、offset、count);
}
char[]buf=新字符[计数];
str.getChars(偏移量,偏移量+计数,buf,0);
已同步(锁定){
写入(基本单位,0,基本单位长度);
}
}
如果
str
null
,则
str.length()
将因
NullPointerException
而失败

我认为您需要的是
outputStreamWriter.write(total.toString()),而不是
outputStreamWriter.write(行)

更改此项:

OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                    myContext.openFileOutput("settings.json", Context.MODE_PRIVATE));
            outputStreamWriter.write(line);
            outputStreamWriter.close();

更改此项:

OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                    myContext.openFileOutput("settings.json", Context.MODE_PRIVATE));
            outputStreamWriter.write(line);
            outputStreamWriter.close();


MainActivity.java
中第370行的内容是什么?对不起,我忘了!它是outputStreamWriter.write(行);这基本上意味着outputstreamwriter的构造函数返回null。我想如果您指定的文件(settings.json)丢失,可能会发生这种情况。也许在尝试此操作时放置了文件的完整路径?我在assets文件夹中有这个settings.json文件。或者你的确切意思是什么?问题是,我在代码的另一个地方有相同的代码,它在那里工作…MainActivity.java中第370行的内容是什么?对不起,我忘了!它是outputStreamWriter.write(行);这基本上意味着outputstreamwriter的构造函数返回null。我想如果您指定的文件(settings.json)丢失,可能会发生这种情况。也许在尝试此操作时放置了文件的完整路径?我在assets文件夹中有这个settings.json文件。或者你的确切意思是什么?问题是,我在我的代码的另一个地方有相同的代码,它在那里工作…太棒了!谢谢我的错误。使用total.toString()它成功了!令人惊叹的!谢谢我的错误。使用total.toString()它成功了!