我在整个学期都和LibGDX一起工作,并且很好地掌握了它所引发的错误,但是我无法理解这一点。
我正在使用ScrollPane类,它接受一个String []来显示,所以我正在使用一种方法从一堆已赢得的“游戏”(Not LibGDX Game)对象中提取字符串并将它们放入String [],但是当我运行这段代码时,它给了我一个包含stage.draw()的行的NullPointer;
当我只是在数组中放置一个字符串并运行代码时,它工作正常,所以它必须是我的方法,但我不明白为什么它会这样做。我可以在分配之前按索引打印整个String []。
以下是我的代码和错误:
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw(); //**this is the line that causes the error**
}
public void show() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
gameNotes = new String[CampusLocation.countAllNotes(game)]; //this returns String[20]
skin = new Skin(Gdx.files.internal("ui/menuSkin.json"),
(new TextureAtlas("ui/atlas.pack")));
table = new Table(skin);
table.setFillParent(true);
// table.debug();
list = new List<String>(skin);
if (!populateWonNotes()) {
list.setItems(new String[] {"No Locations Found"});
System.out.println("returned false");
} else {
list.setItems(gameNotes);
System.out.println("returned true"); //this is what is printing, so method returns true
}
scrollPane = new ScrollPane(list, skin);
table.clear();
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
table.add("NOTES").colspan(3).expandX().spaceBottom(50).row();
table.add().width(table.getWidth() / 3);
table.add().width(table.getWidth() / 3);
table.add().width(table.getWidth() / 3).row();
table.add(scrollPane).uniformX().left().expandY().top().left();
table.add(games).uniformX();
table.add(home).uniformX().bottom().right();
stage.addActor(table);
}
private boolean populateWonNotes() {
int i = 0;
if (game.lstGameContainers != null) {
for (GameContainer gc : game.lstGameContainers) {
if (gc.isWon) { //checks if the game has been won
Array<String> gcNotes = gc.getNotes(); //gets all notes for this game location
for (String s : gcNotes) {
gameNotes[i] = gc.name + ": " + s;
System.out.println("gameNotes[" + i + "] is: " + gameNotes[i]);
i++;
}
} else {
gameNotes[i] = gc.name + " Notes Locked";
System.out.println("gameNotes[" + i + "] is: " + gameNotes[i]);
i++;
}
}
return true;
} else
return false;
}
在populateWonNotes()
中输出print语句gameNotes[0] is: Trabant Notes Locked
gameNotes[1] is: Perkins Notes Locked
gameNotes[2] is: Writing Center Notes Locked
gameNotes[3] is: UDSIS Notes Locked
gameNotes[4] is: Advising Notes Locked
gameNotes[5] is: Little Bob Notes Locked
gameNotes[6] is: Counseling Center Notes Locked
gameNotes[7] is: Dining Halls Notes Locked
gameNotes[8] is: Bus Stop Notes Locked
gameNotes[9] is: Important Dates Notes Locked
错误:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.scenes.scene2d.ui.List.layout(List.java:112)
at com.badlogic.gdx.scenes.scene2d.ui.Widget.validate(Widget.java:88)
at com.badlogic.gdx.scenes.scene2d.ui.List.getPrefWidth(List.java:238)
at com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.getPrefWidth(ScrollPane.java:610)
at com.badlogic.gdx.scenes.scene2d.ui.Value$3.get(Value.java:65)
at com.badlogic.gdx.scenes.scene2d.ui.Table.computeSize(Table.java:793)
at com.badlogic.gdx.scenes.scene2d.ui.Table.layout(Table.java:925)
at com.badlogic.gdx.scenes.scene2d.ui.Table.layout(Table.java:719)
at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.validate(WidgetGroup.java:106)
at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:100)
at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:112)
at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:58)
at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:127)
at edu.udel.patc.screen.NoteListScreen.render(NoteListScreen.java:51)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
感谢您的帮助!