gpt4 book ai didi

java - Jtable java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 23:04:28 25 4
gpt4 key购买 nike

我不确定为什么 update_table() 给我 java.lang.NullPointerException...需要一些灯..我可以建立与 jdbc 的连接..当我注释掉 update_table() 方法时,一切都像魅力一样。

public class UserMenu extends JFrame {
private JPanel contentPane;
private JTable userTable;

Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;


private void Update_table (){

try {
String sql = "select * from login";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery(sql);
userTable.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e) {
// TODO Auto-generated catch block
//JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}
}

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserMenu frame = new UserMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public UserMenu() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
//initComponents();
try{
conn = JavaConnect.ConnecrDb();
Update_table();
}catch(NullPointerException npe)
{
//JOptionPane.showMessageDialog(null, e);
npe.printStackTrace();
}


JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(10, 11, 89, 23);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MainMenu mm = new MainMenu();
mm.setVisible(true);
mm.setExtendedState( mm.getExtendedState()|JFrame.MAXIMIZED_BOTH );
MainMenu.CurrentDate();
dispose();
}
});
contentPane.setLayout(null);
contentPane.add(btnNewButton);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(51, 107, 293, 102);
contentPane.add(scrollPane);

userTable = new JTable();
scrollPane.setViewportView(userTable);
userTable.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
},
new String[] {
"id", "uname", "passwd"
}
));
}
}

这是错误

java.lang.NullPointerException
at UserMenu.Update_table(UserMenu.java:35)
at UserMenu.<init>(UserMenu.java:75)
at UserMenu$1.run(UserMenu.java:52)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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)

最佳答案

您在初始化该方法中使用的 userTable = new JTable(); 之前调用 Update_table();

关于java - Jtable java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23007715/

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