0

I'm trying to get property values as an Array in order to use them to display some data in a PDF file. When I try something like this all properties values are equal null.

Product p = new Product("989-ST", "Magazine Vogue");
System.out.println(p.getData().length);
for(String e : p.getData()){
System.out.print(e + " ");
}
//--Output:: 2
//--null     null

My Prdocut class is like:

Public class Product {
private String sku;
private String descripcion;


public Product() {

}

public Product(String sku, String descripcion) {
    sku = this.sku;
    descripcion = this.descripcion;

}

public String getDescripcion() {
    return descripcion;
}
public void setDescripcion(String descripcion) {
    this.descripcion = descripcion;
}
public String getSku() {
    return sku;
}
public void setSku(String sku) {
    this.sku = sku;
}

public String[] getData() {
    String[] data = { this.descripcion, this.sku };
    return data;
}
}

Am I missing something? Because when I set the property values using setters I can get the expected values:

Product p = new Product();
p.setSku("989-ST");
p.setDescription("Magazine Vogue");
System.out.println(p.getData().length);
for(String e : p.getData()){
System.out.print(e + " ");
}
//--Output:: 2
//--989-ST     Magazine Vogue
Mario Rodz
  • 37
  • 1
  • 8

3 Answers3

1

The assignment statements in the constructor must be reversed:

this.sku = sku;
this.descripcion = descripcion;

otherwise you would be assigning the field values (which are null) to the local variables.

M A
  • 71,713
  • 13
  • 134
  • 174
1

You have reversed assignment and your class sku and descripcion members are not properly initialized. Your constructor should look like

public Product(String sku, String descripcion) {
    this.sku = sku;
    this.descripcion = descripcion;
}
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
0

Your constructor must be

public Product(String sku, String descripcion) {
    this.sku = sku;
    this.descripcion = description;
}

You must set the values provided to the constructor as arguments, to the variables in the scope of the current class.

Iqbal S
  • 1,156
  • 10
  • 16