How to Send, Receive, and Print a File in Java? : <p>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.</p>

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

<pre><code>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();
</code></pre>

<p>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):</p>

<pre><code>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();
</code></pre>

<p>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?</p>
