I am a beginner in Java and I have a question on the subject of records. I am carrying out an article management program that allows, among other things, to register an article. The maximum number of articles that can be is 100
. They ask us the code of the article and if an article with that code already exists, it can not be registered. Then I write what I have done thanks to the help of this forum:
public class Main {
public static void main(String[] args) {
final int MAXARTICULOS = 100; // {número de artículos máximos de la ferretería}
class Articulo implements Serializable {
String codart; // {código del articulo}
String nombre; // {nombre del artículo}
String caracteristicas; // {características}
double precio; // {precio del artículo}
int cantidad; // {cantidad de artículos en stock}
}
Articulo[] misArticulos = new Articulo[MAXARTICULOS]; // {array que contiene los artículos}
Scanner entrada = new Scanner(System.in);
int menu;
System.out.println("FERRETERIA");
for (int i = 0; i < misArticulos.length; i++) {
System.out.println("Introduce el código: ");
String codigo = entrada.nextLine();
boolean encontrado = false;
for (int j = 0; !encontrado && j < i - 1; j++) {
if (misArticulos[j].codart.equals(codigo)) {
System.out.println("Articulo existente");
encontrado = true;
}
}
if (!encontrado) {
System.out.println("Introduce el nombre: ");
misArticulos[i].nombre = entrada.nextLine();
System.out.println("Introduce las características: ");
misArticulos[i].caracteristicas = entrada.nextLine();
System.out.println("Introduce el precio: ");
misArticulos[i].precio = entrada.nextDouble();
System.out.println("Introduce la cantidad: ");
misArticulos[i].cantidad = entrada.nextInt();
entrada.nextLine();
}
}
} // fin main
Once detected that the article does not exist, you must enter the name, the characteristics, the price and the quantity but when executing it it only lets me enter the name and then the exception java.lang.NullPointerException skips.