When trying to print the list of objects I get the error of java.lang.NullPointerException
public Productos(int id, int precio, String nombre) {
this.id = id;
this.precio = precio;
this.nombre = nombre;
}
public int getId() {
return id;
}
public int getPrecio() {
return precio;
}
public String getNombre() {
return nombre;
}
@Override
public String toString(){
String a= "Producto: "+nombre+" ID: "+id+" Precio: "+precio;
return a;
}
}
In spite of having the toString method, the error does not change, nor does netbeans recognize the getters with autocompletion, so there is something wrong with me, nor is it a try-catch topic, since it should only take values already added and printed
private Productos Productos;
private ArrayList<Productos> productos = new ArrayList<>();
public void newProducto(){
System.out.println("Ingrese nombre del producto");
String nombre = sc.nextLine();
System.out.println("Ingrese precio del producto");
int precio=sc.nextInt();
int id = productos.size()+1;
new Productos(id, precio, nombre);
productos.add(Productos);
}
public void mostrarProductos(){
for(int i=0; i<productos.size();i++){
System.out.println(productos.get(i).toString());
}
}
Thank you very much in advance.