我有以下java代码来读取CSV文件并将值存储在ArrayList()中。
try {
File file =new File("Data.csv");
BufferedReader br=new BufferedReader(new FileReader(file));
while(br.readLine() != null){
String input=br.readLine();
input=input.replace('"', '\0');
datam.add(input.split(",|\\s|;"));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Exception");
e.printStackTrace();
}
但我在以下几行获得例外
String input=br.readLine();
input=input.replace('"', '\0');
datam.add(input.split(",|\\s|;"));
我发现代码中没有错误。我正在使用Arraylist来填充JTable。 异常消息如下:
java.lang.NullPointerException
at com.cs319.MyTable.<init>(Lab2Swing.java:366)
at com.cs319.Lab2Swing.createPanel3(Lab2Swing.java:190)
at com.cs319.Lab2Swing.<init>(Lab2Swing.java:103)
at com.cs319.Lab2Swing$1.run(Lab2Swing.java:75)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:4)
您调用readLine()
方法两次:一次在if
子句中,而不使用返回值,然后在if
块的主体中,而不检查它是否为{ {1}}。使用变量存储null
的结果,检查此变量是否为readLine()
,然后访问它。