来自另一个 JFrame 的 JTable 值
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用的 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.