vilal_
vilal_

Reputation: 37

variable is still null after I assign a value to it JAVA

public void crearCliente() {

    int i, k, j, l;
    registro r = new registro();
    k = lr.getSize();

    for (i = 0; i < k; i++) {
        r = lr.get(i);
        l = r.getSize();

        String contenido[] = new String[8];
        for (j = 0; j < l; j++) {
            contenido[j] = r.getCampoR(j);
            //System.out.println(contenido[j]);
        }
        c1 = new Cliente(contenido[0], contenido[1], contenido[2], contenido[3], contenido[4], contenido[5], contenido[6], contenido[7]);
        c1.verCliente();}

In this code, at the end,I assign a value to c1, but when I print it i get null in all the c1 fields. I write my code down for the class Cliente. I want to print all the values i gave to c1 but i do not know why it print null in all the fields. I follow the code using the debugger and everything is right until the sentences which assign all the values to the new variable.

public class Cliente {

private String Id_cliente;
private String Cod_postal;
private String Numero;
private String Calle;
private String Provincia;
private String Poblacion;
private String Telefono;
private String Apellidos;

public Cliente(String idc, String cp, String num, String cal, String prov, String pob, String tlf, String aps){

    idc = Id_cliente;
    cp = Cod_postal;
    num = Numero;
    cal = Calle;
    prov = Provincia;
    pob = Poblacion;
    tlf = Telefono;
    aps = Apellidos;

}

public void verCliente(){

   System.out.println("Id_cliente: "+ Id_cliente);
   System.out.println("Codigo postal: "+ Cod_postal);
   System.out.println("Numero: "+ Numero);
   System.out.println("Calle: "+ Calle);
   System.out.println("Provincia: "+ Provincia);
   System.out.println("Poblacion: "+ Poblacion);
   System.out.println("Telefono: "+ Telefono);
   System.out.println("Apellidos: "+ Apellidos);

}

}

Upvotes: 2

Views: 3125

Answers (3)

Pradeep Simha
Pradeep Simha

Reputation: 18123

You should change this

idc = Id_cliente;

to

Id_cliente = idc;. 

You are assigning a values to your method parameters, rather than fields. Same applies to all of the parameters in your constructor Cliente.

Upvotes: 3

javadev
javadev

Reputation: 1669

Your constructor for the class Cliente is switching assignement. Change the idc = Id_cliente to Id_cliente = ic.

Upvotes: 0

Daedalus
Daedalus

Reputation: 1667

Your assignments are backwards in your constructor. Switch the left and right hand sides of all those and it should work.

Upvotes: 1

Related Questions