0

Please someone help , I get an exception error : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException ..can someone point me the error in this code? thank you in advance.

This is my client side code:

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.net.Socket;
import java.net.InetAddress;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;



public class xClient extends JFrame
{

    public JTextField textField2;
    public JTextArea textArea2;
    public String chatServer;

    public Socket client;
    public ObjectOutputStream output;
    public ObjectInputStream input;

    public String message ="";

    public xClient(String host)
    {
        this.setTitle("Client");
        this.setSize(500,200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        chatServer = host;

        textField2 = new JTextField();
        textField2.addActionListener
        (
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        sendData(event.getActionCommand());
                        textField2.setText("");
                    }
                }

        );
        this.add(textField2,BorderLayout.NORTH);

        textArea2 = new JTextArea();
        JScrollPane pane2 = new JScrollPane(textArea2);
        this.add(pane2,BorderLayout.CENTER);

    }

    private void runClient()
    {
        try
        {
            connectToServer();
            getStreams();
            processConnection();
        }
        catch(EOFException eof)
        {
           displayMessage("\n Client terminated connection!"); 
        }
        catch(IOException io)
        {
            io.printStackTrace();
        }
        finally
        {
            System.out.println("Bye");
        }
    }

    private void connectToServer() throws IOException
    {
        displayMessage("\n Attempting connection");
        client = new Socket(InetAddress.getByName(chatServer),12345);
        displayMessage("Connected to : " + client.getInetAddress().getHostName());
    }

    private void getStreams() throws IOException
    {
        output = new ObjectOutputStream(client.getOutputStream());
        output.flush();

        input = new ObjectInputStream(client.getInputStream());
        displayMessage("\n Got I/O streams \n");

    }

    private void processConnection() throws IOException
    {
        setTextFieldEditable(true);
        sendData("Testing 123");
        do
        {
            try
            {
                message = (String)input.readObject();
                displayMessage("\n" + message);
            }
            catch(Exception eof)
            {
                displayMessage("\n Unknown object type received");
            }

        }
        while(!message.equals("SERVER>>> TERMINATE"));

    }

    private void sendData(String message)
    {
        try
        {
            output.writeObject("CLIENT>>> " + message);
            output.flush();
            displayMessage("\nCLIENT>>> " + message);
        }
        catch(IOException io)
        {
           textArea2.append("\nError writing object");
        }

    }

    private void displayMessage(final String messageToDisplay)
    {
        SwingUtilities.invokeLater
        (
                    new Runnable()
                    {
                        public void run()
                        {
                            textArea2.append(messageToDisplay);
                        }

                    }
        );

    }

    private void setTextFieldEditable(final Boolean editable)
    {
        SwingUtilities.invokeLater
        (
                new Runnable()
                {
                    public void run()
                    {
                        textArea2.setEditable(true);
                    }
                }


        );




    }

    public static void main(String[]args)
    {
        xClient client;

        if(args.length == 0)
        {
            client = new xClient("127.0.0.1");
        }
        else
        {
            client = new xClient(args[0]);
        }
    }

}

This is my server side code:

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import java.awt.BorderLayout;

import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;


public class xServer extends JFrame
{
    public JTextField textField1;
    public JTextArea textArea1;

    public ServerSocket server;
    public Socket connection;

    public int counter = 1;

    public ObjectInputStream input;

    public xServer()
    {
        this.setTitle("Server");
        this.setSize(500,200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textField1 = new JTextField();
        //textField1.setEditable(false);
        this.add(textField1,BorderLayout.NORTH);

        textArea1 = new JTextArea();
        textArea1.setEditable(false);

        JScrollPane pane = new JScrollPane(textArea1);

        this.add(pane,BorderLayout.CENTER);
    }

    public void runServer()
    {
        try
        {
            server = new ServerSocket(12345,100);

            while(true)
            {
                try
                {
                    waitForConnection();
                    getStreams();
                    processConnection();
                }
                catch(EOFException eof)
                {
                    displayMessage("\n Server terminated connection");
                }
                finally        
                {
                    System.out.println("Bye");
                    counter++;
                }

            }
        }
        catch(IOException io)
        {
            io.printStackTrace();    
        }
    }

    public void waitForConnection() throws IOException
    {
      displayMessage("Waiting for connection\n");
      connection = server.accept();
      displayMessage("Connection " + counter + " received from: " + connection.getInetAddress().getHostName());

    }

    public void displayMessage(final String messageToDisplay)
    {
        SwingUtilities.invokeLater
                (
                        new Runnable()
                        {
                            public void run()
                            {
                                textArea1.append(messageToDisplay);
                            }
                        }
                );

    }

    public void getStreams() throws IOException
    {
        input = new ObjectInputStream(connection.getInputStream());

        displayMessage("\n Get I/O streams");
    }

    public void processConnection() throws IOException
    {
        String message ="Test";

        do
        {
            try
            {
                message = (String) input.readObject();
                displayMessage("\n" + message);
            }
            catch(ClassNotFoundException cnf)
            {
                displayMessage("\n Unknown object type received");
            }
        }
        while(!message.equals("CLIENT>>> TERMINATE"));

    }

    public void closeConnetion()
    {
        displayMessage("\nTerminating connection\n");

        try
        {
            input.close();
            connection.close();
        }
        catch(IOException io)
        {
            io.printStackTrace();
        }
    }


    public static void main(String[]args)
    {
        xServer server = new xServer();
        server.runServer();
    }

}
Kugan Kumar
  • 333
  • 1
  • 6
  • 13
  • Along with code the exception stack trace is equally important, how ever you can debug the code, it would be nice if you learn to debug also, that will help you get rid of such small exceptions easily. https://netbeans.org/features/java/debugger.html – Bilbo Baggins Dec 01 '15 at 12:42
  • I found my error, i never call one of my important method...anyways thank you – Kugan Kumar Dec 01 '15 at 12:56

0 Answers0