0

Getting the following exception:-

java.lang.NullPointerException
    at com.local.testing.ChatClient$sendButtonActionListener.actionPerformed(ChatClient.java:53)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I am using the following two classes:-

package com.local.testing;

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ChatClient {
    JTextField outgoing=null;
    Socket sock=null;
    PrintWriter writer=null;
    public void buildGui(){
        JFrame frame = new JFrame("ChatClient");
        JPanel mainPanel = new JPanel();
        outgoing = new JTextField(20);
        JButton send = new JButton("send");
        send.addActionListener(new sendButtonActionListener());
        mainPanel.add(outgoing);
        mainPanel.add(send);
        frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
        frame.setVisible(true);
        frame.setSize(400, 400);
        frame.pack();

        setUpNetworking();
    }

    private void setUpNetworking(){
        try{
        sock  = new Socket("127.0.0.1",4242);
        writer = new PrintWriter(sock.getOutputStream());
        System.out.println("Connection Established");
    }
        catch(UnknownHostException uhe){
            uhe.printStackTrace();
        }
        catch(IOException ioe){
            ioe.getMessage();
        }
        finally{
            if(writer !=null){
                writer.close();
            }
        }
}
    public class sendButtonActionListener implements ActionListener{
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            //System.out.println(outgoing.getText());
            try{

            writer.println(outgoing.getText()); 
            writer.flush();
            }
            catch(Exception e){
                e.printStackTrace();
            }
            outgoing.setText("");
            outgoing.requestFocus();
        }
    }

    public static void main(String []x){
        ChatClient client = new ChatClient();
        client.buildGui();
    }
}

package com.local.testing;

import java.io.*;
import java.net.*;

public class ChatServer {

    public void connectToClient(){
        try {
            ServerSocket serverSocket = new ServerSocket(4242);
            while(true)
            {
                Socket sock = serverSocket.accept();
                InputStreamReader stream = new InputStreamReader(sock.getInputStream());
                BufferedReader reader = new BufferedReader(stream);
                String chat=reader.readLine();
                System.out.println(chat);
            }
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String [] x){
        ChatServer server = new ChatServer();
        server.connectToClient();
    }
}

What could be the issue?

4

3 回答 3

1

There is something wrong with your setUpNetworking method. If you run the following, you will see that it prints test/test2/null. So you catched the IOException and hence the writer isn't initialized which will throws the NPE :

private void setUpNetworking(){
        System.out.println("test");
        try{
        sock  = new Socket("127.0.0.1",4242);
        writer = new PrintWriter(sock.getOutputStream());
        }
        catch(UnknownHostException uhe){
            System.out.println("test1");
            uhe.printStackTrace();
        }
        catch(IOException ioe){
            System.out.println("test2");
            ioe.getMessage();
        }
        finally{
            if(writer !=null){
                writer.close();
            }
        }
        System.out.println(writer);
}

If you print the stack trace you get :

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.local.testing.ChatClient.setUpNetworking(ChatClient.java:31)
at com.local.testing.ChatClient.buildGui(ChatClient.java:14)
at com.local.testing.ChatClient.main(ChatClient.java:62)

So you have to fix this error.

于 2013-06-10T18:31:02.050 回答
0

It seems that your event handler has be been called before your initialisation in buildGui(). So basically make sure that this function is called earlied e.g. in the constructor.

于 2013-06-10T18:22:45.523 回答
0
writer.println(outgoing.getText()); 

writer is initialized to null and never set to an object before above call.(JTextField outgoing=null;

于 2013-06-10T18:22:54.840 回答