2

我制作了一个小程序,它在 tcp 套接字上侦听和发送行,并将接收到的信息附加到JTextArea. 我用它在没有打开游戏的情况下在 Minecraft 服务器上聊天。

昨晚我工作得很好,但是当我起床时,它就没有工作了。当我打开 netbeans 并运行它时,它给出了这个错误:

Exception in thread "main" java.lang.NullPointerException
    at com.xxx.mcchat.chat.main(chat.java:333)

谁能解释什么是错的?

这是代码(http://pastebin.com/FPNty0qf):

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xxx.mcchat;

import java.io.*;
import java.net.*;
import net.sf.json.*;
import org.apache.commons.beanutils.*;
import org.apache.commons.collections.*;
import org.apache.commons.lang.*;
import net.sf.ezmorph.*;
import org.apache.commons.logging.*;
import java.awt.event.*;
import javax.swing.UIManager;
/**
 *
 * @author xxx
 */
public class chat extends javax.swing.JFrame {

    /**
     * Creates new form chat
     */

    public chat() {
        initComponents();
    }

    public void send(String user, String message){
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;


        try {
            socket = new Socket("mc.xxx.net", 20060);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection");
            System.exit(1);
        }

        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

                //System.out.println(in.readLine()); //Uncomment to debug
        if(username != null){
            out.println("/api/call?method=broadcast&args="+"[\"§7[Web] §b"+username+"§7:§f "+message+"\"]"+"&key=f0e2ad47a9a43c783d2c54f396f655c9279829c8c69ae9f52934648098dec993");
            chatArea.append(username + ": " + message + "\n\r");
            if(autoscrollCheck.isSelected()){
                chatArea.setCaretPosition(chatArea.getText().length() - 1);
            }
        }else{
            chatArea.append("You must set your username!!" + "\n\r");
            if(autoscrollCheck.isSelected()){
                chatArea.setCaretPosition(chatArea.getText().length() - 1);
            }

        }
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                       
    private void initComponents() {

        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jToggleButton1 = new javax.swing.JToggleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        chatArea = new javax.swing.JTextArea();
        input = new javax.swing.JTextField();
        send = new javax.swing.JButton();
        user = new javax.swing.JTextField();
        userset = new javax.swing.JButton();
        autoscrollCheck = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");

        jToggleButton1.setText("jToggleButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Minecraft Chat");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        chatArea.setEditable(false);
        chatArea.setBackground(new java.awt.Color(0, 0, 0));
        chatArea.setColumns(20);
        chatArea.setFont(new java.awt.Font("Consolas", 0, 14)); // NOI18N
        chatArea.setForeground(new java.awt.Color(255, 255, 255));
        chatArea.setLineWrap(true);
        chatArea.setRows(5);
        jScrollPane1.setViewportView(chatArea);

        input.setToolTipText("Enter message here");
        input.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                inputKeyPressed(evt);
            }
        });

        send.setText("Send");
        send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendActionPerformed(evt);
            }
        });

        user.setToolTipText("");
        user.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userActionPerformed(evt);
            }
        });
        user.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                userKeyPressed(evt);
            }
        });

        userset.setText("Set");
        userset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usersetActionPerformed(evt);
            }
        });

        autoscrollCheck.setSelected(true);
        autoscrollCheck.setText("Auto Scroll");
        autoscrollCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                autoscrollCheckActionPerformed(evt);
            }
        });

        jLabel1.setText("Enter Username:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(userset)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(autoscrollCheck))
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(input, javax.swing.GroupLayout.PREFERRED_SIZE, 649, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(send)))
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)))
                    .addComponent(userset)
                    .addComponent(autoscrollCheck))
                .addGap(6, 6, 6)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(input, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(send))
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>                      

    String username = null;
    private void inputKeyPressed(java.awt.event.KeyEvent evt) {                              
           int key = evt.getKeyCode();
           if (key == KeyEvent.VK_ENTER) {  
                send(username, input.getText());
                input.setText("");
              }
    }                               

    private void sendActionPerformed(java.awt.event.ActionEvent evt) {                                   
        send(username, input.getText());
        input.setText("");
    }                                   

    private void usersetActionPerformed(java.awt.event.ActionEvent evt) {                                       
        if(username == null){
                    if(!"".equals(user.getText())){
                        username = user.getText();
                        chatArea.append("Username set!"+"\n\r");
                        if(autoscrollCheck.isSelected()){
                            chatArea.setCaretPosition(chatArea.getText().length() - 1);
                        }
                    }else{
                        chatArea.append("Username can not be blank."+"\n\r");
                        if(autoscrollCheck.isSelected()){
                            chatArea.setCaretPosition(chatArea.getText().length() - 1);
                        }
                    }
                }else{
                    send(username, "§7changed name to " + user.getText());
                    username = user.getText();
                }
    }                                      

    private void userActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
    }                                   

    private void userKeyPressed(java.awt.event.KeyEvent evt) {                              
           int key = evt.getKeyCode();
           if (key == KeyEvent.VK_ENTER) {  
                if(username == null){
                    if(!"".equals(user.getText())){
                        username = user.getText();
                        chatArea.append("Username set!"+"\n\r");
                        if(autoscrollCheck.isSelected()){
                            chatArea.setCaretPosition(chatArea.getText().length() - 1);
                        }
                    }else{
                        chatArea.append("Username can not be blank."+"\n\r");
                        if(autoscrollCheck.isSelected()){
                            chatArea.setCaretPosition(chatArea.getText().length() - 1);
                        }
                    }
                }else{
                    send(username, "§7changed name to " + user.getText());
                    username = user.getText();
                }
              }
    }                              

    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   

    }                                 

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                               
        // TODO add your handling code here:
    }                                

    private void autoscrollCheckActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
    }                                              

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws IOException {

        /* Set the system look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(chat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(chat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(chat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(chat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new chat().setVisible(true);
            }
        });
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;


        try {
            socket = new Socket("mc.xxx.net", 20060);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection");
            System.exit(1);
        }

        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

                //System.out.println(in.readLine()); //Uncomment to debug

        out.println("/api/subscribe?source=chat&key=1e287587f5d1d45255f4708467eeaf8a71085f9ccfd8a354523d233cf5a77be4&show_previous=true");
        out.println("/api/subscribe?source=connections&key=e410592b70c0288654e6c1040edb0f21811dcb3f2ee11051163f36be9be00788&show_previous=false");

        while(true){
            String jsonString = in.readLine();
            JSONObject obj = JSONObject.fromObject(jsonString); 
            JSONObject success = obj.getJSONObject("success");
            if(success.get("message") != null){
                chatArea.append("<" + success.get("player") + "> " + success.get("message") + "\n\r");
                if(autoscrollCheck.isSelected()){
                    chatArea.setCaretPosition(chatArea.getText().length() - 1);
                }
            }else if (success.get("action") != null){
                chatArea.append(success.get("player") + " " + success.get("action") + "\n\r");
                if(autoscrollCheck.isSelected()){
                    chatArea.setCaretPosition(chatArea.getText().length() - 1);
                }
            }
        }
    }

    // Variables declaration - do not modify                     
    public static javax.swing.JCheckBox autoscrollCheck;
    public static javax.swing.JTextArea chatArea;
    private javax.swing.JTextField input;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JButton send;
    private javax.swing.JTextField user;
    private javax.swing.JButton userset;
    // End of variables declaration                


}

(PS 请不要生气,因为我正在使用 GUI 生成器,这是我的第一个程序,我保证我会学习手工)

4

3 回答 3

3

唯一可以null在第 333 行的是chatArea. (如果successnull,它会在第 332 行的 if 语句中引发异常。)正如其他人所建议的,您可能有一个竞争条件,在到达第 333 行之前它没有被初始化。

修复它的正确方法是将调用包含chatArea在调用中SwingUtilities.invokeLater

final JSONObject success = obj.getJSONObject("success");
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        if (success.get("message") != null) {
            chatArea.append("<" + success.get("player") + "> " + success.get("message") + "\n\r");
            if (autoscrollCheck.isSelected()) {
                chatArea.setCaretPosition(chatArea.getText().length() - 1);
            }
        } else if (success.get("action") != null) {
            chatArea.append(success.get("player") + " " + success.get("action") + "\n\r");
            if (autoscrollCheck.isSelected()) {
                chatArea.setCaretPosition(chatArea.getText().length() - 1);
            }
        }
    }
});

每当您对 Swing 组件进行更改时,都应该在事件调度线程中调用它。更重要的是,由于 EDT 是单线程的、基于队列的执行器,这保证会等到您之前提交的可运行对象完成,所以chatArea肯定会设置。

另一个注意事项:通常将呼叫包装在呼叫中也是UIManager一种好习惯invokeLater

编辑:我只想更清楚地了解您应该始终在invokeLater通话中包含什么内容:

  • 构建 Swing 组件
  • 更改 Swing 组件的属性
  • 修改一个 Swing 组件的数据模型(不一定要获取数据,只是告诉关心它的组件发生了变化,比如触发事件,需要在 EDT 上发生)
  • 修改UIManager属性,包括设置外观或修改其键的值
  • 实例化外观
  • 实例化子类ComponentUI
  • 在容器中添加和删除组件

不需要包装的东西:

  • 更改尚未显示的组件的属性根据 Robin 在评论中的说法,这仍然需要在 EDT 上进行。
  • 来电repaint
  • 致电validate,或invalidate(我认为,我需要对此进行确认)

执行所有这些操作,任何时候切换到新的外观和感觉,在 EDT 上没有调用的东西都不会有任何问题。

长话短说,Swing 不是线程安全的,因此您应该始终从事件调度线程调用 Swing 组件方法。

另外,我欢迎对我可能忘记的事情的清单提出任何建议。

以下是一些描述 Swing 中的线程的资源:

于 2012-11-10T00:25:02.430 回答
2

问题是您很容易在数据static和非static数据之间切换。最初,程序运行main()( static)。其中,您参考chatArea(第 333 行,也static)。但是,chatArea仅在调用(non- )设置,这发生在构造函数 (non- ) 中。这并不总是在函数的其余部分之前调用。initComponents()staticstatic

根据您的invokeLater方法,您应该将与聊天程序相关的所有内容(位于之后 invokeLater)移动到构造函数(或某些不是 static的方法)中。

基本上,唯一应该static是你的main()方法。其余的不应该static。如果有帮助,请将事物分成一个新类,您可以从中引用main();这将帮助您初始化程序,然后运行所有与聊天相关的事情。

于 2012-11-10T00:16:21.337 回答
1

这可能是一种竞争条件,它有时会起作用。不能保证在主线程到达第 333 行时初始化该变量chatArea。这是由于在此invokeLater()之前通过某些行延迟了 GUI 的初始化:

java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        new chat().setVisible(true);
    }
});

您需要在这些线程之间进行一些同步,或者,什么也应该起作用,只需在主线程中初始化 GUI:

final chat chatObject = new chat();
java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        chatObject.setVisible(true);
    }
});
于 2012-11-10T00:13:18.440 回答