Jtable java.lang.NullPointerException

时间:2014-04-11 08:46:54

标签: java swing nullpointerexception jtable runtime-error

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

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)

1 个答案:

答案 0 :(得分:3)

在初始化方法

中使用的Update_table();之前,您正在调用userTable = new JTable();