Im trying to send a message using an object. I have a bit of code that create a GUI and another chunk of code that acts either creates a server if there isnt a connection or acts as a client if a server is available. The User types in a message through the GUI and sends it via the “communicator” as a “message” object. The user is able to send messages if the communicator is setup as a client but not if its set up as a server…
If the user tries to send a message with the program set up as a server I get the following error.
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
And the message doesn’t send
This is my code for the communicator, At the bottom is the function that is supposed to send the message
package hunterinstant;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Chris
*/
public class HunterCom implements Runnable {
private static String SERVER_IP = "127.0.0.1";
private static int SERVER_PORT = 5000;
Scanner scanner = new Scanner(System.in); //to read text from the console
Socket socket = null;
boolean client;
Conversation window;
ObjectOutputStream out = null;
public void initialize(Conversation c, String ip, String port) {
System.out.println(ip);
System.out.println(port);
SERVER_IP = ip;
SERVER_PORT = Integer.parseInt(port);
window = c;
}
public void run() {
try {
socket = new Socket(SERVER_IP, SERVER_PORT);
window.addText("Connected to server");
client = true;
} catch (Exception ex) {
window.addText("No Users Online: Waiting for connection");
client = false;
}
if (client == true) {
ObjectInputStream in = null;
while (true) {
try {
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}
//get the reply from the server
if (in == null) {
in = new ObjectInputStream(socket.getInputStream());
}
Message message = (Message) in.readObject();
window.addText(message.getMessage());
//System.out.println("Server said: " + message.getMessage());
} catch (Exception ex) {
window.addText("Error: " + ex);
}
}
} // End Client
else {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(SERVER_PORT);
} catch (IOException ex) {
window.addText("Error occured while creating the server socket");
return;
}
Socket socket = null;
try {
//Waits untill a connection is made, and returns that socket
socket = serverSocket.accept();
} catch (IOException ex) {
window.addText("Error occured while accepting the socket");
return;
}
window.addText("Connection created, client IP: " + socket.getInetAddress());
ObjectInputStream in = null;
while (true) {
try {
if (in == null) {
in = new ObjectInputStream(socket.getInputStream());
}
Message message = (Message) in.readObject();
window.addText(message.getMessage());
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}
} catch (Exception ex) {
window.addText("Error: " + ex);
}
}
}
}
//THIS IS THE CODE THAT IS SUPPOSED TO SEND THE MESSAGE
public void sendMessage(String str)
{
try {
out.writeObject(new Message(str));
System.out.println("Hello");
out.flush();
} catch (IOException ex) {
Logger.getLogger(HunterCom.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
This is the code that is supposed to send the message from the GUI
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
comunicator.sendMessage(jTextField1.getText());
jTextArea2.append(jTextField1.getText());
}
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==10) // Has the user pressed enter?
{
comunicator.sendMessage(jTextField1.getText());
jTextArea2.append(jTextField1.getText());
}
}
Sorry for what I’m sure is a noobish question… I’m sure I’m doing something really obviously stupid here but I have only been doing java for a couple of months and I have spent hours trying to figure this one out with no joy….
Hours? Oh, my.
Start by reading the stack trace. It tells you the line number and the .java source file at which the NPE occurred. Go to that line, look at the object references, and see which one is null.
NPE is easy to fix. It usually happens because you’ve made erroneous assumptions about object initialization. You’ll just have to figure out how to initialize that reference properly. Scope, constructor – you’ll find the problem.
Get a decent IDE, like IntelliJ, and step through the code with a debugger. It’ll take ten minutes tops, not hours.
Personally, I’m unwilling to debug lots of code for someone. Yours isn’t complete, so it can’t be compiled and run. You didn’t paste the stack trace in your question, so I can’t read it for you. You’ve done little to help us help you.