java - 突然: java. lang.NullPointerException,昨晚还好好的

标签 java swing tcpclient

我制作了一个小程序,它在 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                


}

(P.S 请不要生气,因为我使用的是 GUI 生成器,这是我的第一个程序,我保证我会学会手工制作)

最佳答案

在第 333 行,唯一可以为 null 的是 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 组件进行更改,都应该在事件分派(dispatch)线程中调用它。更重要的是,由于 EDT 是单线程的、基于队列的执行器,这保证会等到您之前提交的可运行对象完成,所以 chatArea 肯定会被设置。

另一个注意事项:将 UIManager 调用也包装在 invokeLater 调用中通常是一种很好的做法。

编辑:我只是想更清楚地说明您应该始终在 invokeLater 调用中包装什么:

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

不需要包装的东西:

  • 更改尚未显示的组件的属性根据 Robin 在评论中的说法,这仍然需要在 EDT 上发生。
  • 调用重绘
  • 调用 validateinvalidate(我想,我需要对此进行确认)

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

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

此外,我欢迎就我可能忘记的事情向我的列表提出任何建议。

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

关于java - 突然: java. lang.NullPointerException,昨晚还好好的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13317806/

相关文章:

java - 我使用 @SuppressWarnings 的什么值来抑制 "Can be private"警告?

c# - 使用 TcpClient 通过网络发送和接收数据

xcode - 如何使用本地网络上的主机名连接TCP套接字

java - 如何读取 JAX-RS 过滤器中的 JSON 有效负载

java - 单击按钮时,在 JSP 上显示 'Please wait' 警报。当 servlet 上的处理完成时,删除警报

java - 跨应用程序重用 JEditorPane

来自文件路径的 Java JTree 目录结构

c# - 从 TcpClient 获取源 IP

java - Spring initializr 生成的 Spring Boot 应用程序在 Eclipse 上运行时出错

java - 在 jtable 中添加行