7

I am not finding any error in this class, but Netbeans continuously showing red symbol on that class. Class is

/*
 * 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;
    }
}

The code is compiling but getting 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 Individual class is being used:

ArrayList<Individual> pop = new ArrayList<Individual>();

This where Individual class is being intantiated:

Individual temp = new Individual(STRING_SIZE);
pop.add(temp);

EDIT

I haven't renamed the file manually. All the coding was done in Netbeans. The only problem is when I am creating instance of Individual.

EDIT2

I have copied the project another place, everything is normal again. probably a bug of Netbeans or JDK

8
  • stackoverflow.com/questions/100107/…
    – stacker
    Dec 20, 2011 at 17:03
  • 6
    The current code seems correct, the problem might be in the usage of the class Individual. How do you create an instance of it?
    – WoLfulus
    Dec 20, 2011 at 17:05
  • @stacker: didn't understand a bit. I am e newbie
    – user
    Dec 20, 2011 at 17:07
  • I don't see any error in Eclipse when I create a project with this class.
    – CoolBeans
    Dec 20, 2011 at 17:07
  • 4
    This seems to be a Netbeans specific quirk when having duplicate classes of the same signature in different packages: stackoverflow.com/questions/6560988/… Probably you renamed old classes manually by file explorer instead of using IDE refactoring functions. Cleansweep your project.
    – BalusC
    Dec 20, 2011 at 17:14

5 Answers 5

5

You should javap the .class file and check whether the compiler generated a call to super() near the start of your constructor.

The JVM verifier requires that any constructor (other than for Object, of course) invoke (possibly indirectly via another constructor) it's superclass's constructor. Normally the compiler inserts a call to the superclass constructor automatically if you don't do it, but it's possible that it could be confused into not doing this under some circumstances (though the code presented doesn't seem that complex).

(And, yes, you have this and ind swapped in most places in copy.)

4

This is probably not the problem, but your copy() method is totally messed up... Instead of copying anything, it actually just resets the original object, and returns a new empty one. If you wanted it to create copies of the 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();
1
  • Glad to help, but if this fixed your problem, it is by chance :P Check Hot Licks answer for a better explanation.
    – SuperTron
    Dec 20, 2011 at 17:49
1

I had the same problem in Netbeans. Clean and then Build the project again solved it for me.

0

Hi I have the same experience with NetBeans. I was pretty mad about it, but the solution is quite easy. You have to copy create new projects and the same classes as in the not-functional one. Then copy all the texts from the classes in old project to new project and don't forget to change package name if it isn't the same. Your work will then run :)

0

java.lang.VerifyError can be the result when you have compiled against a different library than you are using at runtime.

For example, this happened to me when trying to run a program that was compiled against Xerces 1, but Xerces 2 was found on the classpath. The required classes (in org.apache.* namespace) were found at runtime, so ClassNotFoundException was not the result. There had been changes to the classes and methods, so that the method signatures found at runtime did not match what was there at compile-time.

Normally, the compiler will flag problems where method signatures do not match. The JVM will verify the bytecode again when the class is loaded, and throws VerifyError when the bytecode is trying to do something that should not be allowed -- e.g. calling a method that returns String and then stores that return value in a field that holds a List. This is what I got, But still I am unable to correct it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.