Getting the following exception:-
java.lang.NullPointerException
at com.local.testing.ChatClient$sendButtonActionListener.actionPerformed(ChatClient.java:53)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I am using the following two classes:-
package com.local.testing;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChatClient {
JTextField outgoing=null;
Socket sock=null;
PrintWriter writer=null;
public void buildGui(){
JFrame frame = new JFrame("ChatClient");
JPanel mainPanel = new JPanel();
outgoing = new JTextField(20);
JButton send = new JButton("send");
send.addActionListener(new sendButtonActionListener());
mainPanel.add(outgoing);
mainPanel.add(send);
frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
frame.setVisible(true);
frame.setSize(400, 400);
frame.pack();
setUpNetworking();
}
private void setUpNetworking(){
try{
sock = new Socket("127.0.0.1",4242);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("Connection Established");
}
catch(UnknownHostException uhe){
uhe.printStackTrace();
}
catch(IOException ioe){
ioe.getMessage();
}
finally{
if(writer !=null){
writer.close();
}
}
}
public class sendButtonActionListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//System.out.println(outgoing.getText());
try{
writer.println(outgoing.getText());
writer.flush();
}
catch(Exception e){
e.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
}
public static void main(String []x){
ChatClient client = new ChatClient();
client.buildGui();
}
}
package com.local.testing;
import java.io.*;
import java.net.*;
public class ChatServer {
public void connectToClient(){
try {
ServerSocket serverSocket = new ServerSocket(4242);
while(true)
{
Socket sock = serverSocket.accept();
InputStreamReader stream = new InputStreamReader(sock.getInputStream());
BufferedReader reader = new BufferedReader(stream);
String chat=reader.readLine();
System.out.println(chat);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String [] x){
ChatServer server = new ChatServer();
server.connectToClient();
}
}
What could be the issue?