Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试将对象添加到ArrayList时出现NullPointerException_Java_Swing_Arraylist_Nullpointerexception - Fatal编程技术网

Java 尝试将对象添加到ArrayList时出现NullPointerException

Java 尝试将对象添加到ArrayList时出现NullPointerException,java,swing,arraylist,nullpointerexception,Java,Swing,Arraylist,Nullpointerexception,我正在尝试向ArrayList添加对象。但是,我不明白为什么在使用该方法将客户插入ArrayList时会出现NullPointerException 我一共有7个类:客户、账户、储蓄账户、支票账户(后两个类从账户扩展而来)、服务、GUI 1和GUI 2。 我在Accounts类中创建了一个名为“titular”的客户对象。是我在下面的代码中使用setter添加参数时引用的参数 GUI2类代码: private void btnOKActionPerformed(java.awt.event.

我正在尝试向ArrayList添加对象。但是,我不明白为什么在使用该方法将客户插入ArrayList时会出现NullPointerException

  • 我一共有7个类:客户、账户、储蓄账户、支票账户(后两个类从账户扩展而来)、服务、GUI 1和GUI 2。
  • 我在Accounts类中创建了一个名为“titular”的客户对象。是我在下面的代码中使用setter添加参数时引用的参数
GUI2类代码:

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();
        }
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());
       }
    }
服务等级代码:

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();
        }
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());
       }
    }

您能否共享异常的堆栈跟踪?相关:,最后但并非最不重要。同样关于
.toString()==“Cuenta de Ahorros”
->
txtCombo.getSelectedItem().toString()==“Cuenta de Ahorros”
这对于字符串equals是错误的语法。正确的语法是
object.toString()…等于(“Cuenta de Ahorros”)我已经添加了异常。字符串比较良好,条件执行。要更快获得更好的帮助,请添加or。