简体   繁体   中英

Constructor must call super() or this() before return in method

I am getting this error:

Exception in thread "Thread-0" java.lang.VerifyError: Constructor must call super() or this() before return in method JGame.Util.KeyboardMap.<init>()V at offset 0
        at JGame.Room.Room.keyboardEventTests(Room.java:81)
        at JGame.Room.Room.run(Room.java:54)
        at java.lang.Thread.run(Thread.java:722)

When my application loads, it calls this method right away (KeyboardMap.map is an empty HashMap).

Here is the Method (Line 54 calls this method this.keyboardEventTests(); ):

protected void keyboardEventTests(){
    for(Map.Entry ap : KeyboardMap.map.entrySet()){ // Line 81
        Mapping mp = (Mapping)ap.getValue();
        if(mp.doing){
            mp.run();
        }
    }
}

And here is the KeyboardMap class.

package JGame.Util;

import java.util.HashMap;
import java.util.Map;

public class KeyboardMap{

    public static Map<String, Mapping> map = new HashMap<>();

    public static void set(String key, Boolean value, Runnable run){
        Mapping mp = new Mapping();
        mp.doing = value;
        mp.run = run;
        KeyboardMap.map.put(key, mp);
    }

    public static Mapping get(String key){
        return KeyboardMap.map.get(key);
    }
}

Why am I getting that error, and how can I get rid of it?

Why am I getting that error, and how can I get rid of it?

The big clue is that this is a VerifyError , not a compilation error. What this means is that the JVM has found a bytecode file in which one of the constructors is not chaining properly. These are (in effect) a malformed bytecodes.

How can that happen?

  • Well it CAN'T happen in a Java class that is (just) compiled in the normal way. The compiler will automatically insert an implicit super() call into any constructor that doesn't explicitly chain.

  • If this is Java code, then either:

    • the class was compiled using broken compiler (unlikely!), or

    • something has tweaked the bytecodes after compilation.

  • If it was some other language, then the first suspect would be the "other language to bytecode" compilation process.

I think you are getting this problem because your unit tests is using a mocking framework, and the mocking framework is using "byte code engineering" to inject something into the classes under test. The code that is doing this has "made a mistake" and the result is bytecodes that won't compile.


This was apparently fixed by a rebuild, but that doesn't contradict this explanation. A rebuild could clear out broken instrumentation code injected by the mocking framework. And next time around, the framework could "get it right".

Just override the default contructor by adding

public KeyboardMap() {
}

to the KeyboardMap class. It will work.

I had the same problem, and it was caused by a very strange thing: In NetBeans, i use editor fold to fold long codes:

// <editor-fold desc="SOME DESCRIPTION">
  ...
  ...
// </editor-fold>

And it appeared that in one of such my folds i had forgotten to write the beginning of fold, like this:

  ...
  ...
// </editor-fold>

Correcting this solved my problem . Strange, because this is just a comment and it's for NetBeans

在我的例子中,在buildTypes下关闭build.gradle中的minify有帮助。

minifyEnabled false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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