gpt4 book ai didi

java - 调用方法时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 14:09:48 27 4
gpt4 key购买 nike

调用方法 fillDBWeek(String mName) 时出现 NullPointerException。我无法发现我哪里出了问题,有人可以帮助我吗?

这是我在使用以下方法时遇到的错误

*Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at View.LectureReport.fillDBWeek(LectureReport.java:49)
at View.LectureReport$2.actionPerformed(LectureReport.java:96)*

方法fillDBWeek

public void fillDBWeek(String mName)
{
tableModel.setDataVector(lRHand.popuSDataWeek(mName), columnNames);
table.setModel(tableModel);
table.repaint();
}

按下按钮时调用该方法的位置

JButton btnViewReport = new JButton("View Report");
btnViewReport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String moduleName = (String)comboBox.getSelectedItem();
if(comboBox_1.getSelectedItem().equals("Week"))
{
fillDBWeek(moduleName);
}
else if (comboBox_1.getSelectedItem().equals("Semester"))
{
fillDBSem(moduleName);
}
else if (comboBox_1.getSelectedItem().equals("Year"))
{
fillDBYear(moduleName);
}
}
});

查询用于调用SQL数据库中的数据

public Object[][] popuSDataWeek(String moduleName)
{
data = new Object[30][9];

try{
Class.forName(DRIVER_CLASS);

connection = DriverManager.getConnection( url,"polk", "mire");

statement = connection.createStatement();
results = statement.executeQuery("SELECT * FROM Group6_StudAttWeek WHERE module = '"+ moduleName +"'");
int i=0;
while(results.next())
{
data[i][0]= results.getString("firstname");
data[i][1]= results.getString("lastname");
data[i][2]= results.getString("module");
data[i][3] = results.getString("yearOfStudy");
data[i][4]= results.getInt("workshop%");
data[i][5]= results.getInt("tutorial%");
data[i][6] = results.getInt("lecture%");
data[i][7] = results.getInt("avg%");
data[i][8] = results.getInt("number");
i++;
}
results.close();
statement.close();
connection.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
System.exit(1);
}catch(Exception exception) {
System.err.println("An error happened");
System.exit(1);
}

return data;
}

最佳答案

基于堆栈:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at View.LectureReport.fillDBWeek(LectureReport.java:49)
at View.LectureReport$2.actionPerformed(LectureReport.java:96)

fillDBWeek 中出现异常,因为 tableModeltable 为 null。如果异常是由 popuSDataWeek 引发的,那么它应该出现在堆栈跟踪中,但无论如何,我看到的唯一可能的 null 对象是 connection

请确保在使用之前声明并初始化了正确的对象。

关于java - 调用方法时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13706014/

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