Flying Nimbus
Flying Nimbus

Reputation: 109

Class Not Found Exception on Class.forName()

I started reading "Java Game Development with LibGDX" and I'm building a game, which is explained step by step in chapter 3 of the book. The player controls a turtle, avoiding rocks and captures starfish. I'm using intellij (instead of blueJ, recommend by the book) and java8.

To explain the problem. The BaseActor Class has a method "ArrayList getList" which creates a list for each type of actor(which can be found in the StarfishCollecor, which are 4 Rocks actors and 4 Starfish actors. The output should be 2 actor lists. The Rock list is created no problem, but the starfish list generates a error: java.lang.ClassNotFoundException: Starfish

Can I do this different? Yes, but this code will be reused later in the book, so I like to solve the problem using this method.

I looked around the internet and I found a post (Class Not Found Exception on Class.forName()), asking the same question I have and the person seems to have found the solution.

S/he says the For each loop was getting out of bounds for the Starfish class and did a little null checking. But I don't understand how the For each loop gets out of bound or what s/he means with null checking. Please check the code in the post. And if you can explain the solution in a way a simple beginner like me can underhand, you would help be out a lot.

game file hierarchy visual

Lastly I put a prinln in the getList method to check how the method is building the list. The method is checking for actors in the MainStage and only grabbing the ones with the name "Rock" and later again for "Starfish". At the end the action is repeating, but I dont understand why that is. If feel this is were thinks get out of hand.

BaseActor index: BaseActor || list size: 0 || BaseActor list: []

BaseActor index: Starfish || list size: 0 || BaseActor list: []

BaseActor index: Starfish || list size: 0 || BaseActor list: []

BaseActor index: Starfish || list size: 0 || BaseActor list: []

BaseActor index: Starfish || list size: 0 || BaseActor list: []

BaseActor index: Rock || list size: 1 || BaseActor list: [Rock]

BaseActor index: Rock || list size: 2 || BaseActor list: [Rock, Rock]

BaseActor index: Rock || list size: 3 || BaseActor list: [Rock, Rock, Rock]

BaseActor index: Rock || list size: 4 || BaseActor list: [Rock, Rock, Rock, Rock]

BaseActor index: Turtle || list size: 4 || BaseActor list: [Rock, Rock, Rock, Rock]

BaseActor index: BaseActor || list size: 0 || BaseActor list: []

BaseActor index: Starfish || list size: 1 || BaseActor list: [Starfish]

BaseActor index: Starfish || list size: 2 || BaseActor list: [Starfish, Starfish]

BaseActor index: Starfish || list size: 3 || BaseActor list: [Starfish, Starfish, Starfish]

BaseActor index: Starfish || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]

BaseActor index: Rock || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]

BaseActor index: Rock || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]

BaseActor index: Rock || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]

BaseActor index: Rock || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]

BaseActor index: Turtle || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]

Upvotes: 1

Views: 574

Answers (1)

Tobias
Tobias

Reputation: 2575

The exception you get is a ClassNotFoundException. That indicates that the class Starfish could not be found. Since there is no class called Starfish in the git repo, you provided, this seems to be the problem.

When you call the Class.forName method, it tries to find a class that's name equals the parameter of the method (in this case "Starfish") and if there is no such class, this will fail with a ClassNotFoundException.

A solution would be to add the class Starfish from the source code repository that @Abra mentioned in the comments.

Upvotes: 1

Related Questions