I have been struggled to solve this problem for few days, i was working on multichat application where in this class i want to send the text to the server and and response in the client side, if anyone can help me i really appreciate
from what i known the error shown at MyChatPage.java:43, which is
out.println(txtmsg.getText());
it is something related null value in either Print Writer or txtmsg.getText(), but no matter what i done still cannot solve.
Here is my other codes
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MyChatPage$1.actionPerformed(MyChatPage.java:43)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
at javax.swing.JTextField.postActionEvent(JTextField.java:721)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1664)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2926)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2842)
at java.awt.Component.processEvent(Component.java:6282)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
client
public class MyChatPage extends javax.swing.JFrame {
BufferedReader in;
PrintWriter out;
private static String PassAddress;
private static String PassName2;
private static String PassEmail2;
public MyChatPage() {
initComponents();
txtmsg.addActionListener(new ActionListener() {
/**
* Responds to pressing the enter key in the textfield by sending
* the contents of the text field to the server. Then clear
* the text area in preparation for the next message.
*/
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, out);
JOptionPane.showMessageDialog(null, txtmsg.getText());
out.println(txtmsg.getText());
out.flush();
txtmsg.setText("");
}
});
}
//------Pass value from IP------------------------------------------------------
public void setAddress(String sA)
{
PassAddress = sA;
}
public String getAddress()
{
return PassAddress;
}
public void setName2(String sN2)
{
PassName2 = sN2;
txtuserlist.append(PassName2 + "\n");
}
public String getName2()
{
return PassName2;
}
public void setEmail2(String sE2)
{
PassEmail2 = sE2;
}
public String getEmail2()
{
return PassEmail2;
}
//------------------------------------------------------------------------------
private void run() throws IOException {
// Make connection and initialize streams
String serverAddress2 = getAddress();
Socket socket = new Socket(serverAddress2, 1235);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
out.println(line + "\r\n");
out.flush();
if (line.startsWith("SUBMITNAME")) {
out.println(getName2());
} else if (line.startsWith("NAMEACCEPTED")) {
txtmsg.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
txtmsgarea.append(line.substring(8) + "\n");
}
}
}
public static void main(String args[]) throws Exception {
MyChatPage mcp = new MyChatPage();
mcp.run();
}
server side
public class MyChatServer {
private static final int PORT = 1235;
private static HashSet<String> names = new HashSet<String>();
/**
* The set of all the print writers for all the clients. This set is kept so
* we can easily broadcast messages.
*/
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
/**
* The application main method, which just listens on a port and spawns
* handler threads.
*/
public static void main(String[] args) throws Exception {
System.out.println("MyChat server is running.");
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
private static class Handler extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private String name;
/**
* Constructs a handler thread, squirreling away the socket. All the
* interesting work is done in the run method.
*/
public Handler(Socket socket) {
this.socket = socket;
}
/**
* Services this thread's client by repeatedly requesting a screen name
* until a unique one has been submitted, then acknowledges the name and
* registers the output stream for the client in a global set, then
* repeatedly gets inputs and broadcasts them.
*/
public void run() {
try {
// Create character streams for the socket.
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
out.println("SUBMITNAME");
name = in.readLine();
if (name == null) {
return;
}
synchronized (names) {
if (!names.contains(name)) {
names.add(name);
break;
}
}
}
out.println("NAMEACCEPTED");
writers.add(out);
while (true) {
String input = in.readLine();
if (input == null) {
return;
}
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + ": " + input);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
if (name != null) {
names.remove(name);
}
if (out != null) {
writers.remove(out);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}