我看到有人问这个,但那是两年前的事了,他们一直没有得到答案。我正试图运行《java game development with libgdx》一书中的一个示例程序,但我遇到了一个奇怪的错误。错误如下:
java.lang.ClassNotFoundException: Rock
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:377)
at com.mygdx.game.BaseActor.getList(BaseActor.java:319)
at com.mygdx.game.StarfishCollector.update(StarfishCollector.java:35)
at com.mygdx.game.GameBeta.render(GameBeta.java:24)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:232)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)
Exception in thread "LWJGL Application" java.lang.NullPointerException: Cannot invoke "java.lang.Class.isInstance(Object)" because "theClass" is null
at com.mygdx.game.BaseActor.getList(BaseActor.java:326)
at com.mygdx.game.StarfishCollector.update(StarfishCollector.java:35)
at com.mygdx.game.GameBeta.render(GameBeta.java:24)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:232)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)
11:45:50 PM: Task execution finished 'DesktopLauncher.main()'.
我正在使用intellij,源代码可以找到:https://github.com/apress/java-game-dev-libgdx/blob/master/ch03%20starfish%20collector/baseactor.java 还有https://github.com/apress/java-game-dev-libgdx/blob/master/ch03%20starfish%20collector/starfishcollector.java
该方法的代码为:
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)
{ error.printStackTrace(); }
for (Actor a : stage.getActors())
{
if ( theClass.isInstance( a ) )
list.add( (BaseActor)a );
}
return list;
}
和
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);
}
}
似乎找不到类名rock,所以类变量保持为null并导致错误。我希望有人能找到解决办法,否则这是一本好书。
谢谢
暂无答案!
目前还没有任何答案,快来回答吧!