2013-07-01 1 views
-1

IRC 클라이언트를 만들려고합니다. 내가 사용하고있는 라이브러리 (PircBot)는 기본 메소드에서 간단히 시도 할 때 작동합니다.알 수없는 소스로 NulPointerException이 발생했습니다.

비록 더 복잡한 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) 

코드 :

Main 클래스 :

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

그래픽 사용자 인터페이스 코드 (경고, 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(); 
     } 
    } 
} 
} 

편집 : 나는 서버를 하드 코딩/포트와 여전히 NPE 그래서 NPE의 원인이 아닌 다른 것

+1

stacktrace를 보여주세요 – sunrize920

+0

ConnectGUI의 라인 # 216을 가리 키십시오. – Smit

+0

@ 216 행을 보내십시오. instance.connect (server, port); line – user36976

답변

1

알아 내기 : 인스턴스 변수가 코드 실행과 다른 스레드에서 초기화되었습니다. 나는 그 모든 것을 엉망으로 만든 것을 모릅니다.하지만 같은 스레드에서 초기화하면 작동합니다.