0

这完美运行:

package sledmonitor;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class GetList3 {

    private Connection conn;
    private String ConnectURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=." + File.separator + "db" + File.separator + "sledprod.mdb";
    private String user = "";
    private String pw = "";
    String offer;


    GetList3(String sql){
    try{

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
        conn = DriverManager.getConnection(ConnectURL, user, pw);
        Statement stmt = conn.createStatement();
        System.out.println(sql);
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
            offer = rs.getString("offernumber");
            System.out.println(offer);
        }
        rs.close();
        stmt.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        if(conn!=null){
            try{
                conn.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    }
}

现在我想在 JTable 中显示结果。所以我做了这个,通常应该做什么:

while(rs.next()){
            offer = rs.getString("offernumber");
            List.tablemodel.addRow(new String[]{offer});
        }

但这会以某种方式引发 NullPointerException:

SELECT o.offernumber, o.TotalMPL
FROM sled_offers o left join sled_changes c on o.offerguid = c.objectid
WHERE (c.nextsequencenr is null or c.nextsequencenr=99999) AND TotalMPL='0'
AND (o.offerstate=2 or o.offerstate=4 or o.offerstate=5)
java.lang.NullPointerException
    at sledmonitor.GetList3.<init>(GetList3.java:28)
    at sledmonitor.GetList.<init>(GetList.java:45)
    at sledmonitor.Monitor$4.mouseClicked(Monitor.java:99)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6508)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

类 List 如下所示:

package sledmonitor;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class List extends JFrame {

    private JPanel contentPane;
    private JTable table;
    static DefaultTableModel tablemodel;

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

    /**
     * Create the frame.
     */
    public List() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        tablemodel = new DefaultTableModel();
        tablemodel.addColumn("OfferNumber");

        table = new JTable(tablemodel);
        scrollPane.setViewportView(table);
    }

}

任何想法?

4

2 回答 2

0

您似乎在List.tablemodel没有首先实例化List对象的情况下访问静态字段,因此您没有调用List()不会实例化tablemodel对象的调用。

然后,您应该使用静态构造函数:

static {
    tablemodel = new DefaultTableModel();
    tablemodel.addColumn("OfferNumber");
}

List()并从构造函数中删除这两行代码。

于 2012-10-24T15:16:03.787 回答
0

用这个

DefaultTableModel myData = new DefaultTableModel();
  JTable table = new JTable(myData);

// 使用模型类型构造函数初始化表

while(rs.next()){
       String offer = rs.getString("offernumber");
        Object [] rowData=new Object[]{offer,null};
       myData.addRow(rowData);
    }

//使用defaulttablemodel类的addRow(Object[] rowData)方法

于 2012-10-24T18:28:45.603 回答