I'm making a simple program that I'm trying to add multiplayer for. I currently have byte arrays (byte[]
) for the data that's being sent to the server and to the clients. When I send a byte array after I move the player on the screen I get a java.lang.NullPointerException
and I know that it is not null. I've even tried putting an if
statement checking if it's null and eclipse said it was dead code.
if (data == null) {
System.out.println("Data is null!");
}
Here is my code where it gives me the exception.
byte[] data = new byte[10];
byte packet00 = (byte) ((byte) x/8);
byte packet01 = (byte) ((byte) x/8);
byte packet02 = (byte) ((byte) x/8);
byte packet03 = (byte) ((byte) x/8);
byte packet04 = (byte) ((byte) x/8);
byte packet05 = (byte) ((byte) x/8);
byte packet06 = (byte) ((byte) x/8);
byte packet07 = (byte) ((byte) x/8);
byte type = 39;
data[0] = 2;
data[1] = packet00;
data[2] = packet01;
data[3] = packet02;
data[4] = packet03;
data[5] = packet04;
data[6] = packet05;
data[7] = packet06;
data[8] = packet07;
data[9] = type;
client.sendData(data);
x
is an int
that is changed by a key press, and the method sendData
from client takes a byte[]
.
I couldn't find anything to help me fix this, and I want to know if I'm missing something and/or a fix to this.
Here's a portion of the stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.dripgames.main.Main$1.keyPressed(Main.java:132)
at java.awt.Component.processKeyEvent(Unknown Source)