Java.lang.VerifyError: (class: ea / Individual, method: <init> signature: (I) V) The constructor must call super () or this ()
I find no errors in this class, but Netbeans constantly displays a red character in this class. Class
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ea; /** * * @author riyad */ import java.util.Random; import java.util.BitSet; public class Individual { BitSet variable; double x; double fitness; double sharedFitness; final int SIZE; Random generator = new Random(); public Individual(int SIZE) { this.variable = new BitSet(SIZE); this.fitness = 0; this.sharedFitness = 0; this.SIZE = SIZE; for(int i=0; i<SIZE; i++) { if(generator.nextBoolean()) { variable.set(i); } else { variable.clear(i); } } x = Double.parseDouble(variable.toString()); } public Individual copy() { Individual ind = new Individual(SIZE); this.variable = (BitSet) ind.variable.clone(); this.fitness = ind.fitness; this.sharedFitness = ind.sharedFitness; this.x = ind.x; return ind; } public void evaluate() { fitness = x * Math.sin(Math.sqrt(x)); } public boolean getBit(int i) { return variable.get(i); } public BitSet getBitSet() { return variable; } public void setBit(int i) { variable.set(i); } public void clearBit(int i) { variable.clear(i); } public double getFitness() { return fitness; } public double sharedFitness() { return sharedFitness; } public void setSharedFitness(double fitness) { this.sharedFitness = fitness; } public void setFitness(double fitness) { this.fitness = fitness; } }
Compiling the code, but getting a runtime error.
Exception in thread "main" java.lang.VerifyError: (class: ea/Individual, method: <init> signature: (I)V) Constructor must call super() or this()
In another class where an individual class is used:
ArrayList<Individual> pop = new ArrayList<Individual>();
This is when an individual class is intensified:
Individual temp = new Individual(STRING_SIZE); pop.add(temp);
EDIT
I did not rename the file manually. All coding was done in Netbeans. The only problem is creating an Individual instance.
EDIT2
I copied the project to another place, everything is fine again. probably a Netbeans or JDK bug
You should javap
create the .class file and check if the compiler generated a super () call near the start of your constructor.
The JVM verifier requires that any constructor (except for Object, of course) invoke (perhaps indirectly through another constructor) the superclass constructor. Usually, the compiler automatically inserts a call to the constructor of the superclass if you do not, but it is possible that in some cases it could be confused so as not to do it (although the presented code does not seem so complicated).
(And yes, you have this
and ind
in most places in copy
.)
This is probably not a problem, but your copy()
method is completely confused ... Instead of copying something, it just dumps the original object and returns a new empty one. If you want to create copies of an object, you should do something like this:
public Individual copy() { Individual ind = new Individual(SIZE); ind.variable = (BitSet) this.variable.clone(); ind.fitness = this.fitness; ind.sharedFitness = this.sharedFitness; ind.x = this.x; return ind; }
and then call it like this:
Individual newOne = oldOne.copy();
I had the same issue in Netbeans. Clean and then re-create the project, solving it for me.
Hi, I have the same experience with NetBeans. I was very angry at this, but the solution is pretty simple. You need to copy new projects and the same classes as in non-functional. Then copy all the texts from the classes in the old project to the new project and remember to change the package name if it is not the same. Then your work will be done :)
java.lang.VerifyError may be the result when you are compiled against a different library than you use at runtime.
For example, this happened to me when I tried to run a program that was compiled against Xerces 1, but Xerces 2 was found in the classpath. The required classes (in the org.apache. * Namespace) were found at run time, so a ClassNotFoundException was not the result. Changes were made to classes and methods, so that the method signatures found at runtime did not match what was at compile time.
Typically, the compiler will flag problems when the method signatures do not match. The JVM checks the bytecode again when the class loads and throws a VerifyError when the bytecode is trying to do something that should not be allowed - for example. calling a method that returns a String and then stores that return value in a field that contains a list. This is what I got, but still I canβt fix it.