0

I would like to create a simulation containing an entity. There can be two kinds of entities, complex and simple ones. When I instantiate a simple simulation, I want the simple entity to be instantiated, and when I instantiate a complex simulation I want the complex entity to be instantiated.

Entities:

class ComplexEntity extends Entity {
    public ComplexEntity(){}
}

class SimpleEntity extends Entity  {
    public SimpleEntity(){}
}

class Entity {
    public Entity(){}
}

Simulations:

class ComplexSimulation extends Simulation<ComplexEntity>
{

    public ComplexSimulation() throws InstantiationException, IllegalAccessException {
        super(ComplexEntity.class);
    }

}

class SimpleSimulation extends Simulation<SimpleEntity>
{
    public SimpleSimulation() throws InstantiationException, IllegalAccessException
    {
        super(SimpleEntity.class);
    }
}

class Simulation<E extends Entity> {
    protected final E entity;

    public Simulation(Class<E> class1) throws InstantiationException, IllegalAccessException 
    {
        entity = class1.newInstance();
    }
}

The problem is that when I try to construct a ComplexSimulation:

ComplexSimulation c = new ComplexSimulation();

I get the following InstantiationException:

java.lang.InstantiationException: test.Test$ComplexEntity
at java.lang.Class.newInstance(Unknown Source)
at test.Test$Simulation.<init>(Test.java:55)
at test.Test$ComplexSimulation.<init>(Test.java:37)
at test.Test.go(Test.java:12)
at test.Test.main(Test.java:6)
Caused by: java.lang.NoSuchMethodException: test.Test$ComplexEntity.<init>()
at java.lang.Class.getConstructor0(Unknown Source)

The zero argument constructor cannot be the problem because my entities have them... Does anybody know what the problem can be?

7
  • 1
    The exception should be telling you what is wrong. Post the stacktrace. Commented Oct 5, 2015 at 16:26
  • I added the stacktrace Commented Oct 5, 2015 at 16:27
  • You are using a generic class without erasure, the generic type doesn't now the actual type. Commented Oct 5, 2015 at 16:27
  • The problem is that you're using inner classes. Move each class into a proper top class or mark them static. Commented Oct 5, 2015 at 16:27
  • Are all those classes inner classes? Commented Oct 5, 2015 at 16:28

1 Answer 1

3

The problem is that you're using inner classes. You cannot create an instance of an inner class without an instance of the outer class, not even if you call Class<InnerClass>.newInstance().

Just make these inner classes to be top classes and your example should work as expected.

If you really want/need to initialize non-static inner classes using reflection, see here: How to instantiate inner class with reflection in Java?

Sign up to request clarification or add additional context in comments.

5 Comments

this is also not the case
Yes it is the case nikpon! Thanks Luiggi, your solution solved my problem.
@nikpon yes it is. Just look at the stacktrace.
It there a way to make invoking newInstance() (or similar), on an inner class work?
@Bohemian yes, this is covered in other Q/A so I've posted the link here in my answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.