2015-04-27 155 views
-2

我必須做一個聊天客戶端/服務器項目,但我在ChatServer1類的pr1.println(msg)上收到一個空指針異常。 任何幫助,將不勝感激。Java:空指針異常

public class ChatClient1 extends JFrame{ 

    private JPanel contentPane; 
    private JPanel panel_1 = new JPanel(); 
    private static JTextArea textArea = new JTextArea(); 
    private static JTextField Txt1 = new JTextField(); 
    private JButton DisconnectBtn = new JButton("DISCONNECT"); 
    private static JButton SendTxt = new JButton("SEND"); 
    private JLabel lbl1 = new JLabel("Message to send"); 

    ServerSocket serversocket; 
    static BufferedReader br1; 
    static PrintWriter pr1; 
    Socket socket; 


    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ChatServer1 frame = new ChatServer1(); 
        frame.setVisible(true); 
        frame.setTitle("CLIENT"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 

    /** 
    * Create the frame. 
    */ 
    public ChatClient1() { 
     setResizable(false); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 542, 383); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     panel_1.setBackground(Color.GRAY); 
     panel_1.setBounds(0, 0, 536, 355); 
     contentPane.add(panel_1); 
     panel_1.setLayout(null); 

     DisconnectBtn.setBounds(29, 220, 183, 33); 
     panel_1.add(DisconnectBtn); 

     textArea.setBounds(235, 11, 291, 242); 
     panel_1.add(textArea); 

     Txt1.setBounds(29, 303, 387, 41); 
     panel_1.add(Txt1); 
     Txt1.setColumns(10); 

     SendTxt.setBounds(437, 303, 89, 41); 
     panel_1.add(SendTxt); 

     lbl1.setBounds(29, 278, 123, 14); 
     panel_1.add(lbl1); 

     try { 
      Socket socket = new Socket("LocalHost" , 5000); 
      pr1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true); 
      br1 = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      while(true){ 
       textArea.append("Server: " + br1.readLine() + '\n'); 
      } 

     } catch (IOException e) { 

     } 
     SendTxt.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       String msg = Txt1.getText(); 
       pr1.println(msg); 
       pr1.flush(); 
       textArea.append(msg + '\n'); 
       Txt1.setText(""); 

      } 
     }); 
    } 
} 

ChatServer1類:

public class ChatServer1 extends JFrame{ 

    private JPanel contentPane; 
    private JPanel panel_1 = new JPanel(); 
    private static JTextArea textArea = new JTextArea(); 
    private static JTextField Txt1 = new JTextField(); 
    private JButton DisconnectBtn = new JButton("DISCONNECT"); 
    private static JButton SendTxt = new JButton("SEND"); 
    private JLabel lbl1 = new JLabel("Message to send"); 

    static BufferedReader br1; 
    static PrintWriter pr1; 
    Socket socket; 


    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ChatServer1 frame = new ChatServer1(); 
        frame.setVisible(true); 
        frame.setTitle("SERVER"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 
    /** 
    * Create the frame. 
    */ 
    public ChatServer1() { 
     setResizable(false); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 542, 383); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     panel_1.setBackground(Color.GRAY); 
     panel_1.setBounds(0, 0, 536, 355); 
     contentPane.add(panel_1); 
     panel_1.setLayout(null); 

     DisconnectBtn.setBounds(29, 220, 183, 33); 
     panel_1.add(DisconnectBtn); 

     textArea.setBounds(235, 11, 291, 242); 
     panel_1.add(textArea); 

     Txt1.setBounds(29, 303, 387, 41); 
     panel_1.add(Txt1); 
     Txt1.setColumns(10); 

     SendTxt.setBounds(437, 303, 89, 41); 
     panel_1.add(SendTxt); 

     lbl1.setBounds(29, 278, 123, 14); 
     panel_1.add(lbl1); 

     try { 
      ServerSocket serversocket = new ServerSocket(5000); 
      socket = serversocket.accept(); 
      pr1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true); 
      br1 = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      while(true){ 
       textArea.append("Client: " + br1.readLine() + '\n'); 
      } 

     } catch (IOException e) { 

     } 
     SendTxt.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       String msg = Txt1.getText(); 
       pr1.println(msg); 
       pr1.flush(); 
       textArea.append(msg + '\n'); 
       Txt1.setText(""); 

      } 
     }); 
    } 
} 
+0

可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) – Krease

回答

0

你的問題是與ChatServer1代碼,在這裏你有以下幾點:

的ServerSocket ServerSocket的=新的ServerSocket(5000); socket = serversocket.accept();

當您調用accept時,它等待並監聽傳入連接,因此實際上並沒有達到pr1的實例化。因此,pr1保持爲空。易於修復只需將pr1實例化移動到接受調用上方,如下所示:

 pr1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true); 
     ServerSocket serversocket = new ServerSocket(5000); 
     socket = serversocket.accept(); 

希望這有助於!

+0

謝謝你的幫助斯圖爾特。非常感激。 – trant73