0

Hi I have tried Looking for a solution online but it doesn't seem to apply to my problem

I am trying to use a straightforward getText from a textfield in my JForm when I press the register button it works if i try to get the value from the method addCustomer() but in the addCustomer() i have a method called isValid() which checks that it can be added it is here that the txtFullName.getText() does not work with the NullPointerException.

I have tried getting the text value from the addCustomer() using String name = txtFullName.getText() and passing that as a parameter to the isValid() this does work but I don't want to have to pass every variable that i want checking.

I just want to be able to use the standard getText method to do this.

This line is the problem:

if (txtFullName.getText().length() > 0)

Here is the code for the CustomerRegistration Class that is throwing the error:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dataModel.Customer;
import dataModel.Person;
import javax.swing.JOptionPane;

/**
 *
 * @author Rich
 */
public class CustomerRegistration extends javax.swing.JFrame {

    Person customerList = new Person();

    /**
     * Creates new form CustomerRegistration
     */
    public CustomerRegistration() {
        initComponents();
    }

    /**
     * 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() {

        pnlAddress = new javax.swing.JPanel();
        lblAddressLine1 = new javax.swing.JLabel();
        txtAddressLine1 = new javax.swing.JTextField();
        txtAddressLine2 = new javax.swing.JTextField();
        lblAddressLine2 = new javax.swing.JLabel();
        lblAddressLine3 = new javax.swing.JLabel();
        txtAddressLine3 = new javax.swing.JTextField();
        lblTownCity = new javax.swing.JLabel();
        txtTownCity = new javax.swing.JTextField();
        lblPostcode = new javax.swing.JLabel();
        txtPostcode = new javax.swing.JTextField();
        pnlCustomerDetails = new javax.swing.JPanel();
        lblFullName = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lblCostPerKg = new javax.swing.JLabel();
        txtCostPerKg = new javax.swing.JTextField();
        txtFullName = new javax.swing.JTextField();
        lblCustomerDetails = new javax.swing.JLabel();
        lblCustomerRegistration = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        lblAddressDetails = new javax.swing.JLabel();
        btnRegister = new javax.swing.JButton();
        txtTest = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnlAddress.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblAddressLine1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblAddressLine1.setText("Address Name/Number:");

        lblAddressLine2.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblAddressLine2.setText("Address Line 1:");

        lblAddressLine3.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblAddressLine3.setText("Address Line 2:");

        lblTownCity.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblTownCity.setText("Town / City:");

        lblPostcode.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblPostcode.setText("Postcode:");

        javax.swing.GroupLayout pnlAddressLayout = new javax.swing.GroupLayout(pnlAddress);
        pnlAddress.setLayout(pnlAddressLayout);
        pnlAddressLayout.setHorizontalGroup(
            pnlAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAddressLayout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(pnlAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlAddressLayout.createSequentialGroup()
                        .addComponent(lblAddressLine1)
                        .addGap(18, 18, 18)
                        .addComponent(txtAddressLine1))
                    .addGroup(pnlAddressLayout.createSequentialGroup()
                        .addComponent(lblPostcode)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlAddressLayout.createSequentialGroup()
                        .addComponent(lblAddressLine2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtAddressLine2, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlAddressLayout.createSequentialGroup()
                        .addComponent(lblAddressLine3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtAddressLine3, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlAddressLayout.createSequentialGroup()
                        .addComponent(lblTownCity)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtTownCity, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pnlAddressLayout.setVerticalGroup(
            pnlAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAddressLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAddressLine1)
                    .addComponent(txtAddressLine1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAddressLine2)
                    .addComponent(txtAddressLine2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAddressLine3)
                    .addComponent(txtAddressLine3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTownCity)
                    .addComponent(txtTownCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPostcode)
                    .addComponent(txtPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlCustomerDetails.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblFullName.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblFullName.setText("Full Name:");

        lblEmail.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblEmail.setText("Email:");

        lblCostPerKg.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblCostPerKg.setText("Cost Per KG:");

        javax.swing.GroupLayout pnlCustomerDetailsLayout = new javax.swing.GroupLayout(pnlCustomerDetails);
        pnlCustomerDetails.setLayout(pnlCustomerDetailsLayout);
        pnlCustomerDetailsLayout.setHorizontalGroup(
            pnlCustomerDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCustomerDetailsLayout.createSequentialGroup()
                .addContainerGap(46, Short.MAX_VALUE)
                .addGroup(pnlCustomerDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCustomerDetailsLayout.createSequentialGroup()
                        .addGroup(pnlCustomerDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCustomerDetailsLayout.createSequentialGroup()
                                .addComponent(lblFullName)
                                .addGap(11, 11, 11))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCustomerDetailsLayout.createSequentialGroup()
                                .addComponent(lblEmail)
                                .addGap(36, 36, 36))
                            .addComponent(lblCostPerKg))
                        .addGroup(pnlCustomerDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlCustomerDetailsLayout.createSequentialGroup()
                                .addGap(78, 78, 78)
                                .addComponent(txtCostPerKg, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnlCustomerDetailsLayout.createSequentialGroup()
                                .addGap(77, 77, 77)
                                .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(txtEmail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlCustomerDetailsLayout.setVerticalGroup(
            pnlCustomerDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCustomerDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCustomerDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFullName)
                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlCustomerDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEmail)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlCustomerDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCostPerKg)
                    .addComponent(txtCostPerKg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblCustomerDetails.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        lblCustomerDetails.setText("Customer Details:");

        lblCustomerRegistration.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        lblCustomerRegistration.setText("Customer Registration");

        btnCancel.setText("Cancel");

        lblAddressDetails.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        lblAddressDetails.setText("Address Details:");

        btnRegister.setText("Register");
        btnRegister.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRegisterMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(165, 165, 165)
                        .addComponent(lblCustomerRegistration)
                        .addGap(74, 74, 74)
                        .addComponent(txtTest, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblCustomerDetails)
                            .addComponent(pnlCustomerDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblAddressDetails)
                            .addComponent(pnlAddress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(144, 144, 144)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(68, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblCustomerRegistration))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(txtTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(8, 8, 8)
                .addComponent(lblCustomerDetails)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlCustomerDetails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblAddressDetails)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnRegister))
                .addContainerGap(38, Short.MAX_VALUE))
        );

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

    private void btnRegisterMouseClicked(java.awt.event.MouseEvent evt) {                                         
        addCustomer();
    }                                        

    /**
     * @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/tutorial/uiswing/lookandfeel/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(CustomerRegistration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CustomerRegistration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CustomerRegistration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CustomerRegistration.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 CustomerRegistration().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnRegister;
    private javax.swing.JLabel lblAddressDetails;
    private javax.swing.JLabel lblAddressLine1;
    private javax.swing.JLabel lblAddressLine2;
    private javax.swing.JLabel lblAddressLine3;
    private javax.swing.JLabel lblCostPerKg;
    private javax.swing.JLabel lblCustomerDetails;
    private javax.swing.JLabel lblCustomerRegistration;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblFullName;
    private javax.swing.JLabel lblPostcode;
    private javax.swing.JLabel lblTownCity;
    private javax.swing.JPanel pnlAddress;
    private javax.swing.JPanel pnlCustomerDetails;
    private javax.swing.JTextField txtAddressLine1;
    private javax.swing.JTextField txtAddressLine2;
    private javax.swing.JTextField txtAddressLine3;
    private javax.swing.JTextField txtCostPerKg;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JTextField txtPostcode;
    private javax.swing.JTextField txtTest;
    private javax.swing.JTextField txtTownCity;
    // End of variables declaration                   


    public void addCustomer() {
        txtFullName.getText();
        if(this.isValid())
        {
            Person itemToAdd;
            itemToAdd = new Customer(txtFullName.getText());
            customerList.registerCustomer((Customer) itemToAdd);
            JOptionPane.showMessageDialog(this, "Customer " + itemToAdd.getName() + " Added");

        }
    }

    public boolean isValid() 
    {
        Boolean blnValid = false;
        // Check that name is of a valid format (eg. no numbers)
        try
        {
            **if (txtFullName.getText().length() > 0)**
            {
                blnValid = true;
            } else 
            {
                throw new IllegalArgumentException("cannot be blank!");
            }
        } catch(NumberFormatException ex) 
        {
            JOptionPane.showMessageDialog(this, "Full name can only contain letters", "Error Reading data", JOptionPane.ERROR_MESSAGE);
        } catch (IllegalArgumentException ex) 
        {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Invalid Data!", JOptionPane.ERROR_MESSAGE);
        }
        return blnValid;
    }


}
  • if (txtFullName.getText().length() > 0) either txtFullName is not initialised, or txtFullName.getText() isn't initialized ... if you couldn't find this online, what did you look for? – Stultuske Jul 25 '16 at 11:29
  • I looked for textfield.getText() Null pointer. Sorry the other questions looked slightly different to my problem but i guess it is linked to the same problem thanks for your help – Richard Wimbridge Jul 25 '16 at 13:10
  • I have tried the suggestions but nothing works, if it is not initialized why does the same txtFullName.getText() work in my addCustomer method but not in my isValid method that's the bit i can't figure out – Richard Wimbridge Jul 25 '16 at 13:44

0 Answers0