0

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)

Tom
  • 16,842
  • 17
  • 45
  • 54
DripGames
  • 13
  • 1
  • 5
  • 3
    You have the stacktrace? It helps knowing which line threw the exception. – Sekkuar Jul 10 '15 at 21:57
  • 6
    My guess, the problem isn't the array, but the `client.sendData()`, `client` is probably `null` at this point – Sekkuar Jul 10 '15 at 21:58
  • @Sekkuar http://pastebin.com/LbJG8VwK EDIT: I'll try refreshing the client variable to its original. – DripGames Jul 10 '15 at 22:01
  • Well... which line is Main.java:132 ? – Sekkuar Jul 10 '15 at 22:02
  • Yeah but we need to know which line in the given code is line 132. – SamTebbs33 Jul 10 '15 at 22:02
  • Another thing, what do you hope to achieve with the first 10 lines of the code you gave us? You're assigning different variables to the exact same expression, and then assigning array indexes to those variables... Just make a variable, assign it to the expression and then assign each array index to that new variable. – SamTebbs33 Jul 10 '15 at 22:04
  • 2
    @DripGames Do you know that you can [edit] your question? Please add your stacktrace here :). – Tom Jul 10 '15 at 22:04
  • I got it to work I needed to get client to not be null like @Sekkuar said, if someone would answer with that I can mark the question answered. – DripGames Jul 10 '15 at 22:09
  • You can place an answer yourself, explaining how you fixed :) – Sekkuar Jul 10 '15 at 22:11
  • @DripGames You can answer it yourself :). Just write what you've changed and why that works. Give it a try. – Tom Jul 10 '15 at 22:11
  • we can't help you stop the client from being null, since that is entirely controlled by the rest of your code. You need to make sure that the right pieces of your code are running that ensure that client doesn't become null. – SamTebbs33 Jul 10 '15 at 22:11

1 Answers1

1

I reassigned my client variable to a new instance of the Client class, as shown here:

client = new Client(/*insert parameters here*/);

The NullPointerException wasn't the byte[] it was the client variable.

Tom
  • 16,842
  • 17
  • 45
  • 54
DripGames
  • 13
  • 1
  • 5