Java 初始化错误中的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3375810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:42:06  来源:igfitidea点击:

exception in initializer error

javabindingpack

提问by Adam Outler

I am using Netbeans. I did some things with bindings and now whenever I start my program, before it even intializes the form, it gives me an error

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

The exception in thread main is ocuring before the form is even an intialized object yet. The form is not even an object yet. Every line in my main() causes an exception. Random stuff. I don't understand it at all.

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

Here is the error.

这是错误。

    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 line 731 is bindingGroup.bind(); sometimes it errors out on pack();

OBD2nerForm 第 731 行是 bindingGroup.bind(); 有时它会在 pack() 上出错;

the exception in "main" does not even seem relevant because it occurs as soon as the program is run and every time I comment out a line it jumps to the next

“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>       

please help, i do not understand. What information do you need from me?

请帮忙,我不明白。你需要我提供什么信息?

edit: it seems to all be code which I cannot touch. I should probly add that this started with netbeans adding about 200 invalid imports "import jCheckbox1" which I deleted.

编辑:似乎都是我无法触摸的代码。我可能应该补充一点,这是从 netbeans 添加了大约 200 个我删除的无效导入“import jCheckbox1”开始的。

采纳答案by Christian Ullenboom

You get an ExceptionInInitializerError if something goes wrong in the static initializer block.

如果静态初始化程序块中出现问题,您将收到 ExceptionInInitializerError 。

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

Because static variables are initialized in static blocks there are a source of these errors too. An example:

因为静态变量是在静态块中初始化的,所以也有这些错误的来源。一个例子:

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

=>

=>

class C
{
  static int v;

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

So if foo() goes wild, you get a ExceptionInInitializerError.

所以如果 foo() 变得疯狂,你会得到一个 ExceptionInInitializerError。

回答by Stephen C

@Christian Ullenboom' explanation is correct.

@Christian Ullenboom 的解释是正确的。

I'm surmising that the OBD2nerFormcode you posted is a static initializer block and that it is all generated. Based on that and on the stack trace, it seems likely that generated code is tripping up because it has found some component of your form that doesn't have the type that it is expecting.

我猜测OBD2nerForm您发布的代码是一个静态初始化程序块,并且都是生成的。基于此和堆栈跟踪,生成的代码似乎有可能出错,因为它发现表单的某些组件不具有它期望的类型。

I'd do the following to try and diagnose this:

我会执行以下操作来尝试诊断此问题:

  • Google for reports of similar problems with NetBeans generated forms.
  • If you are running an old version of NetBeans, scan through the "bugs fixed" pages for more recent releases. Or just upgrade try a newer release anyway to see if that fixes the problem.
  • Try cutting bits out of the form design until the problem "goes away" ... and try to figure out what the real cause is that way.
  • Run the application under a debugger to figure out what is being (incorrectly) type cast as what. Just knowing the class names may help. And looking at the instance variables of the objects may reveal more; e.g. which specific form component is causing the problem.
  • 谷歌搜索有关 NetBeans 生成的表单的类似问题的报告。
  • 如果您运行的是旧版本的 NetBeans,请浏览“已修复的错误”页面以获取更新的版本。或者只是升级尝试更新的版本,看看是否可以解决问题。
  • 尝试从表单设计中删除一些内容,直到问题“消失”……并尝试找出真正的原因是什么。
  • 在调试器下运行应用程序以找出正在(错误地)类型转换为什么的内容。只知道类名可能会有所帮助。查看对象的实例变量可能会发现更多;例如,哪个特定的表单组件导致了问题。

My suspicion is that the root cause is a combination of something a bit unusual (or incorrect) with your form design, and bugs in the NetBeans form generator that is not coping with your form. If you can figure it out, a workaround may reveal itself.

我怀疑根本原因是您的表单设计有点不寻常(或不正确)的组合,以及 NetBeans 表单生成器中无法处理您的表单的错误。如果你能弄清楚,一个解决方法可能会显露出来。

回答by Adam Outler

I found that I had bound jFormattedCheckBox1.foreground to jCheckBox1[${selected}].... this was the problem. Thank you for your help.

我发现我已经将 jFormattedCheckBox1.foreground 绑定到 jCheckBox1[${selected}].... 这就是问题所在。感谢您的帮助。

It seems that a color should not be able to be bound to a boolean. I guess bindings are an "advance feature"?

似乎颜色不应该绑定到布尔值。我猜绑定是“高级功能”?

I found the problem by deleting all of the controls, then running, then undoing and then deleting one at a time. When I found the offending control, i examined the properties.

我通过删除所有控件,然后运行,然后撤消然后一次删除一个来发现问题。当我发现有问题的控件时,我检查了属性。

回答by Utsav

Hope this helps...

希望这可以帮助...

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

Code snippet 1: Absolutely OK - all checked exceptions handled

代码片段 1:绝对没问题 - 处理了所有已检查的异常

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

Code snippet 2: Won't compile - unreported checked exception

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

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

Code snippet 3: OK (see code snippet 1)

代码片段 3:OK(参见代码片段 1)

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

Code snippet 4: Compilation error, initilalizer must be able to complete normally

代码片段4:编译错误,initilalizer必须能正常完成

static{
        throw new RuntimeException();
}

Basically it boils down to this:

基本上它归结为:

  1. Inside the static block, every checked exception MUST have a handler.
  2. If a RuntimeException were to occur, it would be wrapped in ExceptionInInitializerError and then the latter would be thrown.
  1. 在静态块内,每个检查的异常都必须有一个处理程序。
  2. 如果发生 RuntimeException,它将被包装在 ExceptionInInitializerError 中,然后后者将被抛出。

This makes sense as A CLASS SHOULD BE ABLE TO COMPLETE INITIALIZATION NORMALLY. If this happens to be a problem, this should be categorized as an Error (from which recovery is usually difficult or impossible) rather than an Exception (which is usually recoverable)...

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

回答by Amir Fo

Wherever there is errors or exceptions in static blocks, this exception will be thrown. To get the cause of this exception simply use Throwable.getCause()to know what is wrong.

只要静态块中出现错误或异常,就会抛出这个异常。要获得此异常的原因,只需使用Throwable.getCause()即可知道出了什么问题。

回答by CoolMind

Retrofithave recently updated to 2.7.1 version. After that Android 4.xclients have crashed. See https://stackoverflow.com/a/60071876/2914140.

Retrofit最近更新到 2.7.1 版本。在那之后,Android 4.x客户端崩溃了。请参阅https://stackoverflow.com/a/60071876/2914140

Downgrade Retrofitto 2.6.4.

降级Retrofit到 2.6.4。