1

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]

5
  • Are you doing the same thing they are in their code, calling Class.forName() with a value in a for loop? And do you understand what null is in Java? Commented Nov 6, 2020 at 19:48
  • yes it is all the same. I see null as a placeholder, until a real value can be replaced inside a variable. Commented Nov 6, 2020 at 20:02
  • Based on what that other person said, they apparently were accidentally passing null as the className to the getList() method. But they don't show the code where they did that so I can't tell any more about what they did. Just looking at the code they did post, I would be wary of the advice of whoever wrote the tutorial. It is a fundamental design error to load a Texture in the constructor of an Actor, especially if there are multiple copies of that Actor. Commented Nov 6, 2020 at 20:16
  • I updated my question, with extra information about the project and the error im having. Commented Nov 7, 2020 at 10:16
  • Did you check the book's source code repository? If your code compiles but does not run then you need to check the run configuration. Are you using Maven or Gradle or similar to compile and/or run your java code? Commented Nov 7, 2020 at 10:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.