【发布时间】:2014-08-24 23:36:56
【问题描述】:
我正在尝试逐行读取文件并将其保存到字节数组中,但由于某种原因 String.getBytes() 会引发 Nullpointer 异常。
我做错了什么?
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
byte[][] bytes = null;
try {
String data;
int i = 0;
while((data = br.readLine()) != null) {
bytes[i] = data.getBytes(); // THROWS A NULLPOINTER EXCEPTION HERE
i++;
}
System.out.println(bytes.length);
} catch (IOException e)
e.printStackTrace();
}
}
【问题讨论】:
-
byte[][] bytes = null;-- 你从来没有真正创建过数组。 -
YemSalat - 你在使用像 eclipse 这样的 IDE 吗?
-
@BoratSagdiyev,完全正确,它没有抛出异常。它仅在我尝试运行实际程序时出现。
标签: java string byte bufferedreader