我正在尝试使用[0]元素和[1]元素创建一个Arraylist字符串,其他元素加倍。我想在我的其他类中使用这些数组作为我的坐标。
我得到一个NullPointerException,我做错了什么?
在这里输入代码
public class ExampleTextInArrayReader
{
//naam document van ECG-patiént 1
public static final String FILE_NAME = "ecg1.txt";
//array waarin de String van ECG-waarden is omgezet in een ArrayMetDoubles
private Double[] arrayMetDoubles = new Double[722-2];
private String[] lines, gegevensPatiënt;
//velden voor patientnummer en de bijbehorende afdeling
public void main()
{
// Open the file
TextInArrayReader in = new TextInArrayReader(FILE_NAME);
// Check for errors during opening file
if (in.getError() != null)
{
System.out.println("Error opening file: " + in.getError());
System.exit(1);
}
// Read the file
String[] lines = in.getLines();
if (in.getError() != null) // check for errors during reading file
{
System.out.println("Error reading file: " + in.getError());
System.exit(1);
}
// Print file contents
for (int i = 0; i < lines.length; i++) System.out.println(lines[i]);
}
public String[] drukGegevensPatiëntAf()
{
gegevensPatiënt = new String[2];
for(int i = 0; i < 2; i++)
{
gegevensPatiënt[i] = lines[i];
}
return gegevensPatiënt;
}
public void changeArray()
{
// Get the ECG-values from the lines array and change them in doubles and put them in an arrayMetDoubles array
arrayMetDoubles = new Double[lines.length - 2];
for(int i = 2; i < arrayMetDoubles.length; i++)
{
arrayMetDoubles[i] = Double.parseDouble(lines[i + 2]);
}
}
public Double [] getLijnen()
{
changeArray();
return arrayMetDoubles;
}
}
答案 0 :(得分:1)
因为类成员的变量行尚未分配。在Main方法中你已经确定了局部变量
String[] lines = in.getLines();
将以上行更改为
lines = in.getLines();
在Main()方法中。
确保在changeArray方法之前调用Main()方法。
答案 1 :(得分:0)
我喜欢你在整个代码中将Nederlands与英语混合在一起! :)
无论如何,在for循环中,从0循环到长度为2 ......否则代码会将索引2中的ArraymetDoubles填充到数组外的两个位置
public void changeArray()
{
arrayMetDoubles = new Double[lines.length - 2];
for(int i = 0; i < arrayMetDoubles.length; i++)
{
arrayMetDoubles[i] = Double.parseDouble(lines[i + 2]);
}
}