突然:java.lang.NullPointerException,昨晚工作正常

时间:2012-11-10 00:03:50

标签: 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生成器,这是我的第一个程序,我保证我会学会手工完成)

3 个答案:

答案 0 :(得分:3)

第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组件时,都应该在事件派发线程中调用它。更重要的是,由于EDT是基于队列的单线程执行程序,因此可以保证等到你之前提交的runnable完成后,所以chatArea肯定会被设置。

另一个注意事项:通常也可以在UIManager来电中包含invokeLater来电。

编辑:我只是想更清楚一下你应该总是在invokeLater电话中包含什么:

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

不需要包装的东西:

  • 更改尚未显示的组件的属性根据评论中的Robin,这仍然需要在EDT上进行。
  • 致电repaint
  • 致电validateinvalidate(我想,我需要找到确认)

完成所有这些操作,无论何时切换到新的外观,您都不会遇到任何未在EDT上调用的问题。

长话短说,Swing不是线程安全的,所以你应该总是从事件派发线程中调用Swing组件方法。

此外,我欢迎任何有关我可能忘记的事项的建议。

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

答案 1 :(得分:2)

问题是您可以随时在static和非static数据之间切换。最初,该程序运行main()static)。其中,您引用了chatArea(第333行,也是static)。但是,chatArea仅在调用initComponents()(非static)时设置 ,这在构造函数中发生(非static)。在函数的其余部分之前不会总是调用它。

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

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

答案 2 :(得分:1)

这可能是一种竞争条件,使它有时工作。变量chatArea不能保证在主线程到达第333行时初始化。这是由于在此之前通过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);
    }
});