0
\$\begingroup\$

Edit: Solved, for some reason my For each loop was getting out of bounds for the Starfish class, so I did a little null check and it worked out fine.

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

Edit: Here is the photo of my project, and I was indeed wrong for not putting the whole path to the class I wanted. Now "book.ep3.Rock" seems to work, but strangely Starfish is now having an exception.

Caught message:

    java.lang.ClassNotFoundException: Starfish
    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.BaseActor.count(BaseActor.java:174)
    at book.ep3.StarfishCollector.update(StarfishCollector.java:43)
    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:167)
    at book.ep3.BaseActor.count(BaseActor.java:174)
    at book.ep3.StarfishCollector.update(StarfishCollector.java:43)
    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)

Starfish Class:

package book.ep3;

import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Action; 
import com.badlogic.gdx.scenes.scene2d.actions.Actions;

public class Starfish extends BaseActor 
{    

    boolean collected;


    public Starfish(float x, float y, Stage s)    
    {       
        super(x,y,s);
       loadTexture("starfish.png");
       Action spin = Actions.rotateBy(30, 1);   
       this.addAction( Actions.forever(spin) );   
       setBoundaryPolygon(8);

       collected = false;

    } 

    public boolean isCollected() {
        return collected;
    }

    public void collect() {
        collected = true;
        clearActions();
        addAction(Actions.fadeOut(1));
        addAction(Actions.after(Actions.removeActor()));
    }
}

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, "book.ep3.Rock"))
        turtle.preventOverlap(rockActor);

    for (BaseActor starfishActor : BaseActor.getList(mainStage, "book.ep3.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!

\$\endgroup\$
  • \$\begingroup\$ Presumably you started by searching through the many existing answers about this type of error on StackOverflow? It looks like mismatches between your file structure and the package hierarchy are a common cause for this type of error, so you should update your question to show us your Rock and Starfish classes, what package they're in, and where each of these files sits in your file hierarchy. \$\endgroup\$ – DMGregory May 23 at 17:29
  • \$\begingroup\$ Hi Gregory, thanks for answering, I edited the post and now things are one step forward, strangely tho it doesnt seem to work with the Starfish class \$\endgroup\$ – juanSenna May 23 at 17:49
  • \$\begingroup\$ If you've solved your problem, please post your solution as an Answer below, never as an edit. An edited question with no Answer posts still counts as unanswered as far as the StackExchange systems are concerned. \$\endgroup\$ – DMGregory May 23 at 22:47
  • \$\begingroup\$ Thank you, will do! \$\endgroup\$ – juanSenna May 24 at 19:38
0
\$\begingroup\$

Solved, for some reason my For each loop was getting out of bounds for the Starfish class, so I did a little null check and it worked out fine! The problem in the 5th loop of the Starfish searching, where It resulted in null

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.