Categories
Game Creation Mastering Development

Class Not Found Exception on Class.forName()

I am having this exception on title. I have been following a LibGDX programming PDF, everything was working perfectly and then this code have been implemented, and it isnt working anymore.
I should add that Rock and Starfish are classes on my project, and there is no typo on class names

Caught message:

java.lang.ClassNotFoundException: Rock
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at book.ep3.BaseActor.getList(BaseActor.java:161)
    at book.ep3.StarfishCollector.update(StarfishCollector.java:34)
    at book.ep3.GameBeta.render(GameBeta.java:22)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:233)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)
Exception in thread "LWJGL Application" java.lang.NullPointerException
    at book.ep3.BaseActor.getList(BaseActor.java:168)
    at book.ep3.StarfishCollector.update(StarfishCollector.java:34)
    at book.ep3.GameBeta.render(GameBeta.java:22)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:233)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)

Main class code:

public class StarfishCollector extends GameBeta {
private Turtle turtle;
private boolean win;

public void initialize() {
    BaseActor ocean = new BaseActor(0, 0, mainStage);
    ocean.loadTexture("water.jpg");
    ocean.setSize(800, 600);
    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);
        }
    }
    if (BaseActor.count(mainStage, "Starfish") == 0 && !win) {
        win = true;
        BaseActor youWinMessage = new BaseActor(0, 0, mainStage);
        youWinMessage.loadTexture("you-win.png");
        youWinMessage.centerAtPosition(400, 300);
        youWinMessage.setOpacity(0);
        youWinMessage.addAction(Actions.delay(1));
        youWinMessage.addAction(Actions.after(Actions.fadeIn(1)));
    }
}
}

Get List function code (this is inside the BaseActor class):

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) {     //THIS IS WHERE THE EXCEPTION IS HAPPENING
        error.printStackTrace();
    }

    for (Actor a : stage.getActors()) {
        if (theClass.isInstance(a))
            list.add((BaseActor) a);
    }
    return list;
}

public static int count(Stage stage, String className) {    
    return getList(stage, className).size(); 
}

So the mainStage and Class names are indeed correct, and the rest of the code is copied from the book. Also, if I comment my For loop where its trying to find the Rock class, It will have the same Exception on the next For loop, while searching for the Starfish class.
Any help?
Thank you so much!

Leave a Reply

Your email address will not be published. Required fields are marked *