1

When I try to save an Object using the elements listed in the main title subject I get this error:

target is null for setProperty(null, "tiposClientesIdTipoCliente", [Ljava.lang.String;@2504d0cd)

First, I have this Action class gotten using Hibernate:

public class Cliente implements java.io.Serializable {

private Integer idCliente;
private int tiposClientesIdTipoCliente;
private String nombreCliente;
private int telefonoCliente;

public Cliente() {
}

public Cliente(int tiposClientesIdTipoCliente, String nombreCliente, int telefonoCliente) {
    this.tiposClientesIdTipoCliente = tiposClientesIdTipoCliente;
    this.nombreCliente = nombreCliente;
    this.telefonoCliente = telefonoCliente;
}

public Integer getIdCliente() {
    return this.idCliente;
}

public void setIdCliente(Integer idCliente) {
    this.idCliente = idCliente;
}

public int getTiposClientesIdTipoCliente() {
    return this.tiposClientesIdTipoCliente;
}

public void setTiposClientesIdTipoCliente(int tiposClientesIdTipoCliente) {
    this.tiposClientesIdTipoCliente = tiposClientesIdTipoCliente;
}

public String getNombreCliente() {
    return this.nombreCliente;
}

public void setNombreCliente(String nombreCliente) {
    this.nombreCliente = nombreCliente;
}

public int getTelefonoCliente() {
    return this.telefonoCliente;
}

public void setTelefonoCliente(int telefonoCliente) {
    this.telefonoCliente = telefonoCliente;
}

}

Second, this is my DAO interface:

package dao;

import entities.Cliente;
import java.util.ArrayList;


public interface ClienteDAO {

public boolean agrego(Cliente cliente);
public boolean borrar(Cliente cliente);
public ArrayList<Cliente> listar();
} 

Third, I got this as my DAOs Implementation:

package dao;

import entities.Cliente;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class ClienteImpl implements ClienteDAO {

Transaction transaction = null;
Session session;

public boolean agrego(Cliente cliente) {
   try
   { 
       session = HibernateUtil.getSessionFactory().getCurrentSession();
       transaction=session.beginTransaction();
       //registrar o actualizar
       session.saveOrUpdate(cliente);
       transaction.commit();
       return true;
   }
   catch(Exception e)
   {
       if(transaction!=null)
           transaction.rollback();
       
       return false;
   }
}

public boolean borrar(Cliente cliente) {
   try
   { 
       session = HibernateUtil.getSessionFactory().getCurrentSession();
       transaction=session.beginTransaction();
       session.delete(cliente);
       transaction.commit();
       return true;
   }
   catch(Exception e)
   {
       if(transaction!=null)
           transaction.rollback();
       
       return false;
   }
}

public ArrayList<Cliente> listar() {
    try
    {
       Session session; 
       Transaction transaction;
       session = HibernateUtil.getSessionFactory().getCurrentSession();
       transaction=session.beginTransaction();
       return (ArrayList<Cliente>)session.createQuery("from Cliente").list();
    }
    catch(Exception e)
    {
        return null;
    }
}
}

Forth, this is my controller:

 package controllers;

import com.opensymphony.xwork2.ModelDriven;
import dao.ClienteImpl;
import dao.ClienteDAO;
import entities.Cliente;
import java.util.ArrayList;


public class ClienteController implements ModelDriven<Cliente> {

Cliente cliente = new Cliente();//
ArrayList<Cliente> listaclientes = new ArrayList();
ClienteDAO clienteDAO;
String msg = "";

public ClienteController() {
    clienteDAO = new ClienteImpl();
}

public Cliente getModel() {
    return cliente;
}

public String agrego() {
    clienteDAO.agrego(cliente);
    listaclientes = clienteDAO.listar();
    return "exito";
}

public String borrar() {
    clienteDAO.borrar(cliente);
    listaclientes = clienteDAO.listar();
    return "exito";
}

public String listar() {
    listaclientes = clienteDAO.listar();
    return "exito";
}

public Cliente getDatos() {
    return cliente;
}

public void setDatos(Cliente datos) {
    this.cliente = datos;
}

public ArrayList<Cliente> getListaclientes() {
    return listaclientes;
}

public String getMsg() {
    return msg;
}
}

Sixth, This is the form I use to add the new Object/data:

<s:form action="agregaCliente">
       <s:textfield label="Tipo" name="cliente.tiposClientesIdTipoCliente" />
       <s:textfield label="Nombre" name="cliente.nombreCliente" />
       <s:textfield label="Teléfono" name="cliente.telefonoCliente" />
      <s:submit value="registrar/actualizar" />
    </s:form>

and finally, this is my action in the struts.xml file:

<action name="agregaCliente" class="controllers.ClienteController" method="agrego">
    <result name="exito">/jsp/clientes.jsp</result>
</action>

I don't know what I am missing. I can load and display data from the same DB, but cannot add/update nor delete it.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Carlos
  • 49
  • 1
  • 1
  • 8

1 Answers1

0

If you have implemented a ModelDriven your model is pushed to the top of the valueStack where you can map form properties directly to model properties.

   <s:textfield label="Tipo" name="tiposClientesIdTipoCliente" />
   <s:textfield label="Nombre" name="nombreCliente" />
   <s:textfield label="Teléfono" name="telefonoCliente" />

This answer could be used if you want to access action properties next to the OGNL root.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks for the reply. I found out anyway what I was missing in the code (setters and getters in the controller where wrong lol). – Carlos Apr 21 '14 at 01:37