gpt4 book ai didi

java - 如何根据程序启动时生成的数据初始化全局矩阵?

转载 作者:行者123 更新时间:2023-12-01 17:27:34 25 4
gpt4 key购买 nike

我正在创建一个程序,它必须保存许多信息,其中一个存储一个矩阵,其中的想法是保留 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);
}
}

所以我不知道是哪个问题,因为它之前已经初始化过。

谢谢:)

最佳答案

我猜你的arrayListLibros尚未初始化。加载类时会加载静态内容。

public static int libros[][]= new int[arrayListLibros.size()][numeroMayor()];

arrayListLibros尚未初始化,因此在调用size()时会导致NullPointerException,从而导致ExceptionInInitializerError

来自 API:

Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

编辑:

类似如下:

public class StaticTestForLaura {
static List<String> arrayListLibros;
static {
arrayListLibros= new ArrayList<String>();
arrayListLibros.add("Laura");
}
public static int libros[]= new int[arrayListLibros.size()];

}

关于java - 如何根据程序启动时生成的数据初始化全局矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13744084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com