尝试将消息发送到客户端套接字时出现NullPointerException

时间:2013-09-19 18:58:16

标签: java multithreading sockets nullpointerexception

我有一个ServerHandler类,它创建一个新的PrintWriter并将其存储到PrintWriter数组中。通过给selectedId整数提供应该接收消息的客户端的ID号来发送消息。当我在ServerHandler类中调用SendMessage()方法时,它会毫无问题地发送消息,但是当我尝试从包含GUI的类调用SendMessage(“some message”)方法时,它会给我一个NullPointerException。

posljiSporocilo()是实际的SendMessage(),因为有些方法是我的语言

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

  public class ServerHandler implements Runnable{
Socket klientSocket;
static int userCounter = 0;
static int selectedId = 0;
BufferedReader reader;
static PrintWriter writer;
static PrintWriter[] writerHolder = new PrintWriter[10];

static outputHelper out = new outputHelper();   


public ServerHandler(Socket klientSocket) throws IOException
{
    userCounter++;
    this.klientSocket = klientSocket;
    writer = new PrintWriter(this.klientSocket.getOutputStream());
    writerHolder[userCounter] = writer;

    InputStreamReader inReader = new      InputStreamReader(this.klientSocket.getInputStream());
    reader = new BufferedReader(inReader);

    out.frameOutput(this.klientSocket.getInetAddress().toString(), "Server sprejel povezavo:"); 

    selectedId = 1;
    posljiSporocilo("AutoSent");

当我在实际的类中调用posljiSporocilo()方法时,它会将消息发送给客户端。
        }

public ServerHandler(){
    // Do not launch the main Constructor
}

public void run(){

    String inMessage = null;
    try{
        while((inMessage = reader.readLine()) != null){
            System.out.println("Server Sprejel: " + inMessage);
        }
    }catch(IOException ex){
        ex.printStackTrace();
    }

}

public  void selectId(int id){
    selectedId = id;
    out.frameOutput("ID Changed to: " + selectedId, "ID change");


}

public void posljiSporocilo(String message){
    try
    {
        writerHolder[selectedId].println(message);
        writerHolder[selectedId].flush();

    }catch(Exception ex)
    {
        out.frameOutput("ERROR Trying to send a message", "ERR_MESSAGE:");
        ex.printStackTrace();
    }

}


 }

包含GUI内容的类:

package homeControl;

 IMPORTS

public class mainWindow extends JFrame {

private JPanel contentPane;
private JTextField messageTextField;
ServerHandler svr = new ServerHandler();
outputHelper o = new outputHelper();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                mainWindow frame = new mainWindow();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public mainWindow() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnOptions = new JMenu("Options");
    menuBar.add(mnOptions);

    JMenu mnId = new JMenu("ID");
    menuBar.add(mnId);

    JMenuItem idEnaItem = new JMenuItem("ID: 1");
    mnId.add(idEnaItem);
    idEnaItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            if(event.getActionCommand() != null){
                svr.selectId(1);
                setTitle("ID changed to 1");
            }
        }
    });

    JMenuItem idDvaItem = new JMenuItem("ID: 2");
    mnId.add(idDvaItem);
    idDvaItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            if(event.getActionCommand() != null){
                svr.selectId(2);
                setTitle("ID changed to 2");
            }
        }
    });

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    messageTextField = new JTextField();
    messageTextField.setBounds(10, 209, 292, 20);
    contentPane.add(messageTextField);
    messageTextField.setColumns(10);

    JButton sendButton = new JButton("Send");
    sendButton.setBounds(312, 208, 89, 23);
    contentPane.add(sendButton);
    sendButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){

            if(event.getActionCommand() != null)
            {
            svr.posljiSporocilo("THIS MESSAGE DOES NOT GET SENT");
            }

当我从这个类调用posljiSporocilo()方法时,它会给我一个NullPointerException

        }
    });
}
 }

我希望我能给你足够的信息,谢谢!

Debugmode SS:     http://imageshack.us/f/543/1vea.png/

2 个答案:

答案 0 :(得分:0)

你能告诉我为什么你在开始时递增计数器吗?这将导致您的writerHolder数组中的第0个元素永远不会被填充。

userCounter++;
this.klientSocket = klientSocket;
writer = new PrintWriter(this.klientSocket.getOutputStream());
writerHolder[userCounter] = writer;

我猜你的NPE与此有关。如果您选择的id为0,则方法posljiSporocilo中的代码将抛出NPE。

答案 1 :(得分:0)

  

但是当我尝试从包含GUI的类调用SendMessage(“some message”)方法时,它会给我一个NullPointerException。

这很难理解,因为你没有显示哪条行实际上正在抛出NPE。在查看代码时,我看到以下代码。我看到writeHolder数组的初始化位置,但我想知道writerHolder[selectedId]是否可能是null

public void posljiSporocilo(String message){
    try {
        writerHolder[selectedId].println(message);
        writerHolder[selectedId].flush();
        ...

selectedId可以大于 吗?如果多个线程正在调用初始化,您可能应该使用userCounter而不是非同步AtomicInteger

我将学会使用debugger in eclipse并逐步查看每个对象以查看哪个对象为空。