sixstring
sixstring

Reputation: 31

Java Game Development with Libgdx Book Chapter 3 ClassNotFoundException

I saw someone else asked this but that was over two years ago and they never got an answer. I am trying to run a sample program in the book Java Game Development with LibGDX but I am getting an odd error. Here is the error:

java.lang.ClassNotFoundException: Rock
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:377)
at com.mygdx.game.BaseActor.getList(BaseActor.java:319)
at com.mygdx.game.StarfishCollector.update(StarfishCollector.java:35)
at com.mygdx.game.GameBeta.render(GameBeta.java:24)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:232)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)
Exception in thread "LWJGL Application" java.lang.NullPointerException: Cannot invoke "java.lang.Class.isInstance(Object)" because "theClass" is null
at com.mygdx.game.BaseActor.getList(BaseActor.java:326)
at com.mygdx.game.StarfishCollector.update(StarfishCollector.java:35)
at com.mygdx.game.GameBeta.render(GameBeta.java:24)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:232)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)
11:45:50 PM: Task execution finished 'DesktopLauncher.main()'.

I am using intelliJ and the source code can be found: https://github.com/Apress/java-game-dev-LibGDX/blob/master/Ch03%20Starfish%20Collector/BaseActor.java and also https://github.com/Apress/java-game-dev-LibGDX/blob/master/Ch03%20Starfish%20Collector/StarfishCollector.java

The code for the method is:

public static ArrayList<BaseActor> getList(Stage stage, String className)
{
    ArrayList<BaseActor> list = new ArrayList<BaseActor>();
    
    Class theClass = null;
    try
    {  theClass = Class.forName(className);  }
    catch (Exception error)
    {  error.printStackTrace();  }
    
    for (Actor a : stage.getActors())
    {
        if ( theClass.isInstance( a ) )
            list.add( (BaseActor)a );
    }

    return list;
}

and

    new Starfish(400,400, mainStage);
    new Starfish(500,100, mainStage);
    new Starfish(100,450, mainStage);
    new Starfish(200,250, mainStage);
    
    new Rock(200,150, mainStage);
    new Rock(100,300, mainStage);
    new Rock(300,350, mainStage);
    new Rock(450,200, mainStage);
    
    turtle = new Turtle(20,20, mainStage);
    
    win = false;
}

public void update(float dt)
{
    for (BaseActor rockActor : BaseActor.getList(mainStage, "Rock"))
        turtle.preventOverlap(rockActor);

    for (BaseActor starfishActor : BaseActor.getList(mainStage, "Starfish"))
    {
        Starfish starfish = (Starfish)starfishActor;
        if ( turtle.overlaps(starfish) && !starfish.collected )
        {
            starfish.collected = true;
            starfish.clearActions();
            starfish.addAction( Actions.fadeOut(1) );
            starfish.addAction( Actions.after( Actions.removeActor() ) );

            Whirlpool whirl = new Whirlpool(0,0, mainStage);
            whirl.centerAtActor( starfish );
            whirl.setOpacity(0.25f);
        }
    }

It seems like it can't find the class name rock, so theClass variable stays null and causes an error. I was hoping someone had a solution because otherwise its a great book.

Thanks

Upvotes: 0

Views: 233

Answers (1)

Xombie404
Xombie404

Reputation: 11

This worked for me you have to include the package and not just the class name

theClass = Class.forName("com.mygdx.game." + className);

Upvotes: 1

Related Questions