1

i'm developing swing client server application in java using netbeans its a project from vb and making it in java,

i've programmed some initial code like created gui, runned a thread for connection, now for future expandable i want to make it saperate. eg. gui (here NewJFrame.java) is separated from main class and one another class is client.java which handles connection and thread for it.

so design is, The Main Class (NewClass.java)

public class NewClass {

    public static void main(String args[])
    {
        ClientA A = new ClientA();
        A.go();
    }
}

The GUI Class ( NewJFrame.java)

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }


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

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jTextField1.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(26, 26, 26)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27))
        );

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

    public void setTexter(String s ) { // the setter method to set text of "jTextField1"
        jTextField1.setText(s);
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}

The ClientA class for socket or networking (ClientA.java)

import java.io.*;
import java.net.*;


public class ClientA {
BufferedReader reader;
Socket sock;
NewJFrame gui;
    public void go()
    {
        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() {
            @Override
            public void run() {
                gui  = new NewJFrame();
                gui.setVisible(true);
            }
        });
       setUpNetworking();

    }

        public void setUpNetworking() {
            gui.setTexter("HEllo");
        try {

            String message;
            sock = new Socket("127.0.0.1",8888);
            InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
            reader = new BufferedReader(streamReader);

            System.out.println("Recieved + " + reader.readLine());

            while((message = reader.readLine() ) != null) {
                System.out.println("Recieved + " + message);
            }
            System.out.println("Network Established !!! from client");
        }  catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

but the problem is, in Client.java file while setting the jTextField's value using setter method which is in NewJFrame.java, the error occurs in Client.java as "Exception in thread "main" java.lang.NullPointerException".

how can i solve it or i think it is not proper class design of the application so what would the proper class design of the client-server's client application ?

Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
  • 1
    Give the code, where it exactly throws the error? – Rahul Feb 18 '13 at 10:45
  • 1
    The `jTextField` field in your instance of `NewJFrame` has not been initialised by the time you try to update it from then client. Easy fix: make sure that all fields are initialized during the construction of the instance or - implement an `init()` method on `NewJFrame` that you call before you use the frame by a client. – Andreas Dolk Feb 18 '13 at 10:50
  • 1
    all fields are initialized in the GUI class as u can see ... – Bhavesh G Feb 18 '13 at 14:01
  • Here's a working [example](http://stackoverflow.com/a/3245805/230513) for reference. – trashgod Feb 18 '13 at 14:25

0 Answers0