0

I am trying to add an object to an ArrayList. However, I don't understand why I'm getting a NullPointerException when I use the method to insert a Customer into my ArrayList.

  • I have 7 classes in total: Customer, Account, SavingsAccount, CheckingAccount (the last two classes extend from Account), Services, gui1 and gui2.
  • I made a Customer object called "titular" inside the Accounts class. Is the one I'm referencing when adding the parameters with the setters in the code bellow.

GUI2 class code:

private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {                                      
         Cuenta aux;
         Random numCuenta = new Random();
        int randomNum = 25;
        if (txtCombo.getSelectedItem().toString() == "Cuenta de Ahorros") {
            aux = new CuentaAhorro();
            aux.titular = new Cliente();
            aux.titular.setNombre(txtNombre.getText());
            //aux.setSaldo(Double.parseDouble(txtSaldo.getText()));
            aux.setNumeroDeCuenta(numCuenta.nextInt(randomNum));
            aux.titular.setTelefono(txtTelefono.getText());
            aux.titular.setDireccion(txtDireccion.getText());
           try {
               servicio.insertarCuentaAhorro(aux.titular.getNombre(), aux.titular.getTelefono(), aux.titular.getDireccion());
           } catch (Exception e) {
               e.printStackTrace();
           }
            
        } 
        } catch (Exception ex) {
            System.out.println("Ha occurido un error en línea 136.");
            ex.printStackTrace();
        }

Service class code:

public class Servicios implements Serializable {

    private ArrayList<Cuenta> clientes = new ArrayList<Cuenta>();

    public void insertarCuentaAhorro(String nombre, String telefono, String direccion) {
       Cuenta aux = new CuentaAhorro();
       aux.titular = new Cliente();
       try {
       aux.titular.setNombre(nombre);
       //aux.setSaldo(Double.parseDouble(saldo));
       aux.titular.setTelefono(telefono);
       aux.titular.setDireccion(direccion);
       } catch (Exception e1) {
           e1.printStackTrace();
       } finally {
           clientes.add(aux);
           System.out.println(clientes.size());
       }
    }

Error I'm getting:

java.lang.NullPointerException
    at gui.AddAccountMenu.btnOKActionPerformed(AddAccountMenu.java:138)
    at gui.AddAccountMenu$2.actionPerformed(AddAccountMenu.java:91)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
fatlafel
  • 7
  • 5
  • 1
    Can you share the stacktrace of the exception? – illuminator3 May 01 '21 at 13:09
  • 3
    Related: [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788), [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) and last but not least https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. Also regarding `.toString() == "Cuenta de Ahorros"` -> [How do I compare strings in Java?](https://stackoverflow.com/q/513832) – Pshemo May 01 '21 at 13:14
  • `txtCombo.getSelectedItem().toString() == "Cuenta de Ahorros"` this is wrong syntax for String equals. The correct syntax is `object.toString()..equals("Cuenta de Ahorros");` – papaya May 01 '21 at 13:15
  • I've added the Exception. The String are being compared fine, the conditional executes. – fatlafel May 01 '21 at 13:25
  • Well...which line in the **gui.AddAccountMenu.btnOKActionPerformed** event is line **138**? – DevilsHnd - 退職した May 01 '21 at 13:45
  • The line servicio.insertarCuentaAhorro(aux.titular.getNombre()... – fatlafel May 01 '21 at 13:55
  • 1
    For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 01 '21 at 14:13
  • `servicio` is `null`. We can't tell you why, but an NPE thrown on that line can only mean that ... given what happens in the preceding statements of that method. – Stephen C May 01 '21 at 14:16

0 Answers0