Reputation: 13
Im new to stackoverflow, Kryonet and libgdx, too. Here, while learning process of libgdx, I want to make an online rock-paper-scissors game to experience both libgdx kryonet. At first I wanted to implement a chat system to my game to try out Kryonet. The idea is a very simple one; chatbox(textarea), send and join buttons, username and message textfields. When someone clicked on send button, the message will be sent to every client connected to the server. But when i spam the send button, it gives some error types (sometimes 1, sometimes 2 or sometimes another one) and closes the game :
Error Type 1
Exception in thread "LWJGL Application" java.lang.NullPointerException at com.badlogic.gdx.graphics.g2d.GlyphLayout.setText(GlyphLayout.java:144) at com.badlogic.gdx.graphics.g2d.GlyphLayout.setText(GlyphLayout.java:63) at com.badlogic.gdx.scenes.scene2d.ui.TextArea.calculateOffsets(TextArea.java:288) at com.badlogic.gdx.scenes.scene2d.ui.TextField.draw(TextField.java:330) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:123) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:170) at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:119) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:123) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:170) at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:119) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:110) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128) at com.mygdx.game.screens.PlayScreen.render(PlayScreen.java:118) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:233) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)
Error Type 2
Exception in thread "LWJGL Application" java.lang.ArrayIndexOutOfBoundsException: 2980 at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addGlyph(BitmapFontCache.java:408) at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addToCache(BitmapFontCache.java:379) at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addText(BitmapFontCache.java:511) at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addText(BitmapFontCache.java:505) at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addText(BitmapFontCache.java:486) at com.badlogic.gdx.graphics.g2d.BitmapFont.draw(BitmapFont.java:214) at com.badlogic.gdx.scenes.scene2d.ui.TextField.drawText(TextField.java:376) at com.badlogic.gdx.scenes.scene2d.ui.TextField.draw(TextField.java:349) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:123) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:170) at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:119) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:123) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:170) at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:119) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:110) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128) at com.mygdx.game.screens.PlayScreen.render(PlayScreen.java:118) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:233) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)
Error Type 3
Exception in thread "LWJGL Application" java.lang.NullPointerException at com.badlogic.gdx.graphics.g2d.GlyphLayout.setText(GlyphLayout.java:144) at com.badlogic.gdx.graphics.g2d.GlyphLayout.setText(GlyphLayout.java:63) at com.badlogic.gdx.scenes.scene2d.ui.TextArea.calculateOffsets(TextArea.java:288) at com.badlogic.gdx.scenes.scene2d.ui.TextField.draw(TextField.java:330) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:123) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:170) at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:119) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:123) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:170) at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:119) at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:110) at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57) at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128) at com.mygdx.game.screens.PlayScreen.render(PlayScreen.java:118) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:233) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)
the line at com.mygdx.game.screens.PlayScreen.render(PlayScreen.java:118)
which is referred in the error is : stage.draw();
line in my render() method of the current Screen. I don't know what the problem is but my guess is that something is missing while clients get spammed message packets.
Below are my code to get some help and clues that will be appreciated.
render() method
public void render(float delta) {
//inputHandler();
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
app.camera.update();
app.viewport.apply();
app.batch.setProjectionMatrix(app.camera.combined);
app.batch.begin();
//app.batch.setColor(0.85f, 0.85f, 0.85f, 1);
app.batch.draw(wallpaper,0,0);
app.batch.end();
//stage.getCamera().update();
//stage.getViewport().apply();
stage.act(delta);
stage.draw();
}
startServer() method
private void startServer() {
server = new Server(25565,25565);
server.getKryo().register(LoginRequest.class);
server.getKryo().register(LoginResponse.class);
server.getKryo().register(MessageRequest.class);
server.getKryo().register(MessageResponse.class);
server.getKryo().register(String.class);
server.start();
try {
server.bind(25565,25565);
server.addListener(new Listener(){
@Override
public void received(Connection connection, Object o) {
super.received(connection, o);
if(o instanceof LoginRequest) {
LoginRequest req = (LoginRequest) o;
LoginResponse response = new LoginResponse();
response.setResponseMessage("\n -> " + req.getUsername() + " has joined the server!");
server.sendToAllTCP(response);
}
if(o instanceof MessageRequest) {
MessageRequest req = (MessageRequest) o;
MessageResponse response = new MessageResponse();
response.setMessage("\n -> " + req.getUsername() + " : " + req.getMessage());
server.sendToAllTCP(response);
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
//btn_server.setDisabled(true);
//btn_server.clearListeners();
}
createButton() method (btn_server => send message button, i forgot to change the variable name, don't mind it)
private void createButton() {
btn_style = new TextButton.TextButtonStyle();
btn_style.font = font;
btn_style.up = new TextureRegionDrawable(texUp);
btn_style.down = new TextureRegionDrawable(texDown);
btn_style.over = new TextureRegionDrawable(texOver);
btn_style.disabled = new TextureRegionDrawable(texLock);
btn_join = new TextButton("Join Server", btn_style);
btn_server = new TextButton("Send Message", btn_style);
btn_server.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
if(client.isConnected()) {
MessageRequest req = new MessageRequest();
req.setUsername(name.getText());
req.setMessage(message.getText());
client.sendTCP(req);
}
//btn_server.setDisabled(true);
//btn_server.clearListeners();
}
});
btn_join.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
client = new Client(25565,25565);
client.getKryo().register(LoginRequest.class);
client.getKryo().register(LoginResponse.class);
client.getKryo().register(MessageRequest.class);
client.getKryo().register(MessageResponse.class);
client.getKryo().register(String.class);
try {
client.start();
client.connect(5000, myIP, 25565, 25565);
} catch (IOException e) {
e.printStackTrace();
}
if(client.isConnected()) {
btn_join.setDisabled(true);
name.setDisabled(true);
btn_join.clearListeners();
LoginRequest req = new LoginRequest();
req.setUsername(name.getText());
client.sendTCP(req);
client.addListener(new Listener(){
@Override
public void received(Connection connection, Object o) {
super.received(connection, o);
if(o instanceof LoginResponse) {
LoginResponse response = (LoginResponse) o;
textArea.appendText(response.getResponseMessage());
}
if(o instanceof MessageResponse) {
MessageResponse response = (MessageResponse) o;
textArea.appendText(response.getMessage());
}
}
});
}
return true;
}
});
}
Upvotes: 1
Views: 102