Search code examples
javanullpointerexceptionabstract-classinstantiationjmonkeyengine

How can I instantiate an abstract class to remove a NullPointerException?


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!


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