Re: Threads java.lang.NullPointerException
- From: "Sean" <anonymous@xxxxxxxxxxx>
- Date: Mon, 22 Aug 2005 05:25:05 -0500
On
>Hi there I was wondering if anyone can help me please.
>
>I ve been working on a simple chat server program and it works when the
>server handles one client at the time. I ve been trying to make the
>server able to answer clients requests at the same time with threads.
>
>I always get the same error message when I run the server:
>
>java.lang.NullPointerException
>
>at chatserverq3.ServerGUI.processClientRequests(ServerGUI.java:113)
>
> at chatserverq3.ServerGUI.run(ServerGUI.java:73)
>
> at java.lang.Thread.run(Thread.java:484)
>
>This is the client code:
>
>import java.awt.*;
>import javax.swing.*;
>import java.awt.event.*;
>import java.io.*;
>import java.net.*;
>
>
>public class ClientGUI extends JFrame implements ActionListener
>{
> private JTextField nameField;
> private JLabel nameLabel;
> private JLabel areaLabel;
> private JPanel topPanel, botPanel;
> private JButton connectButton;
> private JTextArea listArea;
>
> private InputStream is;
> private OutputStream os;
> private BufferedReader fromServer;
> private Socket socket;
> private PrintWriter toServer;
>
>
> static final String SERVER_ADDRESS = "127.0.0.1";
> static final int SERVER_PORT_NUMBER = 4000;
> static final String CLIENT_LOGGINGOFF = "Exit";
>
>//constructor for the client GUI
> public ClientGUI(String title)
> {
> setSize(400, 400);
> setTitle(title);
> setLocation(100, 100);
> nameLabel = new JLabel("Name");
> nameField = new JTextField(10);
> areaLabel = new JLabel("Connected Users");
> topPanel = new JPanel();
> topPanel.setLayout(new BorderLayout());
> botPanel = new JPanel();
> connectButton = new JButton("Connect");
> connectButton.addActionListener(this);
> listArea = new JTextArea(10, 30);
> JScrollPane scr = new JScrollPane(listArea);
>
> topPanel.add(nameField, "West");
> topPanel.add(nameLabel, "Center");
> topPanel.add(connectButton, "East");
>
> botPanel.add(areaLabel);
> botPanel.add(listArea);
>
> getContentPane().add(topPanel, "North");
> getContentPane().add(botPanel, "Center");
>
> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>
> }
>
> //attempts to connect the client to the server
> private void connectServer()
Change to connectToServer?
> {
> try
> {
> socket = new Socket(SERVER_ADDRESS, SERVER_PORT_NUMBER);
> openStreams();
I can't find this method. Do you mean to call open()?
> }
> catch(IOException e)
> {
> System.out.println("Trouble contacting the server " + e);
> }
> }
>
> private void open()throws IOException
> {
> final boolean AUTO_FLUSH = true;
> is = socket.getInputStream();
> fromServer = new BufferedReader(new InputStreamReader(is));
> os = socket.getOutputStream();
> toServer = new PrintWriter(os, AUTO_FLUSH);
> }
>
>//action performed when the client GUI button is pressed
> public void actionPerformed(ActionEvent a)
> {
> String buttonLabel = connectButton.getText();
> String userName = nameField.getText();
> try
> {
> if((buttonLabel == "Connect")&&(userName.length()> 0))
> {
> connectToServer();
> sendUserName(userName);
> connectButton.setText("Disconnect");
> }
> else
> {
> logOff(userName);
> connectButton.setText("Connect");
> }
>
> }
> catch(IOException e)
> {
> System.out.println("Problem with the server " + e);
> }
> }
>
> private void close()throws IOException
> {
> toServer.close();
> os.close();
> fromServer.close();
> is.close();
> }
>
> //sends the user name to the server
> private void sendUserName(String aName)throws IOException
> {
> String reply;
> String uName = aName;
> toServer.println(uName + " logged on");
>
> reply = fromServer.readLine();
> listArea.setText(reply);
> }
>
>//notifies the server a client has logged off
> private void logOff(String aName)throws IOException
> {
> String uName = aName;
> toServer.println(uName + " logged off");
> listArea.setText("");
> toServer.println(CLIENT_LOGGINGOFF);
> closeStreams();
I can't find this one either. close()?
> socket.close();
> }
>}
>
>public class Main {
>
> public static void main(String[] args)
> {
> ClientGUI cg = new ClientGUI("ChatClient");
> cg.setVisible(true);
> }
>
>}
>
>The server code is:
>
>
>import java.awt.*;
>import javax.swing.*;
>import java.awt.event.*;
>import java.io.*;
>import java.net.*;
>import java.util.*;
>
>public class ServerGUI extends JFrame implements Runnable
>{
> private JTextArea logArea;
> private JLabel logLabel;
> private JPanel logPanel;
>
> private ServerSocket ss;
> private Socket socket;
>
> private InputStream is;
> private OutputStream os;
>
> private PrintWriter toClient;
> private BufferedReader fromClient;
>
> static final int PORT_NUMBER = 4000;
> static final String CLIENT_LOGGINGOFF = "Exit";
>
> //constructor for the Server GUI
> public ServerGUI(String title)
> {
> setSize(400, 400);
> setTitle(title);
> setLocation(100, 100);
> logArea = new JTextArea(15, 30);
> JScrollPane scr = new JScrollPane(logArea);
> logLabel = new JLabel("Logging history");
>
> logPanel = new JPanel();
> logPanel.add(logLabel);
> logPanel.add(logArea);
>
> getContentPane().add(logPanel);
>
>
>
> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> }
>
>//constructor for the server to set the socket to handle a new thread
> public ServerGUI(Socket s)
> {
> socket = s;
> }
>
>
>
> public void run()
> {
> try
> {
> while(true)
> {
>
> openStreams();
> processClientRequests();
Is this supposed to be processRequests()?
> closeStreams();
> socket.close();
> }
> }
> catch(IOException e)
> {
> System.out.println("Trouble with a connection " + e);
> }
>
> }
>
> //helper method for the class to set up the streams
> private void open() throws IOException
> {
> final boolean AUTO_FLUSH = true;
> is = socket.getInputStream();
> fromClient = new BufferedReader(new InputStreamReader(is));
> os = socket.getOutputStream();
> toClient = new PrintWriter(os, AUTO_FLUSH);
> }
>
>//process the string from received from each client
> private void processRequests() throws IOException
> {
> String userName;
> String reply;
> int numberOfFields = 3;
> String [] fieldContents = new String[numberOfFields];
> userName = fromClient.readLine();
> while(!(userName.equals(CLIENT_LOGGINGOFF)))
> {
> StringTokenizer tokensIn = new StringTokenizer(userName, " ");
> int i = 0;
> while(tokensIn.hasMoreTokens())
> {
> fieldContents[i] = tokensIn.nextToken();
> i++;
> }
>
> reply = fieldContents[0];
> toClient.println(reply);
> logArea.append("[ " + reply + " ] "+ fieldContents[1]+"
>"+fieldContents[2]+"\n");
> userName = fromClient.readLine();
> }
> }
>
> //helper method to close the streams with each client
> private void close() throws IOException
> {
> toClient.close();
> os.close();
> fromClient.close();
> is.close();
> }
>}
>
>import java.net.*;
>import java.io.*;
You should move your imports inside the ChatHandler class.
>
>public class ChatHandler
>{
> static final int PORT_NUMBER = 4000;
>
> public ChatHandler()
This inner class shouldn't be public.
> {
> }
>
> public void run()
> {
> try
> {
> ServerSocket server = new ServerSocket(PORT_NUMBER);
> while(true)
> {
> Socket client = server.accept ();
> System.out.println ("Accepted from " + client.getInetAddress ());
> Thread aThread = new Thread(new ServerGUI(client));
> aThread.start ();
> }
> }
> catch(IOException e)
> {
> System.out.println("Trouble with ServerSocket, port
>"+PORT_NUMBER +": " + e);
> }
> }
>}
>
>
>public class Main {
Any particular reason you have your main method inside an inner class? You
need to put that right in the ServerGUI class.
>
> public static void main(String[] args)
> {
> ServerGUI sg = new ServerGUI("ChatServer");
> sg.setVisible(true);
> ChatHandler cH = new ChatHandler();
> cH.run();
> }
>
>}
>
>
>It is the first time I use threads and maybe there is something I dont
>understand. Can anyone point me to the right direction please? I think
>the problem may be with the 2 server constructor but I am not sure how
>to solve it.
>
>Cheers Pat
>
That's all I have for now, hope I wasn't too hard on ya.
.
- Follow-Ups:
- Re: Threads java.lang.NullPointerException
- From: Max010
- Re: Threads java.lang.NullPointerException
- References:
- Threads java.lang.NullPointerException
- From: Max010
- Threads java.lang.NullPointerException
- Prev by Date: Re: How to improve the performance while scaling image using JMagick
- Next by Date: Re: Can MIDP 1.0 program run in MIDP 2.0 phones?
- Previous by thread: Threads java.lang.NullPointerException
- Next by thread: Re: Threads java.lang.NullPointerException
- Index(es):