无法在不同的JFrame表单中前进(Logger.getLogger方法)

huangapple go评论62阅读模式
英文:

Can't advance in different Jframe Form (Logger.getLogger method)

问题

我在我的库中添加了rs2xmlderby,一开始它是工作的,但是在为derby添加连接并为我的表添加刷新功能后,JFrame表单无法继续到下一个JFrame表单。

以下是用于转到下一个JFrame的代码,但它不起作用,但没有错误:

int confirmed = JOptionPane.showConfirmDialog(null,
    "确定要继续到库存表吗?",
    "确认按钮消息框",
    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, "已取消");
}       

以下是我希望进入的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, null, ex);
                }
            }
        });
    }
}

我尝试了我所知道的一切,甚至在互联网上搜索了该怎么做,但似乎找不到解决方案。我知道trycatchlogger.getlogger中导致错误,我只是不知道如何修复它 无法在不同的JFrame表单中前进(Logger.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)
英文:

I added rs2xml and derby in my libraries, it was working at the start but after adding the connection for derby and refresh function for my Tables, the JFrame form can't proceed to the next JFrame Form

These are the codes to move to the next JFrame but it's not working but there is no errors

int confirmed = JOptionPane.showConfirmDialog(null,
&quot;Are you sure you want to Continue to Stock Table?&quot;,
&quot;Confirm Button Message Box&quot;,
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, &quot;Cancelled&quot;);
}       

and these are the codes in the JFrame form that I wish to get to but it won't advance to that JFrame form.

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(&quot;SELECT * FROM STOCK&quot;);
        jTable.setModel(DbUtils.resultSetToTableModel(rs));
    }

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

            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, null, ex);
            }
        }
    });
}

I tried everything I knew. Even searched the Internet on what to do but can't seem to find the solution. I know it's the try and catch in logger.getlogger that's causing the error I just don't know how to fix it 无法在不同的JFrame表单中前进(Logger.getLogger方法)

here is the error log

Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.NullPointerException
at activity3.InformationofCafe.update_table(InformationofCafe.java:33)
at activity3.InformationofCafe.&lt;init&gt;(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)

答案1

得分: 1

你的问题与 Swing 无关。我也不明白为什么你会将它标记为 sql-server,因为你声称你正在使用 Derby

你无法连接到数据库,即你代码中的以下行会抛出异常。

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

随后,InformationofCafe 类的 stmt 成员未被初始化,因此为空。

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

rs=stmt.executeQuery("SELECT * FROM STOCK");

我建议你编写一个简单的程序,不带有 GUI,尝试连接到 Derby,然后执行 SQL 查询。一旦你成功实现这一点,你可以修复你 GUI 中的代码。你阅读过 Derby 的文档吗?

你需要考虑一下,如果连接到数据库或从数据库检索数据时出现问题,你希望你的 GUI 程序怎么处理。你不应该只是忽视它。而仅仅显示错误消息实质上是忽视了错误。

英文:

Your question has nothing to do with Swing. And I also don't understand why you tagged it with sql-server since you state that you are using Derby.

You are failing to connect to the database, i.e. the following line of your code is throwing an exception.

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

Subsequently, the stmt member of class InformationofCafe is not initialized and therefore is null.

The way your code handles the database connection error is to display the error message in a JOptionPane. That means your program continues to run and when it gets to the following line of your code, it throws NullPointerException because stmt is null.

rs=stmt.executeQuery(&quot;SELECT * FROM STOCK&quot;);

I suggest you write a simple program, without a GUI that tries to connect to Derby and then executes a SQL query. Once you get that right, you can fix the code in your GUI. Have you read the documentation for Derby?

You need to think about what you want your GUI program to do if there is a problem with connecting to the database or retrieving data from the database. You shouldn't just ignore it. And just displaying the error message is essentially ignoring the error.

huangapple
  • 本文由 发表于 2020年10月2日 02:03:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64160891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定