NullPointException in EmailServer EmailClient program

Hi, i'm working on a simple email server and email client program. But i get a NullPointException while using it.

I tried several solution by changing the code a bit, but i can't get it right. I could really use some help, thanks in advance.

import java.io.*;

import java.net.*;

publicclass EmailServer

{

privatestatic ServerSocket servSock;

privatestaticfinalint PORT = 1234;

privatestaticfinal String client1 ="Dave";

privatestaticfinal String client2 ="Karen";

privatestaticfinalint MAX_MESSAGES = 10;

privatestatic String[] mailbox1 =

new String[MAX_MESSAGES];

privatestatic String[] mailbox2 =

new String[MAX_MESSAGES];

privatestaticint messagesInBox1 = 0;

privatestaticint messagesInBox2 = 0;

publicstaticvoid main(String[] args)

{

System.out.println("Opening connection...\n");

try

{

servSock =new ServerSocket(PORT);

}

catch(IOException e)

{

System.out.println("Unable to attach to port!");

System.exit(1);

}

do

{

try

{

run();

}

catch (InvalidClientException icException)

{

System.out.println("Error: " + icException);

}

catch (InvalidRequestException irException)

{

System.out.println("Error: " + irException);

}

}while (true);

}

privatestaticvoid run()

throws InvalidClientException, InvalidRequestException

{

try

{

Socket link = servSock.accept();

BufferedReader in =

new BufferedReader(

new InputStreamReader(

link.getInputStream()));

PrintWriter out =new PrintWriter(

link.getOutputStream(),true);

String name = in.readLine();

String sendRead = in.readLine();

if (!name.equals(client1) && !name.equals(client2))

thrownew InvalidClientException();

if (!sendRead.equals("send") &&

!sendRead.equals("read"))

thrownew InvalidRequestException();

System.out.println("\n" + name +" " + sendRead

+"ing mail...");

if (name.equals(client1))

if (sendRead.equals("send"))

{

doSend(mailbox2,messagesInBox2,in);

if (messagesInBox2<MAX_MESSAGES)

messagesInBox2++;

}

else

{

doRead(mailbox1,messagesInBox1,out);

messagesInBox1 = 0;

}

else//From client2.

if (sendRead.equals("send"))

{

doSend(mailbox1,messagesInBox1,in);

if (messagesInBox1><MAX_MESSAGES)

messagesInBox1++;

}

else

{

doRead(mailbox2,messagesInBox2,out);

messagesInBox2 = 0;

}

link.close();

}

catch(IOException e)

{

e.printStackTrace();

}

}

privatestaticvoid doSend(String[] mailbox,

int messagesInBox, BufferedReader in)throws IOException

{

String message = in.readLine();

if (messagesInBox == MAX_MESSAGES)

System.out.println("\nMessage box full!");

else

mailbox[messagesInBox] = message;

}

privatestaticvoid doRead(String[] mailbox,

int messagesInBox, PrintWriter out)throws IOException

{

System.out.println("\nSending " + messagesInBox

+" message(s).\n");

out.println(messagesInBox);

for (int i=0; i><messagesInBox; i++)

out.println(mailbox[i]);

}

}

class InvalidClientExceptionextends Exception

{

public InvalidClientException()

{

super("Invalid client name!");

}

public InvalidClientException(String message)

{

super(message);

}

}

class InvalidRequestExceptionextends Exception

{

public InvalidRequestException()

{

super("Invalid request!");

}

public InvalidRequestException(String message)

{

super(message);

}

}

import java.io.*;

import java.net.*;

publicclass EmailClient

{

privatestatic InetAddress host;

privatestaticfinalint PORT = 1234;

privatestaticfinal String client1 ="Dave";

privatestaticfinal String client2 ="Karen";

privatestatic String name;

privatestatic BufferedReader in, userEntry;

privatestatic PrintWriter out;

publicstaticvoid main(String[] args)throws IOException

{

try

{

host = InetAddress.getLocalHost();

}

catch(UnknownHostException e)

{

System.out.println("Host ID not found!");

System.exit(1);

}

userEntry =new BufferedReader(

new InputStreamReader(System.in));

do

{

System.out.print(

"\nEnter name ('Dave' or 'Karen'): ");

name = userEntry.readLine();

}while (!name.equals(client1) && !name.equals(client2));

run();

}

privatestaticvoid run()throws IOException

{

String option, message, response;

do

{

Socket link =new Socket(host, PORT);

BufferedReader in =

new BufferedReader(

new InputStreamReader(

link.getInputStream()));

PrintWriter out =new PrintWriter(

link.getOutputStream(),true);

do

{

System.out.print("\nEnter 's' to send a message"

+" or 'r' to read message(s): ");

option = userEntry.readLine();

}while (!option.equals("s") && !option.equals("r"));

if (option.equals("s"))

doSend();

else

doRead();

link.close();

System.out.print("\nDo you wish to send/read again? (y/n) : ");

option = userEntry.readLine();

}while (!option.equals("n"));

}

privatestaticvoid doSend()throws IOException

{

System.out.println("\nEnter 1-line message: ");

String message = userEntry.readLine();

out.println(name);

out.println("send");

out.println(message);

}

privatestaticvoid doRead()throws IOException

{

out.println (name);

out.println("read");

int count = in.read();

String dummy = in.readLine();

if (count == 0)

System.out.println("\nMailbox empty.\n");

else

System.out.println("\nMessages:\n");

String message;

for (int i=0; i><count; i++)

{

message = in.readLine();

System.out.println(message);

}

}

}

Screenshot of the error: http://i7.tinypic.com/53ry8te.jpg>

[14275 byte] By [Patrick.R.a] at [2007-11-27 4:33:03]
# 1
In your class EmailClient change the code PrintWriter out = new PrintWriter(link.getOutputStream(),true);to out = new PrintWriter(link.getOutputStream(),true);
akimotoa at 2007-7-12 9:42:52 > top of Java-index,Java Essentials,New To Java...
# 2
Thank you akimoto, problem solved.
Patrick.R.a at 2007-7-12 9:42:52 > top of Java-index,Java Essentials,New To Java...
# 3
You're welcome. Hope you know what the problem was.Regards
akimotoa at 2007-7-12 9:42:52 > top of Java-index,Java Essentials,New To Java...
# 4
Yes, now i understand. So in class EmailClient i changed the following code as well.BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));to in = new BufferedReader(new InputStreamReader(link.getInputStream()));
Patrick.R.a at 2007-7-12 9:42:52 > top of Java-index,Java Essentials,New To Java...