我正在尝试用逗号替换文件中的字符(和),这样我就可以使用下面的代码从csv文件中将数据输入到JTable
。我试过通过用StringTokenizer
读取文件来尝试这样做,并且我试图操纵我实现它的方式,但是我不能让它与NullPointerException
一起运行。我明白它找不到它所指向的对象,但我无法看到我的问题所在。任何指针都会很棒,因为我已经坚持了几个小时。是使用StringTokenizer
阅读的最佳方法还是有更好的方法?产生的错误如下。
java.lang.NullPointerException at initial.DisplayTableModel.fileImport(DisplayTableModel.java:29) at initial.DisplayTableModel.(DisplayTableModel.java:15) at initial.Display.(Display.java:15) at initial.Display.main(Display.java:27) Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero at initial.DisplayTableModel.getRowCount(DisplayTableModel.java:85) at javax.swing.JTable.getRowCount(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.createTableSize(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at javax.swing.ScrollPaneLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at java.awt.window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.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)
此模型还包含其他两个类,但在替换括号部分时,它们不会返回错误。
package initial;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
@SuppressWarnings("serial")
public class DisplayTableModel extends AbstractTableModel {
protected Vector<String> data;
protected Vector<String> columnNames;
protected String datafile;
public DisplayTableModel(String f) {
datafile = f;
fileImport();
}
public void fileImport() {
String aLine;
data = new Vector<String>();
columnNames = new Vector<String>();
try {
FileInputStream fin = new FileInputStream(datafile);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
aLine = br.readLine();
String strReplace = ")";
br.readLine().replaceAll(strReplace, ",");
StringTokenizer Yearquote = new StringTokenizer(aLine, ")");
columnNames.addElement(Yearquote.nextToken());
StringTokenizer st1 = new StringTokenizer(br.readLine(), ",");
while (st1.hasMoreTokens()) {
columnNames.addElement(Yearquote.nextToken());
}
// extract data
while (aLine != null) {
if (aLine.startsWith("\"")) {
StringTokenizer addquote = new StringTokenizer(aLine, "\"");
data.addElement(addquote.nextToken());
StringTokenizer st2 = new StringTokenizer(addquote
.nextToken(), ",");
while (st2.hasMoreTokens()) {
data.addElement(st2.nextToken());
}
} else {
StringTokenizer st2 = new StringTokenizer(aLine, ",");
while (st2.hasMoreTokens()) {
data.addElement(st2.nextToken());
}
}
}
br.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public int getRowCount() {
return data.size() / getColumnCount();
}
public int getColumnCount() {
return columnNames.size();
}
public String getColumnName(int columnIndex) {
String colName = "";
if (columnIndex <= getColumnCount()) {
colName = columnNames.elementAt(columnIndex);
}
return colName;
}
public Class<String> getColumnClass(int columnIndex) {
return String.class;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data.elementAt((rowIndex * getColumnCount()) + columnIndex);
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
return;
}
}
答案 0 :(得分:1)
行号(堆栈跟踪中的第29行)是一个很大的提示。最有可能的是,br.readline().replaceAll()
导致空指针,因为您连续两次调用br.readline()
而没有检查流中是否有内容,第二次调用可能是在您耗尽了流之后。 / p>
答案 1 :(得分:1)
您还应该小心return data.size() / getColumnCount();
行。您可能最终除以0,从以下异常行判断,这似乎是您的情况:
Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero
at initial.DisplayTableModel.getRowCount(DisplayTableModel.java:85)
at javax.swing.JTable.getRowCount(Unknown Source)
答案 2 :(得分:0)
包装你的代码
while ((aLine = br.readLine()) != null) {
aLine.replaceAll(strReplace, ",");
//logic here
}