Question

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!

Was it helpful?

Solution

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);
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top