The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem in Java Chat Application

1 reply on 1 page. Most recent reply: Apr 30, 2003 5:21 AM by Rahul

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
mrunal

Posts: 1
Nickname: mruntru
Registered: Apr, 2003

Problem in Java Chat Application Posted: Apr 29, 2003 10:32 PM
Reply to this message Reply
Advertisement
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);
}

} finally {
System.out.println("closing...");
socket.close();
}
}
}

//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();

getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

setTitle("Schmoozerella Client GUI");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, 280, 20));

jButton1.setText("Send");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 140, -1, 20));

jTextArea1.setEditable(false);
jScrollPane1.setViewportView(jTextArea1);

getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 280, 120));

jMenu1.setText("File");
jMenuItem1.setText("Connect");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});

jMenu1.add(jMenuItem1);
jMenuItem2.setText("Disconnect");
jMenu1.add(jMenuItem2);
jMenu1.add(jSeparator1);
jMenuItem3.setText("Exit");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});

jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Options");
jMenuItem4.setText("Change UserID");
jMenu2.add(jMenuItem4);
jMenu2.add(jSeparator2);
jMenu3.setText("Status");
jMenuItem5.setText("Online");
jMenu3.add(jMenuItem5);
jMenuItem6.setText("Busy");
jMenu3.add(jMenuItem6);
jMenuItem7.setText("Away");
jMenu3.add(jMenuItem7);
jMenuItem8.setText("Offline");
jMenu3.add(jMenuItem8);
jMenuItem9.setText("Playing (DND)");
jMenu3.add(jMenuItem9);
jMenu2.add(jMenu3);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);

pack();
}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
try
{
InetAddress addr = InetAddress.getByName(null);
Socket socket = new Socket(addr, JabberServer.PORT);
jTextArea1.append("Socket successfully initialized!!!\n");
try
{
PrintWriter sendout =
new PrintWriter(
//new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream()),true);
jTextArea1.append("PrintWriter succecsfully initialized!!!");
}
catch(Exception er){System.out.println("Cannot initialize PrintWriter!!!");}
}catch(Exception e)
{System.out.println("Error initializing Scoket!!!");}

}

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.exit(0);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
try
{
String sendstr = new String();
sendstr=jTextField1.getText();
jTextArea1.append(sendstr+"\n");
jTextField1.setText("");


try{

sendout.flush();
sendout.println(toString(str));
sendout.checkError();}catch(Exception ec){System.out.println("Caught culprit!!!"+ec);}//HERE IS THE PROBEM !!


}catch(Exception e)
{System.out.println("Error sending text!!!");}
//String str jTextField1
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new GUi().show();
}


// Variables declaration - do not modify
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuItem jMenuItem9;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JMenuBar jMenuBar1;
// End of variables declaration
//public BufferedReader d;
public String sendstr;
public InetAddress addr;
public Socket socket;
public PrintWriter sendout;

}


Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: Problem in Java Chat Application Posted: Apr 30, 2003 5:21 AM
Reply to this message Reply
Look like you have neither declared nor initialized 'str' in GUi.java:
sendout.println(toString(str));
It should probably be 'sendstr' that you have declared near the end of your program.

Flat View: This topic has 1 reply on 1 page
Topic: create a streamwriter Previous Topic   Next Topic Topic: VB to VJ++ transition

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use