读取 Java 中的 CSV 文件不起作用?
Reading CSV file in Java doesn't work?
我有以下 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('"', '[=11=]');
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('"', '[=12=]');
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.run(Lab2Swing.java:75)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
您调用了 readLine()
方法两次:一次是在 if
子句中而不使用 return 值,然后是在 if
块的主体中,没有检查它是否是 null
。用一个变量存储readLine()
的结果,检查这个变量是否为null
,然后访问
我有以下 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('"', '[=11=]');
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('"', '[=12=]');
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.run(Lab2Swing.java:75)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
您调用了 readLine()
方法两次:一次是在 if
子句中而不使用 return 值,然后是在 if
块的主体中,没有检查它是否是 null
。用一个变量存储readLine()
的结果,检查这个变量是否为null
,然后访问