Hi
I want to develop a lan messenger using java.
I have written a client code to interact with a server
it connects properly but as soon as i start a thread for listening
connection is ended. please suggest me on this....
below i m pasting my code and the error message
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.lang.*;
class interfaceclient
{
public static void main(String args[])
{
TextInputFrame frame=new TextInputFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
static class TextInputFrame extends JFrame
{
PrintWriter dout;
BufferedReader din;
JPanel panel=new JPanel();
JButton SendButton=new JButton("send");
JButton ExitButton=new JButton("exit");
JTextArea ta=new JTextArea(10,50);
JScrollPane scrollPane=new JScrollPane(ta);
JTextField tf=new JTextField(40);
public TextInputFrame()
{
setTitle("messenger");
setSize(WIDTH,HEIGHT);
Container contentPane=getContentPane();
panel.add(SendButton);
panel.add(ExitButton);
panel.add(tf);
contentPane.add(panel,BorderLayout.SOUTH);
panel.add(ta);
contentPane.add(scrollPane,BorderLayout.CENTER);
try
{
Socket cs=new Socket("localhost",7822);
BufferedReader din = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter dout= new PrintWriter(cs.getOutputStream(),true);
dout.println("hi server");
ta.setText("connected to server");
sockthread thread=new sockthread(cs);
thread.start();
SendButton.addActionListener(new action());
}catch(IOException ie){System.out.println("cant find server");}}
class action implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try
{
String message=(tf.getText());
dout.println(message);
tf.setText("");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class sockthread extends Thread
{private Socket cs;
public sockthread(Socket acs){cs=acs;}
public void run()
{
try
{
dout.println("before while");
while(true)
{
dout.println("after while");
String mess=din.readLine();
ta.append("Server:"+mess+"\n");
}
}catch(IOException ie){System.out.println(ie);}
}
}
public static final int WIDTH=600;
public static final int HEIGHT=400;
}
}
the following error message is generated
Exception in thread "Thread-2" java.lang.NullPointerException
at interfaceclient$TextInputFrame$sockthread.run(Main.java:77)
java.lang.NullPointerException
java.lang.NullPointerException
java.lang.NullPointerException
BUILD SUCCESSFUL (total time: 23 seconds)