Java 写入ArrayList时出现空指针异常
我有这段代码在原始文件夹中的txt文件中写入一个双倍数字,另一个代码读取文件并将所有双倍数字放入数组中。我使用数组对文件中的双倍数字进行平均。(双倍是我的ratingbar double) 在读取之后,在按钮OnClickListener中调用write (有人能编辑一下吗,我通常不会用英语写,所以我肯定它会有一些错误,即使是在这段引文中) 写:Java 写入ArrayList时出现空指针异常,java,android,Java,Android,我有这段代码在原始文件夹中的txt文件中写入一个双倍数字,另一个代码读取文件并将所有双倍数字放入数组中。我使用数组对文件中的双倍数字进行平均。(双倍是我的ratingbar double) 在读取之后,在按钮OnClickListener中调用write (有人能编辑一下吗,我通常不会用英语写,所以我肯定它会有一些错误,即使是在这段引文中) 写: private void writeMyArray(double rate){ //PrintWriter out = new Pri
private void writeMyArray(double rate){
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("arraymedia.txt", true)));
//.println(rate);
double ratex2 = rate * 2;
int erate = (int)ratex2;
try
{
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("myarray.txt", Context.MODE_APPEND));
outputStreamWriter.append(Integer.toString(erate));
outputStreamWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
阅读:
按钮点击代码
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ratingbar.setEnabled(false);
rating = ratingbar.getRating();
writeMyArray(rating);
readMyArray(arraydays);
button.setText(getText(R.string.obrigado) + "!" + media(arraydays));
//button.setText(getText(R.string.obrigado)+"!");
button.setEnabled(false);
}
});
正面
当我更改array.set()时,它给了我以下错误:
07-03 10:25:53.576 25334-25334/com.example.emilio.notification E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.emilio.notification, PID: 25334
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.set(ArrayList.java:481)
at com.example.emilio.notification.MainActivity.readMyArray(MainActivity.java:210)
at com.example.emilio.notification.MainActivity.access$200(MainActivity.java:45)
at com.example.emilio.notification.MainActivity$2.onClick(MainActivity.java:111)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
代码似乎很好,请尝试替换
InputStream inputStream = this.getResources().openRawResource(R.raw.myarray);
到
应该适合您。问题出在您的while循环中
while(receiveString = bufferedReader.readLine()) != null)
这很好,因为它检查null
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
// The next line is the problem
// bufferedReader.readline() is either null
// or array is null
// bufferedReader.readLine() is going to be set to null on the last run.
array.set(enc, Double.parseDouble(bufferedReader.readLine()));
++enc;
}
但在循环中,您将再次获得bufferedReader.readLine()
这意味着,当循环位于最后一个字符串上时,它不是null
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
// The next line is the problem
// bufferedReader.readline() is either null
// or array is null
// bufferedReader.readLine() is going to be set to null on the last run.
array.set(enc, Double.parseDouble(bufferedReader.readLine()));
++enc;
}
代码将拾取下一个字符串
,该字符串现在等于null
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
// The next line is the problem
// bufferedReader.readline() is either null
// or array is null
// bufferedReader.readLine() is going to be set to null on the last run.
array.set(enc, Double.parseDouble(bufferedReader.readLine()));
++enc;
}
我想你可能在找这个
array.set(enc, Double.parseDouble(receiveString));
或者可能是这个
string doubleToParse = bufferedReader.readLine();
if(doubleToParse != null) {
array.set(enc, doubleToParse);
}
注:
对于第二个答案,每次通过循环时,您将从bufferedReader
中提取两行代码
您可能面临的下一个问题是设置ArrayList
确保你已经打过电话了
ArrayList<Double> arrayDays = new ArrayList<Double>();
最后,您必须确定正在解析的每个字符串实际上都包含一个Double
否则您将面临java.lang.NumberFormatException
try {
Double d = Double.parseDouble(doubleToParse);
array.add(d);
} catch (NumberFormatException ex) {
System.out.println(ex.getMessage());
}
当前代码有什么问题?是否已在readMyArray上初始化数组导致此错误arrayDays
似乎未初始化。请确保正在调用ArrayList arrayDays=new ArrayList()代码>我还没有这么做,但我的硬件不是很好,当gradle完成后,我会告诉你这是否有效。更新了我的答案。您应该使用array.add()
,因为array.set()
会更改已经存在的Double
。但它似乎还没有人居住。谢谢你,伙计!现在我有另一个问题,在我写的代码中,我忘记将“myarray.txt”更改为原始文件夹文件,我如何在那里强制转换R.raw.myarray?因为这是一个不同的问题,我将打开一个新问题。在搜索并确保没有重复的问题后,当然:)这里有一个链接,可以让您开始学习。:)在Write()方法中,更符合逻辑的是:outputStreamWriter.append(“\n”+Integer.toString(rate));而不是这个outputStreamWriter.append(Integer.toString(rate));正确的?
ArrayList<Double> arrayDays = new ArrayList<Double>();
array.add(Double.parseDouble(doubleToParse));
try {
Double d = Double.parseDouble(doubleToParse);
array.add(d);
} catch (NumberFormatException ex) {
System.out.println(ex.getMessage());
}