2013-09-22 66 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