exception in initializer error in java when using Netbeans
Asked Answered
B

10

73

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

The exception in thread main is occuring before the form is even an initialized 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.

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

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

        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>       

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 probably add that this started with Netbeans adding about 200 invalid imports import jCheckbox1 which I deleted.

Bromism answered 30/7, 2010 at 22:26 Comment(0)
C
125

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

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.

Cassiecassil answered 30/7, 2010 at 22:26 Comment(4)
I don't understand. It seems to be wrapped up in code which I cannot touch.Bromism
@Adam - I assume you mean the org.jdesktop.beansbinding.* code. You can at least look at that code, set breakpoints in it, single step, etc.Turino
It is not only thrown when an error in the static block occurs. It can happen when Initializing static variables as well.Moffatt
After an ExceptionInInitializerError, the class cannot be used anymore, see monperrus.net/martin/…Cathee
U
13

Hope this helps...

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

Code snippet 1: Absolutely OK - all checked exceptions handled

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

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

Code snippet 3: OK (see code snippet 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

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.

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

Unerring answered 14/3, 2015 at 12:40 Comment(1)
Re "this should be categorized as an Error", Why not unchecked RuntimeException?Ias
T
4

@Christian Ullenboom' explanation is correct.

I'm surmising that the OBD2nerForm code 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.

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.

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.

Turino answered 31/7, 2010 at 1:5 Comment(2)
how did you figure out that the problem was improper casting?Bromism
@Adam - it is amazing what you can figure out by looking carefully at the evidence you already have :-)Turino
B
3

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

It seems that a color should not be able to be bound to a boolean. I guess bindings are an advanced 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.

Bromism answered 31/7, 2010 at 1:11 Comment(0)
W
0

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.

Widener answered 12/7, 2019 at 15:1 Comment(0)
A
0

Retrofit have recently updated to 2.7.1 version. After that Android 4.x clients have crashed. See https://mcmap.net/q/275596/-java-lang-noclassdeffounderror-java-util-objects.

Downgrade Retrofit to 2.6.4.

Atbara answered 7/2, 2020 at 7:22 Comment(0)
H
0

I got same error and it was due to older Lombok version. Check and update your Lombok version, Changes in Lombok

v1.18.4 - Many improvements for lombok's JDK10/11 support.

Hutch answered 29/9, 2020 at 11:24 Comment(0)
L
0

if you are using Mac M1 chip and room lib , try this : In project-level build.gradle, add the following configuration in allprojects

allprojects {
repositories {
    // ...
}

// ADD THE FOLLOWING
configurations.all {
    resolutionStrategy {
        force 'org.xerial:sqlite-jdbc:3.34.0'
    }
}

}

Ref : https://mcmap.net/q/88084/-caused-by-java-lang-exception-no-native-library-is-found-for-os-name-mac-and-os-arch-aarch64-path-org-sqlite-native-mac-aarch64

Lewellen answered 27/12, 2021 at 11:22 Comment(0)
C
0

If you are working on Cucumber framework (Automating testing) below suggestion will work like an arrow.

Info Cukes dependency with IO Cucumber

Remove below dependencies

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-jvm-deps</artifactId>
    <version>1.0.5</version>
    <scope>provided</scope>
</dependency>

Add below dependencies

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.2.2</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>7.2.2</version>
    <scope>test</scope>
</dependency>
<dependency>
Corazoncorban answered 6/5, 2023 at 8:9 Comment(0)
N
-1

Make sure the project does not have any errors. Delete the project from workspace(make the workspace a different directory from the git folder) and import again.

Near answered 30/6, 2020 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.