使用sql db数据填充jtable时的nullpointerexception

时间:2014-01-20 18:51:49

标签: java sql database jtable

当我尝试使用数据库中的数据填充名为ctab的jtable时,程序会抛出一个nullpointer异常。这是程序代码:

package client;

import net.proteanit.sql.DbUtils;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.awt.EventQueue;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Cursor;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;
import client.AddCon;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JList;
import javax.swing.table.TableColumnModel;

public class MAINMENU {

private JFrame frmKusinaNiKambal;

private JTextField searchbar;
    private Connection conn;
    private JTable ctab;
    private ResultSet rs;

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

/**
 * Create the application.
 */
public MAINMENU() {
         try{
//Load database driver and connect

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/KusinaNiKambal","root","1234");
    JOptionPane.showMessageDialog(null, "Database Synchronization Successful.\nQuerying may now commence");
         }
            catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
    initialize();
}

    private void poptab(){
    try{
        String sql = "select * from kusinanikambal.cis";
        PreparedStatement pst = conn.prepareStatement(sql);
        rs = pst.executeQuery(sql);
        ctab.setModel(DbUtils.resultSetToTableModel(rs));
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    }
/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    frmKusinaNiKambal = new JFrame();
    frmKusinaNiKambal.setTitle("Kusina ni Kambal - Client Information System");
    frmKusinaNiKambal.setResizable(false);
    frmKusinaNiKambal.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    frmKusinaNiKambal.setBounds(100, 100, 800, 500);
    frmKusinaNiKambal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmKusinaNiKambal.getContentPane().setLayout(null);
    poptab();
            try{
//Load database driver and connect
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/KusinaNiKambal","root","1234");}
            catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
    JButton AddCon = new JButton("Add");
    AddCon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            client.AddCon.main(null);
        }
    });
    AddCon.setFont(new Font("Arial", Font.BOLD, 17));
    AddCon.setBounds(629, 97, 155, 40);
    frmKusinaNiKambal.getContentPane().add(AddCon);

    JButton ViewCon = new JButton("View");
    ViewCon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

                    }
    });
    ViewCon.setFont(new Font("Arial", Font.BOLD, 17));
    ViewCon.setBounds(629, 199, 155, 40);
    frmKusinaNiKambal.getContentPane().add(ViewCon);

    JLabel lblWelcomeToThe = new JLabel("Welcome to the Kusina ni Kambal Client     Information System!");
    lblWelcomeToThe.setFont(new Font("Arial", Font.PLAIN, 17));
    lblWelcomeToThe.setBounds(133, 11, 443, 14);
    frmKusinaNiKambal.getContentPane().add(lblWelcomeToThe);

    JButton EditCon = new JButton("Edit");
            EditCon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                            client.EditCon.main(null);
                    }
            });
            EditCon.setFont(new Font("Arial", Font.BOLD, 17));
    EditCon.setBounds(629, 148, 155, 40);
    frmKusinaNiKambal.getContentPane().add(EditCon);


    JButton btnNewButton_1 = new JButton("Archive");
    btnNewButton_1.setFont(new Font("Arial", Font.BOLD, 17));
    btnNewButton_1.setBounds(629, 250, 155, 40);
    frmKusinaNiKambal.getContentPane().add(btnNewButton_1);

            String [] columnNames = {"Contract #","Engager","Contact #",
                "Client Address","Event Date","Time","Event Address",
                "Contract FilePath","Referred By"};
            DefaultTableModel model = new DefaultTableModel();
            model.setColumnIdentifiers(columnNames);

    ctab = new JTable();
    ctab.setBounds(26, 97, 580, 347);
    frmKusinaNiKambal.getContentPane().add(ctab);
            ctab.setModel(model);

    searchbar = new JTextField();
    searchbar.setBounds(26, 54, 361, 20);
    frmKusinaNiKambal.getContentPane().add(searchbar);
    searchbar.setColumns(10);

    JComboBox filter = new JComboBox();
    filter.setBounds(397, 54, 115, 20);
    frmKusinaNiKambal.getContentPane().add(filter);

    JButton btnSearch = new JButton("Search");
    btnSearch.setBounds(522, 53, 89, 23);
    frmKusinaNiKambal.getContentPane().add(btnSearch);
}
}

这里是模式kusinanikambal下名为cis的表的sql脚本

create database kusinanikambal;
create table cis(
contno varchar(12) not null,
engager varchar(30) not null,
contactno varchar(12) not null,
claddr varchar(100) not null,
evdate date default 0000-00-00,
evtime time,
contract varchar(200),
referrer varchar(30)
);

这是staktrace:

java.lang.NullPointerException
at client.MAINMENU.poptab(MAINMENU.java:79)
at client.MAINMENU.initialize(MAINMENU.java:102)
at client.MAINMENU.<init>(MAINMENU.java:70)
at client.MAINMENU$1.run(MAINMENU.java:47)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at             java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

1 个答案:

答案 0 :(得分:1)

你可能想要改变

Connection conn = DriverManager.getConnection(...

在你的构造函数中,

this.conn = DriverManager.getConnection(...

目前,您正在使用同名的局部变量为实例变量着色。因此,您的实例变量仍为null

我没有完成所有代码,但似乎你在很多地方都这样做了。纠正这些。