0

So the question title is a little weird but it is the only way I could think of the get the point across. I have a NullPointerException from doing this (The extra code is taken out):

public abstract class Generator extends SimpleApplication{

    private static SimpleApplication sa;

    public static void Generator(){
        CubesTestAssets.initializeEnvironment(sa);
    }

I know that the private static SimpleApplication sa; is null but when I instantiate it I get this error instead of a NullPointerException :

SimpleApplication is abstract; cannot be instantiated

I am using the jMonkey Engine 3. If anyone knows how I could solve this problem, that would be great! Thanks!

JGeo
  • 380
  • 13
  • 33
1Poseidon3
  • 125
  • 1
  • 11
  • 6
    As error clearly says, you can't instantiate an abstract class. Period. :) – Pradeep Simha Apr 01 '14 at 15:30
  • 2
    You need to find (or write) a concrete specialization of `SimpleApplication` and instantiate that – Nick Holt Apr 01 '14 at 15:32
  • I still need help. So far none of the solutions have worked AND @Pradee Simha Sorry but your wrong. I know it can be done I just don't know how. – 1Poseidon3 Apr 01 '14 at 16:53
  • @1Poseidon3 In reply to some of your comments: `Generator` **is** a `SimpleApplication`. Any method that wants a `SimpleApplication` will be very happy with a `Generator`. (although of course you can't instantiated `Generator` because it is also `abstract`. Why is it abstract?) – Richard Tingle Apr 02 '14 at 10:40
  • This is much like how a `CarFerry` wants a `Car` as input, but it'll very happily take a `HondaAccord` – Richard Tingle Apr 02 '14 at 10:42
  • @Richard Tingle Because they have to be. I don't remember why. I believe it has to do with the fact that all my stuff in the Generator class needs to be static. – 1Poseidon3 Apr 03 '14 at 13:46

2 Answers2

3

If you take a close look at the JMonkey documentation, SimpleApplication is made to be extended by you to create your application.

You should create a new class that extends SimpleApplication and implement the missing methods. You then instantiate your custom class and pass it as a parameter.

A little like this :

public class myCustomSimpleApplication extends SimpleApplication {

    // Implementing the missing methods
    @Override
    public void simpleInitApp() {
    }

    @Override
    public void simpleUpdate(float tpf) {
    }

    // etc...

}

And then...

private static SimpleApplication sa = new myCustomSimpleApplication();

public static void Generator(){
    CubesTestAssets.initializeEnvironment(sa);
}
Gladhus
  • 910
  • 1
  • 13
  • 24
  • the problem is that `CubesTestAssets.initializeEnvironment(sa);` MUST be filled with `SimpleApplication` SPECIFICALLY. It will not take it any other way. – 1Poseidon3 Apr 01 '14 at 15:36
  • 1
    Read about [polymorphism](http://www.tutorialspoint.com/java/java_polymorphism.htm). A child of a class can be passed as said class. – Gladhus Apr 01 '14 at 15:37
  • @1Poseidon3 If it's declared something like `void initializeEnvironment(SimpleApplication x)`, then the parameter may be of **any** class that is a subclass of `SimpleApplication`. – ajb Apr 01 '14 at 15:48
  • Here, I edited so that it is a little more of an answer. – Gladhus Apr 01 '14 at 17:01
1

As others have commented, your base application class (I assume it is Generator) which extends SimpleApplication must not be abstract, i.e. it should implement, at least, the simpleInitApp method. See the JMonkey documentation for SimpleApplication or this basic example.

For completion, I will mention that if you really needed to create a dummy instance of an abstract class, you can do so "inline", without explicitly writing the full implementing class. You just need to write the implementation of the abstract methods enclosed by curly brackets after the constructor invocation:

public abstract class AbstractClass {

    private static AbstractClass a = new AbstractClass() {
        @Override
        public void abstract_method() {/*implementation*/}
    };

    public abstract void abstract_method();

}

An anonymous class is created behind the scenes. But I definitely don't think this is what you need in this case.

yyhr
  • 71
  • 1
  • 4