-1

我正在尝试制作 IRC 客户端。我正在使用的库(PircBot)在我简单地在 main 方法中尝试时工作。

虽然当我为它创建一个更复杂的 GUI 时,它在我尝试连接时给了我一个空指针异常。错误:

java.lang.NullPointerException
at nickfromgreek.nicksIrc.gui.ConnectGUI$connectAction.actionPerformed(ConnectGUI.java:216)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

代码:

主类:

package nickfromgreek.nicksIrc;

import nickfromgreek.nicksIrc.gui.ConnectGUI;

import org.jibble.pircbot.PircBot;

public class NicksIRC extends PircBot {

public NicksIRC() {
    this.setName("nickBot|BETA");
}

@Override
protected void onMessage(String channel, String sender, String login,
        String hostname, String message) {

    if (message.equalsIgnoreCase("!time")) {
        String time = new java.util.Date().toString();
        sendMessage(channel, sender + ": The time is now " + time);
    }

}

public static void main(String[] args) {
    try {
        NicksIRC irc = new NicksIRC();
        irc.setVerbose(true);
        @SuppressWarnings("unused")
        ConnectGUI chat = new ConnectGUI(irc);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

GUI代码(警告,大!):

import java.awt.EventQueue;

public class ConnectGUI extends JFrame {

private static final long serialVersionUID = 1L;

ConnectGUI frame;

private JPanel contentPane;
private NicksIRC instance;
private JTextField ServerTextField;

private final Action customPortCheckbox = new customPortCheckbox();
private JCheckBox useCustomPort;
private JTextField portTextField;
private JLabel lblPort;

private JCheckBox needsPassword;
private JTextField passwordTextField;
private JLabel lblPassword;
private final Action needsPasswordCheckBox = new needsPasswordCheckBox();

private JCheckBox identifyToNickServ;
private JLabel lblignoreIfYou;
private JTextField IdentifyTextField;
private final Action identifyToNickServAct = new identifyToNickServ();
private JLabel lblNickservPassword;
private JTextField textField;
private JLabel lblChannelsToJoin;
private JLabel lblceperateTheChannels;
private final Action connectAction = new connectAction();

public ConnectGUI(NicksIRC instanceg) {
    instance = instanceg;
    EventQueue.invokeLater(new Runnable(){
        public void run(){
            try{
                frame = new ConnectGUI();
                frame.setVisible(true);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
private ConnectGUI() {
    // REMOVED ANNOYING GUI CODE!
}

private class customPortCheckbox extends AbstractAction {
    private static final long serialVersionUID = 1L;

    public customPortCheckbox() {
        putValue(NAME, "Use custom port");
        putValue(SHORT_DESCRIPTION, "");
    }

    public void actionPerformed(ActionEvent e){
        portTextField.setEnabled(useCustomPort.isSelected());
    }
}

private class needsPasswordCheckBox extends AbstractAction {
    private static final long serialVersionUID = 1L;

    public needsPasswordCheckBox() {
        putValue(NAME, "Needs password?");
        putValue(SHORT_DESCRIPTION, "");
    }

    public void actionPerformed(ActionEvent e){
        passwordTextField.setEnabled(needsPassword.isSelected());
    }
}

private class identifyToNickServ extends AbstractAction {
    private static final long serialVersionUID = 1L;

    public identifyToNickServ() {
        putValue(NAME, "Identify to NickServ");
        putValue(SHORT_DESCRIPTION, "");
    }

    public void actionPerformed(ActionEvent e){
        IdentifyTextField.setEnabled(identifyToNickServ.isSelected());
    }
}

private class connectAction extends AbstractAction {
    private static final long serialVersionUID = 1L;

    public connectAction() {
        putValue(NAME, "Connect");
        putValue(SHORT_DESCRIPTION, "");
    }

    public void actionPerformed(ActionEvent e){
        try{
            // Null Field Checks
            if(ServerTextField.getText().equals("")){
                JOptionPane.showMessageDialog(frame, "One or more fields are empty", "Null Field", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if(useCustomPort.isSelected() && portTextField.getText().equals("")){
                JOptionPane.showMessageDialog(frame, "One or more fields are empty", "Null Field", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if(needsPassword.isSelected() && passwordTextField.getText().equals("")){
                JOptionPane.showMessageDialog(frame, "One or more fields are empty", "Null Field", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if(identifyToNickServ.isSelected() && IdentifyTextField.getText().equals("")){
                JOptionPane.showMessageDialog(frame, "One or more fields are empty", "Null Field", JOptionPane.ERROR_MESSAGE);
                return;
            }

            String server = ServerTextField.getText();
            int port = 6667;
            String pass = passwordTextField.getText();
            if(useCustomPort.isSelected()){
                port = Integer.parseInt(portTextField.getText());
            }
            System.out.println(server + "  " + port + "  " + pass + "   " + needsPassword.isSelected());
            if(needsPassword.isSelected()){
                instance.connect(server, port, pass);
            }else{
                instance.connect(server, port); // LINE 216 ... THA PROBLEMATIC!
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
}

编辑:我对服务器/端口进行了硬编码,但仍然是 NPE,所以这不是 NPE 的原因,而是其他原因

4

1 回答 1

1

想通了:实例变量是在与运行代码不同的线程上初始化的。我不知道是什么搞砸了一切,但如果你在同一个线程初始化它,它就可以工作。

于 2013-07-01T20:25:19.257 回答