0

我不断收到 NullPointerException,我不知道为什么。该类应该做的是将我在 jtextfield 中写入的任何内容作为 writeUTF 发送,但我不断收到 NullPointerException

dos = new DataOutputStream(socket.getOutputStream());

图形用户界面代码:

public class GUI {

private JFrame frame = new JFrame("Dryck & Ingrediens"); // GUI
private JTextField jtf = new JTextField();// GUI
private JTextArea jl1 = new JTextArea();// GUI
private JList jl = new JList();// GUI
private JScrollPane js = new JScrollPane(jl);// GUI
private DataOutputStream dos;// ServerHandler
private JLabel lab = new JLabel("Ange dryck");//GUI
private JLabel lab1 = new JLabel("Walid Shams");
private JLabel lab2 = new JLabel("Kushtrim Brahimi");
private HashMap<String, ArrayList<String>> drinkar = null;//Controller
private DataInputStream dis;
private Socket socket;
private ServerHandler serverH;

public GUI() {


    frame.getContentPane().setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(50, 300, 420, 400);
    frame.setResizable(false);
    frame.setVisible(true);
    js.add(jl);
    js.add(jl1);
    jl1.setEditable(false);
    lab.setBounds(170, 20, 130, 20);
    lab1.setBounds(300, 310, 130, 20);
    lab2.setBounds(300, 330, 130,20);
    jtf.setBounds(130, 40, 150, 40);
    jl.setBounds(50, 90, 150, 200);
    jl1.setBounds(210, 90, 150, 200);
    Container con = frame.getContentPane();
    con.setBackground(Color.cyan);

这是错误发生的地方,也是每次我在 jtextfield 中键入内容时都会得到 nullpointerexception 的地方。

    jtf.addKeyListener(new KeyListener() {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {

            if (dos == null) {
                if (jtf.getText().length() > 0) {
                    try {
                        dos = new DataOutputStream(socket.getOutputStream());
                        dos.writeUTF(jtf.getText());
                    } catch (IOException ex) {
                        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    String[] empty = new String[]{""};
                    jl.setListData(empty);
                }
            }

        }
    }
);


    jl.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
             if (jl.getSelectedIndex() != -1) {
                 String item = (String) jl.getSelectedValue();
                 jl1.setText("");
                 for (String ingrediens : drinkar.get(item)) {
                     jl1.append(ingrediens + "\n");
                 }
             }else{
                 jl1.setText("");
             }
        }
    });
    frame.add(jtf);
    frame.add(jl);
    frame.add(jl1);
    frame.add(lab);
    frame.add(lab1);
    frame.add(lab2);
    frame.add(js);
}


// tar emot arrayen, lagrar i ny array och visar i JList
public void getArrayList() {

    String[] arr = null;
    for(int i = 0; i < arr.length; i++){
        arr = serverH.setList();
    }
    jl.setListData(arr);


}

public static void main(String[] args) {
    GUI g = new GUI();

}
}
4

2 回答 2

1

关于:

dos = new DataOutputStream(socket.getOutputStream());

唯一可以在这一行抛出 NPE 的变量是 socket,它引出了一个问题:你在哪里初始化 socket?

回答:你没有。

解决方案:解决这个问题。

请始终检查引发 NPE 的行上的变量,因为这将在 99% 的情况下向您显示错误的原因。你似乎还没有尝试过这样做。

于 2012-08-21T01:07:57.327 回答
0

您定义一个实例变量 »serverH«

private ServerHandler serverH;

但不要为其分配任何对象。因此,您应该面对 NullPointerException。您应该为其分配一个对象,例如

serverH = new ServerHandler();

这应该可以解决您关于 NullPointerException 的问题。

于 2012-08-21T21:37:11.910 回答