hi, Im trying to make a java chat application using client/server architecture...the server is console based ..im building a GUi for the client..first the server needs to be run..then when a client is run it returns a scoket to the client socket for communication to take place.. Now my problem is that when the client is console based there r no problems but in GUI when im sending the message upon button press im getting a java.lang.NullPointerException..I have used SunOne 4 community edition for coding the GUI..the server and client can run on the same machine now as i have used localhost for the server..and client which need to be changed when running on network now the codes are anybody help with wot to do bout the error..? Thanx // the console based server without any problems from // BruceEckels book thinking in Java import java.io.*; import java.net.*;
public class JabberServer { // Choose a port outside of the range 1-1024: public static final int PORT = 8080; public static void main(String[] args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Started: " + s); try { // Blocks until a connection occurs: Socket socket = s.accept(); System.out.println ("Connection from : " + socket.getInetAddress().getHostAddress() + ':' + socket.getPort()); try { System.out.println( "Connection accepted: "+ socket); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Output is automatically flushed // by PrintWriter: PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())),true);
while (true) { String str = in.readLine(); if (str.equals("END")) break; System.out.println(str); out.println(str); } // Always close the two sockets... } finally { System.out.println("closing..."); socket.close(); } } finally { s.close(); } } }
//the console based client w/o problems import java.net.*; import java.io.*;
public class JabberClient { public static void main(String[] args) throws IOException { BufferedReader d = new BufferedReader(new InputStreamReader(System.in)); String str = new String(); // Passing null to getByName() produces the // special "Local Loopback" IP address, for // testing on one machine w/o a network: InetAddress addr = InetAddress.getByName(null); // Alternatively, you can use // the address or name: // InetAddress addr = // InetAddress.getByName("127.0.0.1"); // InetAddress addr = // InetAddress.getByName("localhost"); System.out.println("addr = " + addr); Socket socket = new Socket(addr, JabberServer.PORT); // Guard everything in a try-finally to make // sure that the socket is closed: try { System.out.println("socket = " + socket); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Output is automatically flushed // by PrintWriter: PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())),true); out.println(userid); while(true) { System.out.print(userid + " says :"); str=d.readLine(); out.println(str); if (str=="END") break; System.out.println(str); }
//the client code using GUI in Forte //the problem is in sendout.println(sendstr) //in jButton1ActionPerformed method which is giving //a java.lang.NullPointerException
import java.net.*; import java.io.*;
public class GUi extends javax.swing.JFrame {
/** Creates new form GUi */ public GUi() { initComponents(); setSize(380,220); }
/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() { java.awt.GridBagConstraints gridBagConstraints;
jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenuItem2 = new javax.swing.JMenuItem(); jSeparator1 = new javax.swing.JSeparator(); jMenuItem3 = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jMenuItem4 = new javax.swing.JMenuItem(); jSeparator2 = new javax.swing.JSeparator(); jMenu3 = new javax.swing.JMenu(); jMenuItem5 = new javax.swing.JMenuItem(); jMenuItem6 = new javax.swing.JMenuItem(); jMenuItem7 = new javax.swing.JMenuItem(); jMenuItem8 = new javax.swing.JMenuItem(); jMenuItem9 = new javax.swing.JMenuItem();
sendout.flush(); sendout.println(toString(str)); sendout.checkError();}catch(Exception ec){System.out.println("Caught culprit!!!"+ec);}//HERE IS THE PROBEM !!