我正在创建一个程序,该程序必须保存许多信息,其中一个存储一个矩阵,其想法是根据项目是否存在(0)保持0或-1,在其他情况下(-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);
    }
}
所以我不知道是哪个问题,因为它之前已初始化。
谢谢 :)