简体   繁体   English

尝试停止重复J内部形式时的空指针异常

[英]Null pointer exception when trying to stop repeating J internal form

I wanted to stop repeating J internal form by clicking the label in Home J Frame. 我想通过单击Home J Frame中的标签来停止重复J内部表单。 And I'm trying to add "<<" to the label after I load the form. 加载表单后,我试图在标签上添加“ <<”。 Now, I'm getting null pointer exception when I'm trying to load the second J internal form. 现在,当我尝试加载第二个J内部表单时,我得到了空指针异常。 This is my method to load J internal form: 这是我加载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();
        }
    } 

I'm Using moveToFront() method to move the selected J internal form. 我正在使用moveToFront()方法移动选定的J内部表单。

These are the cords to label mouse releasing events: 这些是标记鼠标释放事件的绳索:

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);

    }   

Stack trace is given below. 堆栈跟踪如下。

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)

And viewer.MainFrame.load(MainFrame.java:39) is if (jI.getName().equals(ss)) { at viewer.MainFrame.lblinvoiceMouseReleased(MainFrame.java:186) is load("Invoice", new Invoice(), lblinvoice); 并且viewer.MainFrame.load(MainFrame.java:39)if (jI.getName().equals(ss)) { at viewer.MainFrame.lblinvoiceMouseReleased(MainFrame.java:186)load("Invoice", new Invoice(), lblinvoice);

I suspect the NPE might be in this block: 我怀疑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;
    } 
 } 

Are you sure you're setting the name of the JInternalFrame objects when you create them? 您确定要在创建JInternalFrame对象时设置它们的名称吗? This would explain why the first time you don't get NPE but the second time you do. 这可以解释为什么第一次没有NPE但第二次却没有。 If not so, please post the NPE's stack trace. 如果不是这样,请发布NPE的堆栈跟踪。

Update : based on stack trace recently added, the problem is the line I pointed above. 更新 :基于最近添加的堆栈跟踪,问题是我上面指出的那行。 So, you should make following changes to avoid 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);

}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM