我想通过单击Home J Frame中的标签来停止重复J内部表单。
加载表单后,我试图在标签上添加“ <这是我加载J内部表单的方法:
public void lordForm(JInternalFrame frame) {
jDesktopPane1.add(frame);
frame.setVisible(true);
}
public void load(String ss, JInternalFrame in, JLabel jl) {
JInternalFrame[] j = jDesktopPane1.getAllFrames();
boolean b = false;
for (JInternalFrame jI : j) {
if (jI.getName().equals(ss)) {
b = true;
break;
}
}
if (!b) {
LF(in);
jl.setText(ss + " >>");
jl.setEnabled(false);
} else {
in.moveToFront();
}
}
我正在使用moveToFront()方法移动选定的J内部表单。
这些是标记鼠标释放事件的绳索:
private void lblinvoiceMouseReleased(java.awt.event.MouseEvent evt) <br/> {
load("Invoice", new Invoice(), lblinvoice);
}
private void lblReservationMouseReleased(java.awt.event.MouseEvent evt){
load("Reservation", new Reservation(), lblReservation);
}
堆栈跟踪如下。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at
viewer.MainFrame.load(MainFrame.java:39) at
viewer.MainFrame.lblinvoiceMouseReleased(MainFrame.java:186) at
viewer.MainFrame.access$000(MainFrame.java:18) at
viewer.MainFrame$1.mouseReleased(MainFrame.java:82)
并且
viewer.MainFrame.load(MainFrame.java:39)
是if (jI.getName().equals(ss)) {
,而viewer.MainFrame.lblinvoiceMouseReleased(MainFrame.java:186)
是load("Invoice", new Invoice(), lblinvoice);
最佳答案
我怀疑NPE可能在以下区域中:
for (JInternalFrame jI : j) { // The first time j length == 0, so the loop is not executed
if (jI.getName().equals(ss)) { // In this line, if jI.getName() == null, you'll get NPE
b = true;
break;
}
}
确定创建
JInternalFrame
对象时要设置它们的名称吗?这可以解释为什么第一次没有NPE但第二次却没有。如果不是这样,请发布NPE的堆栈跟踪。更新:基于最近添加的堆栈跟踪,问题是我上面指出的那一行。因此,您应该进行以下更改以避免NPE:
private void lblinvoiceMouseReleased(java.awt.event.MouseEvent evt) {
String name = "Invoice";
Invoice invoice = new Invoice();
invoice.setName(name);
load(name, invoice, lblinvoice);
}
private void lblReservationMouseReleased(java.awt.event.MouseEvent evt){
String name = "Reservation";
Reservation reservation = new Reservation();
reservation.setName(name);
load(name, reservation, lblReservation);
}