来自另一个 JFrame 的 JTable 值

发布于 2024-12-01 15:10:57 字数 3853 浏览 0 评论 0原文

我有 JTable 并且我想在另一个框架中的特定单元格设置值。当我尝试这样做时,出现空指针异常。我更改了修饰符并删除了异常,但仍然无法设置该值。

请大家帮忙

public ChequeVoucher(int id,int row){
       try {
           String str = get_bank_name(id);
           System.out.println(str); 

           table.getModel().setValueAt(str,row,5);
           table.getModel().setValueAt(id,row,4);
       } catch (SQLException f) {
       }
       setBank(id,row); 
    }

public void setBank(int id,int row){
    try {
            String str = get_bank_name(id);
            table.getModel().setValueAt(str,row,5);
        } catch (SQLException f) {
        }
    }

chequevoucher 是我的构造函数,这段代码很好,直到

 table.getModel().setValueAt(str,row,5);

我从中获取值的 JFrame

   table.addMouseListener(new MouseAdapter() {
       public void mouseClicked(MouseEvent e) {
         int row = table.getSelectedRow();
         int col = table.getSelectedColumn();
         String str_id = (String)GetID(row,0); 
         int id = Integer.parseInt(str_id);
         ChequeVoucher CV = new ChequeVoucher(id,row); 
         setVisible(false);
        }
     });

访问修饰符不是静态的,所以我得到以下错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at client.ChequeVoucher.(ChequeVoucher.java:215) 
at client.popupBanks$3.mouseClicked(popupBanks.java:130) 
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253) 
at java.awt.Component.processMouseEvent(Component.java:6292) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) 
at java.awt.Component.processEvent(Component.java:6054) 
at java.awt.Container.processEvent(Container.java:2041) 
at java.awt.Component.dispatchEventImpl(Component.java:4652) 
at java.awt.Container.dispatchEventImpl(Container.java:2099) 
at java.awt.Component.dispatchEvent(Component.java:4482) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
at java.awt.Container.dispatchEventImpl(Container.java:2085) 
at java.awt.Window.dispatchEventImpl(Window.java:2478) 
at java.awt.Component.dispatchEvent(Component.java:4482) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644) 
at java.awt.EventQueue.access$000(EventQueue.java:85) 
at java.awt.EventQueue$1.run(EventQueue.java:603) 
at java.awt.EventQueue$1.run(EventQueue.java:601) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) 
at java.awt.EventQueue$2.run(EventQueue.java:617) 
at java.awt.EventQueue$2.run(EventQueue.java:615) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:614) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

堆栈修饰符是静态的,我没有遇到任何异常,但没有任何反应,而且我无法再收听 JTable

 case KeyEvent.VK_F5:
     switch(col){
         case 4:
         //show the banks 
         popupBanks pB = new popupBanks(row); 
         pB.setVisible(true);

         break;
  }

抱歉,之前的帖子不清楚

I have JTable and I want to set value at specific cell from another frame. When I try to do it, I get null pointer exception. I changed the modifiers and removed the exception, but still can't set the value.

Please guys help

public ChequeVoucher(int id,int row){
       try {
           String str = get_bank_name(id);
           System.out.println(str); 

           table.getModel().setValueAt(str,row,5);
           table.getModel().setValueAt(id,row,4);
       } catch (SQLException f) {
       }
       setBank(id,row); 
    }

public void setBank(int id,int row){
    try {
            String str = get_bank_name(id);
            table.getModel().setValueAt(str,row,5);
        } catch (SQLException f) {
        }
    }

The chequevoucher is my constructor and this piece of code is good until this

 table.getModel().setValueAt(str,row,5);

and the JFrame I get the value from is

   table.addMouseListener(new MouseAdapter() {
       public void mouseClicked(MouseEvent e) {
         int row = table.getSelectedRow();
         int col = table.getSelectedColumn();
         String str_id = (String)GetID(row,0); 
         int id = Integer.parseInt(str_id);
         ChequeVoucher CV = new ChequeVoucher(id,row); 
         setVisible(false);
        }
     });

The access modifiers wasn't static so I get the following error stack

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at client.ChequeVoucher.(ChequeVoucher.java:215) 
at client.popupBanks$3.mouseClicked(popupBanks.java:130) 
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253) 
at java.awt.Component.processMouseEvent(Component.java:6292) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) 
at java.awt.Component.processEvent(Component.java:6054) 
at java.awt.Container.processEvent(Container.java:2041) 
at java.awt.Component.dispatchEventImpl(Component.java:4652) 
at java.awt.Container.dispatchEventImpl(Container.java:2099) 
at java.awt.Component.dispatchEvent(Component.java:4482) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
at java.awt.Container.dispatchEventImpl(Container.java:2085) 
at java.awt.Window.dispatchEventImpl(Window.java:2478) 
at java.awt.Component.dispatchEvent(Component.java:4482) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644) 
at java.awt.EventQueue.access$000(EventQueue.java:85) 
at java.awt.EventQueue$1.run(EventQueue.java:603) 
at java.awt.EventQueue$1.run(EventQueue.java:601) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) 
at java.awt.EventQueue$2.run(EventQueue.java:617) 
at java.awt.EventQueue$2.run(EventQueue.java:615) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:614) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

When I made the modifiers static, I get no exceptions but nothing happens and I can't listen to the JTable anymore

 case KeyEvent.VK_F5:
     switch(col){
         case 4:
         //show the banks 
         popupBanks pB = new popupBanks(row); 
         pB.setVisible(true);

         break;
  }

Sorry for the previous unclear post

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-12-08 15:10:57

您调用的 ChequeVoucher 方法绝对是一个构造函数,此时,除非您用其他内容指定其值,否则您的表为 null。您可以在构造函数中添加表来解决问题。
最好的解决方案是使用 JTable 作为构造函数参数创建一个 ChequeVoucher,并使用方法执行其他所有操作。

The ChequeVoucher method you call is definitely a constructor and at this point, your table is null unless you specify its value with something else. You can add the table in your constructor which will solve the problem.
The best solution would be creating a ChequeVoucher with your JTable as constructor parameter and doing everything else with methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文