2013-09-22 93 views
0
public final class Mymain extends javax.swing.JFrame { 

    public static String name = ""; 
    public String msg; 
    public static String s = null; 
    public static String talkto; 
    XMPPConnection connection; 

    public Mymain() throws NullPointerException, IOException, XMPPException { 
     initComponents(); 
     welcome.setText(name); 

    } 

    public Mymain(String username, String password) throws IOException, XMPPException, NullPointerException, InterruptedException, InvocationTargetException { 
     ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com"); 
     connection = new XMPPConnection(config); 
     connection.connect(); 
     connection.login(username, password); 
     name = username; 

     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       try { 
        new Mymain().setVisible(true); 
       } catch (NullPointerException | IOException | XMPPException ex) { 
        Logger.getLogger(Mymain.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
     }); 

    } 

    public void begin_chat(String to) throws IOException, XMPPException, NullPointerException { 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

     Chat chat = connection.getChatManager().createChat(to, new MessageListener() { 

      @Override 
      public void processMessage(Chat chat, Message msg) { 
       if (msg.getType() == Message.Type.chat) { 
        if (msg != null && msg.getBody() != null) { 
         System.out.println(chat.getParticipant() + " says: " 
           + (msg != null ? msg.getBody() : "NULL")); 
        } 
       } 
      } 
     }); 
     while ((msg = br.readLine()) != null) { 

      chat.sendMessage(msg); 
     } 
     if (msg.equals("bye")) { 
      connection.disconnect(); 
     } 
    } 

    @SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jLabel2 = new javax.swing.JLabel(); 
     jPanel1 = new javax.swing.JPanel(); 
     jLabel1 = new javax.swing.JLabel(); 
     welcome = new javax.swing.JLabel(); 
     jSeparator1 = new javax.swing.JSeparator(); 
     jLabel3 = new javax.swing.JLabel(); 
     tf1 = new javax.swing.JTextField(); 

     jLabel2.setText("jLabel2"); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setTitle("[email protected] Me!"); 
     setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 
     setResizable(false); 

     jPanel1.setBackground(new java.awt.Color(0, 255, 204)); 

     jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N 
     jLabel1.setText("Heya,"); 

     welcome.setFont(new java.awt.Font("Calibri", 1, 18)); // NOI18N 

     jSeparator1.setBackground(new java.awt.Color(0, 0, 0)); 
     jSeparator1.setForeground(new java.awt.Color(0, 0, 0)); 

     jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N 
     jLabel3.setText("Buddies Online"); 

     tf1.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N 
     tf1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       tf1ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
       jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
       .addContainerGap(96, Short.MAX_VALUE) 
       .addComponent(jLabel1) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(welcome, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(67, 67, 67)) 
       .addComponent(jSeparator1) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
       .addGap(57, 57, 57) 
       .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(tf1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(390, 390, 390)) 
       ); 
     jPanel1Layout.setVerticalGroup(
       jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap(19, Short.MAX_VALUE) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(welcome, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addComponent(tf1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(235, 235, 235)) 
       ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addContainerGap()) 
       ); 
     layout.setVerticalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap()) 
       ); 

     pack(); 
    }// </editor-fold>       

    private void tf1ActionPerformed(java.awt.event.ActionEvent evt) { 

     if (!(talkto = tf1.getText()).equals("")) { 
      try { 
       begin_chat(talkto); 
      } catch (IOException | XMPPException | NullPointerException ex) { 
       Logger.getLogger(Mymain.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 

    } 

    public static void main(String args[]) { 
    } 
// Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JSeparator jSeparator1; 
    public static javax.swing.JTextField tf1; 
    public static javax.swing.JLabel welcome; 
// End of variables declaration     
} 

我得到空指針異常在線聊天聊天中的begin_chat()聊天= ...我已經建立了連接..即使我得到異常..但是當我在begin_chat函數中添加連接=新的xmppconnection,我越來越illegela監視器狀態異常...有人請幫助..NullPointerException在聊天功能

+0

你在調用哪個構造函數? –

+0

看起來你並沒有初始化你的連接 – prasanth

+1

爲了更好地幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

2

你有兩個問題。首先,如果您不調用參數化構造函數,則連接永遠不會創建,但如果這樣做,則會創建一個單獨的實例Mymain,這意味着第二個實例沒有有效的連接,因爲有效的引用屬於參數化的實例

public Mymain(String username, String password) throws IOException, XMPPException, NullPointerException, InterruptedException, InvocationTargetException { 
    ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com"); 
    connection = new XMPPConnection(config); 
    connection.connect(); 
    connection.login(username, password); 
    name = username; 

    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       // This is a new instance of MyMain, so the connection 
       // you just created is relevant... 
       new Mymain().setVisible(true); 
      } catch (NullPointerException | IOException | XMPPException ex) { 
       Logger.getLogger(Mymain.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    }); 
} 

開始通過刪除默認的構造函數和移動其功能的參數化構造

刪除此

public Mymain() throws NullPointerException, IOException, XMPPException { 
    initComponents(); 
    welcome.setText(name); 
} 

並更新此...

public Mymain(String username, String password) throws IOException, XMPPException, NullPointerException, InterruptedException, InvocationTargetException { 
    initComponents(); 
    welcome.setText(name); 

    ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com"); 
    connection = new XMPPConnection(config); 
    connection.connect(); 
    connection.login(username, password); 
    name = username; 

    setVisible(true); 
} 

一個更好的解決辦法是先建立連接,然後將它傳遞給的Mymain

例如一個實例...

public Mymain(XMPPConnection connection) { 
    this.connection = connection; 
    initComponents(); 
    setVisible(true); 
} 

try { 

    String username, String password) throws IOException, XMPPException, NullPointerException, InterruptedException, InvocationTargetException { 
    ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com"); 
    connection = new XMPPConnection(config); 
    connection.connect(); 
    connection.login(username, password); 
    new MyMain(connection); 
} catch (IOException | XMPPException | NullPointerException | InterruptedException | InvocationTargetException exp) { 
    exp.printStackTrace(); 
} 

基礎上previos例如,這適合我...

public static void main(String args[]) { 
    try { 
     System.out.println("Connecting..."); 
     ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com"); 
     final XMPPConnection connection = new XMPPConnection(config); 
     connection.connect(); 
     String userName = ...; 
     String password = ...; 
     connection.login(userName, password); 

     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       System.out.println("Connected..."); 
       Mymain mymain = new Mymain(connection); 
      } 
     }); 
    } catch (XMPPException exp) { 
     exp.printStackTrace(); 
    } 
} 
+0

你寫的代碼是完美的@MadProgrammer。但是我應該在哪裏調用t他begin_chat()...我想打開的框架以及begin_chat()一起工作。幫助我請。 – user2799025

+0

當我在構造函數本身中調用begin_chat()時,創建了聊天,但是,GUI組件未初始化:(當我在程序的其他地方調用begin_chat()時,connection.getchatmanager()返回null,即我得到空指針異常 – user2799025

+0

我剛剛嘗試過,它適用於我,看看更新... – MadProgrammer