在给变量赋值之后,变量仍然为null [英] 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();}
在这段代码中,最后,我为c1分配了一个值,但是当我打印它时,我在所有c1字段中都得到了null.我为Cliente类写下了代码.我想打印我给c1的所有值,但是我不知道为什么在所有字段中都打印null.我使用调试器跟踪代码,直到将所有值分配给新变量的句子都正确为止.
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);
}
}
推荐答案
您应更改此
idc = Id_cliente;
到
Id_cliente = idc;.
您正在为方法参数而不是字段分配值.相同的情况适用于构造函数 Cliente
中的所有参数.
You are assigning a values to your method parameters, rather than fields. Same applies to all of the parameters in your constructor Cliente
.
这篇关于在给变量赋值之后,变量仍然为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!