Se ti riferisci al codice presente all'interno di NewJFrame questo è codice autogenerato da Netbeans in seguito alla creazione di un'interfaccia grafica con gli appositi strumenti messi a disposizione.
Attualmente compila e va tutto a buon fine con
a runtime non appena nella classe Server si arriva al punto in cui c'è il comandocodice:public NewJFrame() { Server objS=new Server(); initComponents(); objS.passa(jTextArea1); }
mi segnala il java.lang.NullPointerException.codice:jTextArea.append("XXXX");
Posto tutto il codice per completezza:
codice:package chat_v1c; import java.net.*; import java.io.*; public class Chat_v1c { public static void main(String[] args) throws InterruptedException { Server objS=new Server(); objS.start(); NewJFrame objG=new NewJFrame(); objG.setVisible(true); } }codice:package chat_v1c; import java.net.*; import java.io.*; public class Server extends Thread { BufferedReader dalClient; PrintWriter alClient; javax.swing.JTextArea jTextArea; public void passa(javax.swing.JTextArea a) { this.jTextArea=a; } public Server(){} @Override public void run() { String text=""; Socket clientSocket=null; ServerSocket serverSocket=null; try { serverSocket=new ServerSocket(5555); clientSocket = serverSocket.accept(); dalClient=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); alClient = new PrintWriter(clientSocket.getOutputStream(), true); while(!text.equals("bye")) { text=dalClient.readLine(); System.out.println("ECCOMI!"); System.out.println(clientSocket.getInetAddress() + "Said: " + text + "\n"); jTextArea.append(clientSocket.getInetAddress() + "Said: " + text + "\n"); } alClient.print(clientSocket.getInetAddress()+ "Disconnected!"); serverSocket.close(); System.out.println("FFFUORII"); } catch (IOException e) { e.printStackTrace(); } } }codice:package chat_v1c; import java.net.*; import java.io.*; public class Client { Socket s1; PrintWriter alServer; BufferedReader dalServer; String text; //BufferedReader t = new BufferedReader(new InputStreamReader(System.in)); public void connectToSocket() { try { s1 = new Socket ("169.254.100.208",5555); System.out.print("Connection with "+s1.getInetAddress()+" establised! \n"); alServer= new PrintWriter(s1.getOutputStream(),true); dalServer = new BufferedReader(new InputStreamReader(s1.getInputStream())); } catch(IOException e) { System.out.println(e); } } public void sendToServer(String text) { this.text=text; alServer.println(text); } }codice:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package chat_v1c; /** * * @author fabrizio */ public class NewJFrame extends javax.swing.JFrame { /** * Creates new form NewJFrame */ int i=0; Client objC=new Client(); public NewJFrame() { Server objS=new Server(); initComponents(); objS.passa(jTextArea1); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Send"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 380, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(0, 8, Short.MAX_VALUE)) .add(layout.createSequentialGroup() .add(jTextField1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jButton1))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE) .add(18, 18, 18) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(jButton1) .add(61, 61, 61)) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap()))) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(i==0) { //objS.pass(jTextArea1); objC.connectToSocket(); objC.sendToServer(jTextField1.getText()); i=i+1; } else { objC.sendToServer(jTextField1.getText()); } } private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* * Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* * If Nimbus (introduced in Java SE 6) is not available, stay with the * default look and feel. For details see * http://download.oracle.com/javase/tu...feel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* * Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; // End of variables declaration }

Rispondi quotando