Java Stage.draw LibGDX上出现空指针异常
我整个学期都在使用LibGDX,对它抛出的错误有很好的理解,但我无法理解这一点 我正在使用ScrollPane类,它需要一个字符串[]来显示,所以我正在使用一个方法,从一堆已经赢得的“游戏”(不是LibGDX游戏)对象中提取字符串,并将它们放入字符串[],但是当我运行此代码时,它会在有stage.draw()的行上给我一个空指针 当我在数组中放入一个字符串并运行代码时,它工作得很好,所以它一定是我的方法,但我不明白为什么它会这样做。我能够在分配字符串之前按索引打印整个字符串[] 下面是我的代码和错误:Java Stage.draw LibGDX上出现空指针异常,java,nullpointerexception,libgdx,draw,stage,Java,Nullpointerexception,Libgdx,Draw,Stage,我整个学期都在使用LibGDX,对它抛出的错误有很好的理解,但我无法理解这一点 我正在使用ScrollPane类,它需要一个字符串[]来显示,所以我正在使用一个方法,从一堆已经赢得的“游戏”(不是LibGDX游戏)对象中提取字符串,并将它们放入字符串[],但是当我运行此代码时,它会在有stage.draw()的行上给我一个空指针 当我在数组中放入一个字符串并运行代码时,它工作得很好,所以它一定是我的方法,但我不明白为什么它会这样做。我能够在分配字符串之前按索引打印整个字符串[] 下面是我的代码和
@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;
}
错误:
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)
谢谢你的帮助 您的数组可能比要放入其中的数据长。
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)