Hi,

I wrote a chat client for an assignment at university. Im now trying to improve it as I have a bit of spare time on my hands. The program runs fine as it is but I am wating to add a private chat. I have got it so far and i can recieve private messages from other chat clients but i cant send private messages. This is my code:

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.util.*;

public class Output
{
private Socket s;
private PrintWriter writeOut;
private chatGUI client;
private Input i;

public Output(Socket s, chatGUI c)
{
this.s = s;
client = c;

try
{
writeOut = new PrintWriter(
new OutputStreamWriter(
s.getOutputStream() ) );
}

catch(Throwable t)
{
t.printStackTrace();
}
}

public void login()
{
String login = client.getLogin();
writeOut.println("login:"+login);
writeOut.flush();
writeOut.println("getUsers");
writeOut.flush();
writeOut.println("getTopics");
writeOut.flush();
}

public void logout()
{
writeOut.println("logout");
writeOut.flush();
}

public void updateUsers()
{
writeOut.println("getUsers");
writeOut.flush();
}

public void sendMessageAll(String message)
{
String completeMessage = "all:"+message;
writeOut.println(completeMessage);
writeOut.flush();
}

public void sendMessageTopic(String message,String topic)
{
String completeMessage = "topic:"+topic+":"+message;
writeOut.println(completeMessage);
writeOut.flush();
}

public void sendPrivateMessage(String message, String user)
{
String completeMessage = "private:"+user+":"+message;
System.out.println(completeMessage);
System.out.println(writeOut);
writeOut.println(completeMessage);
writeOut.flush();
}
}

I get the following errors:

private:go:dfgnsry //line printed from code
null //System.out of "writeOut"

Exception occurred during event dispatching:
java.lang.NullPointerException
at Output.sendPrivateMessage(Output.java:74)
at privateChat.actionPerformed(privateChat.java:133)
at javax.swing.JTextField.fireActionPerformed(JTextFi eld.java:421)
at javax.swing.JTextField.postActionEvent(JTextField. java:586)
at javax.swing.JTextField$NotifyAction.actionPerforme d(JTextField.java:696)
at javax.swing.SwingUtilities.notifyAction(SwingUtili ties.java:1384)
at javax.swing.JComponent.processKeyBinding(JComponen t.java:2078)
at javax.swing.JComponent.processKeyBindings(JCompone nt.java:2104)
at javax.swing.JComponent.processKeyEvent(JComponent. java:2041)
at java.awt.Component.processEvent(Component.java:355 3)
at java.awt.Container.processEvent(Container.java:116 4)
at java.awt.Component.dispatchEventImpl(Component.jav a:2593)
at java.awt.Container.dispatchEventImpl(Container.jav a:1213)
at java.awt.Component.dispatchEvent(Component.java:24 97)
at java.awt.LightweightDispatcher.processKeyEvent(Con tainer.java:2155)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:2135)
at java.awt.Container.dispatchEventImpl(Container.jav a:1200)
at java.awt.Window.dispatchEventImpl(Window.java:926)
at java.awt.Component.dispatchEvent(Component.java:24 97)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 339)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:85)

I know it is something to do with "writeOut" but I have tried alsorts and I cant seem to figure out how to solve it. Please can somebody help, i know this must be simple!

Thank you for your time