无法以其他Jframe格式前进Logger.getLogger方法

问题描述

我在库中添加rs2xmlderby,它在一开始就可以正常工作,但是在为我的表添加了derby连接和刷新功能后,JFrame表单无法继续进行下一个JFrame表单

这些是要移至下一个JFrame代码,但它不起作用,但没有错误

int confirmed = JOptionPane.showConfirmDialog(null,"Are you sure you want to Continue to Stock Table?","Confirm Button Message Box",JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
    try {
        new informationofCafe().setVisible(true);
        this.setVisible(false);
    } catch (sqlException ex) {
        Logger.getLogger(Menu.class.getName()).log(Level.SEVERE,null,ex);
    }
} else {
    JOptionPane.showMessageDialog(Menu.this,"Cancelled");
}       

这些是我希望使用的JFrame格式的代码,但是不会升级JFrame格式。

public class informationofCafe extends javax.swing.JFrame {
    Connection con;
    Statement stmt;
    ResultSet rs;
    int cursorRow = 0;

    public informationofCafe() throws sqlException {
        initComponents();
        DoConnect();
        update_table();
    }

    void update_table() throws sqlException {
        rs=stmt.executeQuery("SELECT * FROM STOCK");
        jTable.setModel(dbutils.resultSetToTableModel(rs));
    }

    public Connection DoConnect() {
        try {
            String host ="jdbc:derby://localhost:1527//Downtown";
            String uName ="miguel";
            String uPass ="miguel";
            con = DriverManager.getConnection(host,uName,uPass);
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE );
            String sql = "SELECT * FROM STOCK";
            rs = stmt.executeQuery(sql);
            rs.next();
            int id_col = rs.getInt("ITEM");
            String id = Integer.toString(id_col);
            String description = rs.getString("DESCRIPTION");
            String amount = rs.getString("AMOUNT");

            TextItem.setText(id);
            TextDescription.setText(description);
            TextAmount.setText(amount);

            update_table();
        }
        catch (sqlException err) {
            JOptionPane.showMessageDialog(informationofCafe.this,err.getMessage());
    }
    return con;
}

public static void main(String args[]) {
    java.awt.EventQueue.invokelater(new Runnable() {
        public void run() {
            try {
                new informationofCafe().setVisible(true);
            } catch (sqlException ex) {
                Logger.getLogger(informationofCafe.class.getName()).log(Level.SEVERE,ex);
            }
        }
    });
}

我尽了一切努力。甚至在Internet上搜索该怎么做,但似乎找不到解决方案。我知道是try中的catchlogger.getlogger引起的错误,我只是不知道如何解决:(

这是错误日志

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at activity3.informationofCafe.update_table(informationofCafe.java:33)
at activity3.informationofCafe.<init>(informationofCafe.java:29)
at activity3.Menu.informationActionPerformed(Menu.java:130)
at activity3.Menu.access$000(Menu.java:23)
at activity3.Menu$1.actionPerformed(Menu.java:62)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setpressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.Lightweightdispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.Lightweightdispatcher.processMouseEvent(Container.java:4525)
at java.awt.Lightweightdispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventdispatchThread.pumpOneEventForFilters(EventdispatchThread.java:201)
at java.awt.EventdispatchThread.pumpEventsForFilter(EventdispatchThread.java:116)
at java.awt.EventdispatchThread.pumpEventsForHierarchy(EventdispatchThread.java:105)
at java.awt.EventdispatchThread.pumpEvents(EventdispatchThread.java:101)
at java.awt.EventdispatchThread.pumpEvents(EventdispatchThread.java:93)
at java.awt.EventdispatchThread.run(EventdispatchThread.java:82)

解决方法

您的问题与 Swing 无关。而且我也不明白为什么要使用 sql-server 对其进行标记,因为您声明自己正在使用Derby

您无法连接到数据库,即代码的以下行引发异常。

con = DriverManager.getConnection(host,uName,uPass);

随后,类stmt的{​​{1}}成员未初始化,因此为空。

代码处理数据库连接错误的方式是在InformationofCafe中显示错误消息。这意味着您的程序将继续运行,并且当到达代码的下一行时,它会抛出JOptionPane,因为NullPointerException为空。

stmt

我建议您编写一个简单的程序,不要使用GUI来尝试连接到Derby,然后执行SQL查询。一旦正确,就可以在GUI中修复代码。您阅读过Derby的文档吗?

如果连接到数据库或从数据库检索数据有问题,则需要考虑GUI程序要做什么。您不应该只是忽略它。而且,仅显示错误消息实质上就是忽略该错误。