-2

So, I've seen several of posts like this on Stackoverflow, but I am completely confused here after taking resources from like 10 posts. And the fact that I'm relatively new to Java isn't really helping. So, my goal is to get a file, convert it to a byte array, send that, have the client put that into a byte array, and convert that byte array to a string.

So here's the relevant server code (this is in a try/catch just so you know):

System.out.println("Waiting for a client...");
// Start a server
ServerSocket server = new ServerSocket(3210);
// Listen for anyone at that port
Socket socket = server.accept();
System.out.println("A client has connected!");
DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
// Get file
File file = new File("history.txt");
// Convert file to byte array
byte[] bytes = new byte[(int) file.length()];
// Send bytes
outputStream.write(bytes);
// Close everything down
socket.close();
outputStream.close();
server.close();

If you read the comments, you get a basic idea of what I think is/should be done in the client and server. Here's the client (again, surrounded in a try/catch):

Socket socket = new Socket(desktopName, 3210);
// Get stream
DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
// Create a byte array
byte[] bytes = new byte[1024 * 16];
// Convert byte array to string
allMessagesTextBox.setText(new String(bytes));
inputStream.close();
socket.close();

And when that code is run in Eclipse, I get a java.lang.NullPointerException on the client on the line where it converts the byte array to string and prints it out. I've tried dozens of different techniques provided by websites and QA sites, but they all lead me to this error. Any ideas on what's wrong here, and how to fix it?

  • Please see: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Jun 29 '18 at 16:51
  • `[teach-me]` Please try to think in terms of "divide and conquer". Your question can be subdivided into "How to send and receive 'something' in Java?" ("something" could be a string or a byte array). "How to read a file to byte array?" "How to save a byte array to file in Java?" Each of these questions is answered already. Then you use your skills as a developer to combine the answers into a complete solution. Only you can do that, SO is no help there. –  Jun 29 '18 at 17:03

1 Answers1

0

Many errors:

byte[] bytes = new byte[(int) file.length()];
// Send bytes
outputStream.write(bytes);

This just creates a byte array, and then write it. You're never reading any byte from the file. Soall the bytes in the array have the value 0.

byte[] bytes = new byte[1024 * 16];
// Convert byte array to string
allMessagesTextBox.setText(new String(bytes));

Again, this never reads anything from the input stream. It creates an array, filled with zeroes, of an arbitrary size, and tries to transform it to a String, using your platform default encoding.

I get a java.lang.NullPointerException on the client on the line where it converts the byte array to string and prints it out

This line, presumably (since you didn't post the stack trace of the exception):

allMessagesTextBox.setText(new String(bytes));

This means that allMessagesTextBox is null. You may not call a method on a null reference.

Read What is a NullPointerException, and how do I fix it?, and read the Java IO tutorial to learn how to read bytes from a stream. Also read this article on character encoding, to know what this is about and why it's important.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for telling me what's wrong, but can you also tell me how to fix the problem? – user10007713 Jun 29 '18 at 17:32
  • I did: read the IO tutorial to learn how to read bytes from a stream, read the post on NPEs to understand when they are thrown, and what it means to initialize a variable. – JB Nizet Jun 29 '18 at 17:36