2

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

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Seder
  • 2,735
  • 2
  • 22
  • 43
  • look here http://stackoverflow.com/questions/7053283/sending-messages-between-two-jpanel-objects , answer by @Hovercraft Full Of Eels, that would be similair issue for me – mKorbel Aug 24 '11 at 07:32
  • @mKorbel thanks but this is not what I am looking for because I think my main problem with the JTable – Seder Aug 24 '11 at 07:50
  • then change MouseListener to the http://download.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html – mKorbel Aug 24 '11 at 08:08
  • Please don't swallow exceptions. – trashgod Aug 24 '11 at 09:52
  • which is line 215? why do think being static or not has anything to do with your problem? – kleopatra Aug 24 '11 at 09:55
  • line 215 :table.getModel().setValueAt(str,row,5); and changing it to static stops the exception – Seder Aug 24 '11 at 10:04
  • 3
    then something is wrong in the code you'r not showing - your setup is wrong. Step back and learn about objects, classes and accessor scope.. – kleopatra Aug 24 '11 at 10:51
  • I don't know were is the wrong setup and still have the problem and I've traced the App the problem from setValueAt – Seder Aug 24 '11 at 11:20

1 Answers1

1

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.

belgther
  • 2,544
  • 17
  • 15