gpt4 book ai didi

java - 尝试替换文件中的字符时出现空指针异常

转载 作者:行者123 更新时间:2023-12-02 08:00:06 26 4
gpt4 key购买 nike

我正在尝试用逗号替换文件中的字符 ( 和 ),以便我可以使用下面的代码将数据从 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)

There are two other classes included to this model but they do not return an error when the replace bracket section is commented out.

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;
}

}

最佳答案

行号(堆栈跟踪中的第 29 行)是一个重要提示。最有可能的是,当您连续调用 br.readline().replaceAll() 两次而不检查是否有内容时,br.readline().replaceAll() 会导致空指针在流中,第二次调用可能是在耗尽流之后。

关于java - 尝试替换文件中的字符时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9025513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com