Domanda

I am trying to make a IRC client. The library i am using(PircBot) works when i try it simply at the main method.

Although when i created a more complex GUI for it it gives me a null pointer exception when i try to connect. The Error:

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)

Code:

Main class:

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();
    }
}
}

The GUI code(warning, BIG!):

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();
        }
    }
}
}

EDIT: I hardcoded the server/port and still NPE so thats not the cause of the NPE its something else

È stato utile?

Soluzione

Figured it out: The instance variable was initialised on a different thread than the code was run. I dont know what that messed up everything but if you initialise it at the same thread it works.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top