私は多くの情報を保持しなければならないプログラムを作成しています。このプログラムの1つは、アイテムが存在するかどうか(0)、そうでない場合は(-1)に応じて、0または-1を保持するマトリックスを格納します。しかし、メソッドでこの行列を初期化しようとすると、nullPointerExceptionが生成されます。
私は次のようにマトリックスを初期化することを修正しようとしました:
public static int libros[][]= new int[arrayListLibros.size()][numeroMayor()];
しかし、それは別のエラーを生成します
Exception in thread "main" java.lang.ExceptionInInitializerError
at Modelo.Main.main(Main.java:26)
Caused by: java.lang.NullPointerException
at Modelo.SistemaDeGestion.<clinit>(SistemaDeGestion.java:19)
... 1 more
私はそれを解決する方法を見つけられませんでした
コードは次のとおりです。
public void cargarMatrizLibros() throws Exception{
int numeroMayor = numeroMayor();
for (int j = 0; j < arrayListLibros.size(); j++){
for (int k = 0; k < numeroMayor; k++){
if( k < Integer.getInteger(arrayListLibros.get(j).getEjemplares())){
libros[j][k] = 0;
}
else{
libros[j][k] = -1;
}
}
}
}
public static int numeroMayor(){
int numeroMayor = 0;
for (int i = 0; i < arrayListLibros.size(); i++) {
if(Integer.getInteger(arrayListLibros.get(i).getEjemplares()) > numeroMayor){
numeroMayor = Integer.getInteger(arrayListLibros.get(i).getEjemplares());
}
}
return numeroMayor;
}
arrayListLibrosは、プログラムのmainメソッドで以前に初期化されました。
SistemaDeGestion sistema = new SistemaDeGestion();
try {
sistema.cargarDatosEmpleados();
sistema.cargarDatosUsuarios();
sistema.cargarDatosLibros();
sistema.cargarMatrizLibros();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
ここで、sistemaは作成されたクラスを参照します...そしてcargarDatosLibrosのコードはここにあります:
public void cargarDatosLibros() throws Exception {
arrayListLibros = new ArrayList<>();
ArrayList<String> lineas = leerArchivo("libros.txt");
for (int i = 0; i < lineas.size(); i++) {
String libro[] = {"", "", "", "", "", "", ""};
int c1 = lineas.get(i).indexOf("~T");
int c2 = lineas.get(i).indexOf("~A");
libro[0] = lineas.get(i).substring(c1 + 2, c2);
int c3 = lineas.get(i).indexOf("~A");
int c4 = lineas.get(i).indexOf("~E");
libro[1] = lineas.get(i).substring(c3 + 2, c4);
int c5 = lineas.get(i).indexOf("~E");
int c6 = lineas.get(i).indexOf("~Tem");
libro[2] = lineas.get(i).substring(c5 + 2, c6);
int c7 = lineas.get(i).indexOf("~Tem");
int c8 = lineas.get(i).indexOf("~Edi");
libro[3] = lineas.get(i).substring(c7 + 4, c8);
int c9 = lineas.get(i).indexOf("~Edi");
int c10 = lineas.get(i).indexOf("~Anho");
libro[4] = lineas.get(i).substring(c9 + 4, c10);
int c11 = lineas.get(i).indexOf("~Anho");
int c12 = lineas.get(i).indexOf("~I");
libro[5] = lineas.get(i).substring(c11 + 5, c12);
int c13 = lineas.get(i).indexOf("~I");
int c14 = lineas.get(i).indexOf("~End");
libro[6] = lineas.get(i).substring(c13 + 2, c14);
int z = i + 1;
Libro temp = new Libro(libro[0], libro[1], libro[2], libro[3], libro[4], libro[5], libro[6], z);
arrayListLibros.add(temp);
}
}
以前に初期化されていたので、どちらが問題なのかわかりません。
ありがとう :)