我在java(netbeans)中运行客户端服务器代码时出现异常错误,有人能指出我的代码中的错误吗?

时间:2015-12-01 12:38:40

标签: java netbeans serversocket

请有人帮忙,我收到异常错误:线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException ..有人在这段代码中指出我的错误吗?提前谢谢你。

这是我的客户端代码:

var query = myContext.Mammals.IncludeExtraEntities(typeof(Dog));

这是我的服务器端代码:

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]);
        }
    }

}

0 个答案:

没有答案