Reputation: 576
It's a simple chat app. Server and client as separately app.
Connection between server and client works fine. I have a problem when i'm trying to send String from Client by BufferedWriter to Server BufferedReader. I have null pointer exception.
And can you tell me that is everything fine with threads logic in this app ?
Server app
public class Server {
private int port = 5000;
private ServerSocket server = null;
private Socket socket = null;
private InputStreamReader isr = null;
private OutputStreamWriter osw = null;
private BufferedReader br = null;
private BufferedWriter bw = null;
private Chat chat;
private String message;
private int bOffset = 10;
private JFrame mainFrame;
private JPanel mainPanel;
private JPanel boxPanel;
private JLabel boxTitleLabel;
private JTextArea incomingMessagesJTextArea;
private JTextField messageWritten;
private JButton sendServerButton;
public static void main(String[] args){
System.out.println("Server is running");
new Server().createGUI(); //some swing GUI things
new Server().createServerConnection();
}
public void createServerConnection(){
try {
/** Create socket for communication between apps */
server = new ServerSocket(port);
socket = server.accept();
System.out.println("Server accept client: OK");
/** Create output writers */
osw = new OutputStreamWriter(socket.getOutputStream());
bw = new BufferedWriter(osw);
/** Create inputs readers */
isr = new InputStreamReader(socket.getInputStream()); //data in bytes format
br = new BufferedReader(isr); //data in character format
} catch (IOException e) {
System.out.println("Server Error createServerConnection(): " +e.getMessage());
}
/** Create new thread for read server-input data */
new Thread(new Runnable() {
public void run() {
System.out.println("Server Thread - running");
while (true) {
try {
String message = br.readLine();
System.out.println(message);
} catch (IOException e) {
System.out.println("Server Error createServerConnection() -> new Thread: " +e.getMessage());
}
}
}
}
).start();
}}
Client app
public class Client {
private int port = 5000;
private Socket socket = null;
private InputStreamReader isr = null;
private OutputStreamWriter osw = null;
private BufferedReader br = null;
private BufferedWriter bw = null;
private String message;
private int bOffset = 10;
private JFrame mainFrame;
private JPanel mainPanel;
private JPanel boxPanel;
private JLabel boxTitleLabel;
private JTextArea incomingMessagesJTextArea;
private JTextField messageWritten;
private JButton sendServerButton;
public static void main(String[] args){
System.out.println("Client is running");
new Client().createGUI(); //some swing GUI things
new Client().createConnectionWithServer();
new Client().sendTextToServer();
}
public void sendTextToServer(){
try {
message = "test";
bw.write(message); //null pointer exception
bw.write('\n');
bw.flush();
} catch(Exception ex) {
ex.printStackTrace();
}
}
public void createConnectionWithServer(){
try {
/** Create socket for communication between apps */
socket = new Socket("localhost", port);
/** Create inputs readers */
isr = new InputStreamReader(socket.getInputStream()); //data in bytes format
br = new BufferedReader(isr); //data in characters format
/** Create output writers */
osw = new OutputStreamWriter(socket.getOutputStream());
bw = new BufferedWriter(osw);
} catch (IOException e) {
System.out.println("Server Error createConnectionWithServer(): " +e.getMessage());
}
/** Create new thread for read client-input data */
new Thread(new Runnable() {
public void run() {
System.out.println("Client Thread - running");
while (true) {
try {
message = br.readLine();
System.out.println(message);
} catch (IOException e) {
System.out.println("Client Error createServerConnection() -> new Thread: " +e.getMessage());
}
}
}
}
).start();
}}
Upvotes: 0
Views: 295
Reputation: 61
Change your main method to create a unique instance of your Client class:
public static void main(String[] args){
Client client = new Client();
System.out.println("Client is running");
client.createGUI(); //some swing GUI things
client.createConnectionWithServer();
client.sendTextToServer();
}
You were creating a new instance of the Client class everytime you called the "new" statement. Doing so the variable were initialized in the fist instance, making impossible for the other one to acces the initialized variable (throwing NullPointerException)
Upvotes: 1