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


Starfish Class:

package book.ep3;

public class Starfish extends BaseActor
{

boolean collected;

public Starfish(float x, float y, Stage s)
{
super(x,y,s);
Action spin = Actions.rotateBy(30, 1);
setBoundaryPolygon(8);

collected = false;

}

public boolean isCollected() {
return collected;
}

public void collect() {
collected = true;
clearActions();
}
}


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.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();
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.centerAtPosition(400, 300);
youWinMessage.setOpacity(0);
}
}
}


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

• 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. May 23 '20 at 17:29
• 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 May 23 '20 at 17:49
• 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. May 23 '20 at 22:47
• Thank you, will do! May 24 '20 at 19:38

Why use the class name for this? Just pass the class itself:

public static <T> ArrayList<T> getList(Stage stage, Class<T> type) {
List<T> list = new ArrayList<T>();
for (Actor a : stage.getActors()) {
if (type.isInstance(a))
}
return list;
}


Usage: BaseActor.getList(mainStage, Starfish.class);

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

• juanSenna I'm having the same problem but I don't understand how the For each loop is getting out of bounds. or how to fix it. Can you give some more information or share your code? Nov 6 '20 at 17:19

Note the parameter String Rock and the parameter String Starfish

on Android Studio you can just list the package then the class on other IDE you will have to provide full PATH, that is C:/Games/StarfishCollector/Rock

public void update(float dt){

for(BaseActor rockActor : BaseActor.getList(mainStage, "com.starfishcollector3.Rock")){
turtle.preventOverlap(rockActor);
}

for(BaseActor starfishActor : BaseActor.getList(mainStage, "com.starfishcollector3.Starfish")){
Starfish starfish = (Starfish) starfishActor;

if(turtle.overlaps(starfish) &&  !starfish.collected){
starfish.collected = true;

starfish.clearActions();

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

if(BaseActor.count(mainStage, "com.starfishcollector3.Starfish") == 0 && !win){
win = true;
BaseActor youWinMessage = new BaseActor(0, 0, mainStage);