使用 Netbeans 时 java 中的初始值设定项中的异常错误

2022-08-31 13:48:59

我正在使用 Netbeans。我对绑定做了一些事情,现在每当我启动程序时,甚至在它初始化表单之前,它都会给我一个错误。

线程 main 中的异常在窗体甚至不是初始化的对象之前发生。形式甚至还不是一个对象。我的每一行都会导致异常。随机的东西。我完全不明白。main()

这是错误

    Exception in thread "main" java.lang.ExceptionInInitializerError
        at obd2ner.main(obd2ner.java:26)
Caused by: java.lang.ClassCastException
        at java.lang.Class.cast(Class.java:2990)
        at org.jdesktop.beansbinding.Binding.convertForward(Binding.java:1312)
        at org.jdesktop.beansbinding.Binding.getSourceValueForTarget(Binding.java:844)
        at org.jdesktop.beansbinding.Binding.refreshUnmanaged(Binding.java:1222)
        at org.jdesktop.beansbinding.Binding.refresh(Binding.java:1207)
        at org.jdesktop.beansbinding.AutoBinding.tryRefreshThenSave(AutoBinding.java:162)
        at org.jdesktop.beansbinding.AutoBinding.bindImpl(AutoBinding.java:199)
        at org.jdesktop.beansbinding.Binding.bindUnmanaged(Binding.java:959)
        at org.jdesktop.beansbinding.Binding.bind(Binding.java:944)
        at org.jdesktop.beansbinding.BindingGroup.bind(BindingGroup.java:143)
        at OBD2nerForm.initComponents(OBD2nerForm.java:731)
        at OBD2nerForm.<init>(OBD2nerForm.java:75)
        at Status.<clinit>(Status.java:41)
        ... 1 more
Java Result: 1

OBD2nerForm行有时出错731bindingGroup.bind();pack();

中的异常似乎甚至不相关,因为它在程序运行后立即发生,每次我注释掉一行时,它都会跳到下一行main()

        public void actionPerformed(ActionEvent evt) {
            jFormattedTextField2ActionPerformed(evt);
        }
    });

    jLabel8.setText("Data In Que:");

    jLabel9.setFont(new Font("DejaVu Sans", 0, 14));
    jLabel9.setText("FFFFFFFFFFFFFFFFFFFF");

    GroupLayout jPanel5Layout = new GroupLayout(jPanel5);
    jPanel5.setLayout(jPanel5Layout);
    jPanel5Layout.setHorizontalGroup(
        jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
        .add(jPanel5Layout.createSequentialGroup()
            .add(jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
                .add(jPanel5Layout.createSequentialGroup()
                    .add(19, 19, 19)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jLabel7)
                        .add(jLabel5)
                        .add(jLabel6))
                    .add(18, 18, 18)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
                        .add(GroupLayout.TRAILING, jFormattedTextField1, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(GroupLayout.TRAILING, jCheckBox1)
                        .add(GroupLayout.TRAILING, jCheckBox11))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jFormattedTextField2, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(jCheckBox12)
                        .add(jCheckBox2))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jFormattedTextField3, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(jCheckBox13)
                        .add(jCheckBox3))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jFormattedTextField4, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(jCheckBox14)
                        .add(jCheckBox4))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jFormattedTextField5, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(jCheckBox15)
                        .add(jCheckBox5))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jFormattedTextField6, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(jCheckBox16)
                        .add(jCheckBox6))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jFormattedTextField7, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(jCheckBox17)
                        .add(jCheckBox7))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jFormattedTextField8, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(jCheckBox18)
                        .add(jCheckBox8))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
                        .add(GroupLayout.TRAILING, jFormattedTextField9, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                        .add(GroupLayout.TRAILING, jCheckBox19)
                        .add(GroupLayout.TRAILING, jCheckBox9))
                    .addPreferredGap(LayoutStyle.RELATED)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
                        .add(jCheckBox20)
                        .add(jCheckBox10)
                        .add(jFormattedTextField10, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)))
                .add(jPanel5Layout.createSequentialGroup()
                    .add(4, 4, 4)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
                        .add(jPanel5Layout.createSequentialGroup()
                            .add(jLabel8)
                            .addPreferredGap(LayoutStyle.RELATED)
                            .add(jLabel9, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE))
                        .add(jSeparator1, GroupLayout.PREFERRED_SIZE, 474, GroupLayout.PREFERRED_SIZE))))
            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .add(GroupLayout.TRAILING, jPanel5Layout.createSequentialGroup()
            .addContainerGap(346, Short.MAX_VALUE)
            .add(jToggleButton3, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    jPanel5Layout.setVerticalGroup(
        jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
        .add(jPanel5Layout.createSequentialGroup()
            .addContainerGap()
            .add(jPanel5Layout.createParallelGroup(GroupLayout.BASELINE)
                .add(jLabel5)
                .add(jFormattedTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(jFormattedTextField10, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(LayoutStyle.RELATED)
            .add(jSeparator1, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(LayoutStyle.RELATED)
            .add(jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
                .add(jCheckBox3)
                .add(jCheckBox1)
                .add(jCheckBox2)
                .add(jCheckBox4)
                .add(jCheckBox5)
                .add(jCheckBox6)
                .add(jCheckBox7)
                .add(jCheckBox8)
                .add(jCheckBox9)
                .add(jLabel6)
                .add(jCheckBox10))
            .addPreferredGap(LayoutStyle.RELATED)
            .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                .add(jPanel5Layout.createParallelGroup(GroupLayout.LEADING)
                    .add(jLabel7)
                    .add(jPanel5Layout.createParallelGroup(GroupLayout.TRAILING)
                        .add(jCheckBox13)
                        .add(jCheckBox12)
                        .add(jCheckBox11)
                        .add(jCheckBox14)
                        .add(jCheckBox15)
                        .add(jCheckBox16)
                        .add(jCheckBox17)
                        .add(jCheckBox18)
                        .add(jCheckBox19)))
                .add(jCheckBox20))
            .addPreferredGap(LayoutStyle.RELATED, 42, Short.MAX_VALUE)
            .add(jPanel5Layout.createParallelGroup(GroupLayout.BASELINE)
                .add(jToggleButton3)
                .add(jLabel8)
                .add(jLabel9))
            .addContainerGap())
    );

    jTabbedPane1.addTab("tab6", jPanel5);

    add(jTabbedPane1, BorderLayout.CENTER);

    bindingGroup.bind();

    pack();
}// </editor-fold>       

帮忙,我不明白。你们需要我从我这里获取什么信息?

编辑:它似乎都是我无法触及的代码。我可能应该补充一点,这始于Netbeans添加了大约200个无效的导入,我删除了这些导入。import jCheckbox1


答案 1

如果静态初始值设定项块中出现问题,您将获得 ExceptionInitializerError。

class C
{
  static
  {
     // if something does wrong -> ExceptionInInitializerError
  }
}

由于静态变量是在静态块中初始化的,因此这些错误也是源头。例如:

class C
{
  static int v = D.foo();
}

=>

class C
{
  static int v;

  static
  {
    v = D.foo();
  }
}

因此,如果 foo() 变得疯狂,你会得到一个 ExceptionInitializerError。


答案 2

希望这有帮助...

class SomeClass{
  //Code snippet here...
}

代码段 1:完全正常 - 处理所有已检查的异常

static void m1(){
        try{
            throw new Exception();
        } catch(Exception e){
            System.out.println(e);
        }
}
static{
        m1();
}

代码段 2:无法编译 - 未报告的已检查异常

static void m1() throws Exception{
        throw new Exception();
}
static{
        m1();
}

代码段 3:正常(请参阅代码段 1)

static void m1() throws Exception{
        throw new Exception();
}
static{
        try{m1();}
        catch(Exception e){
            System.out.println(e);
            //or whatever
        }
}

代码片段4:编译错误,初始化器必须能够正常完成

static{
        throw new RuntimeException();
}

基本上可以归结为:

  1. 在静态块中,每个选中的异常都必须有一个处理程序。
  2. 如果发生运行时异常,它将被包装在 ExceptionInInitializerError 中,然后抛出后者。

这是有道理的,因为类应该能够正常完成初始化。如果这碰巧是一个问题,则应将其归类为错误(通常很难或不可能从中恢复)而不是异常(通常可恢复)...