我正在创建一个程序,必须保存许多信息,其中一个存储矩阵,其中的想法是保持0或-1,这取决于项目是否存在(0),在其他情况下(-1).但是,当我尝试在方法中初始化此矩阵时,会生成nullPointerException.
我试图修复它像这样初始化矩阵:
public static int libros[][]= new int[arrayListLibros.size()][numeroMayor()];
Run Code Online (Sandbox Code Playgroud)
但它会产生另一个错误
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
Run Code Online (Sandbox Code Playgroud)
我没有找到解决方法
这是代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
其中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);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我不知道哪个是问题因为它之前被初始化了.
谢谢 :)
arrayListLibros我猜你没有初始化.加载类时加载静态内容.
public static int libros[][]= new int[arrayListLibros.size()][numeroMayor()];
Run Code Online (Sandbox Code Playgroud)
arrayListLibros尚未初始化,因此导致NullPointerException调用size(),从而导致ExceptionInInitializerError.
FROM API:
表示静态初始化程序中发生意外异常的信号.抛出ExceptionInInitializerError以指示在评估静态初始化程序或静态变量的初始化程序期间发生异常.
像下面的Simething:
public class StaticTestForLaura {
static List<String> arrayListLibros;
static {
arrayListLibros= new ArrayList<String>();
arrayListLibros.add("Laura");
}
public static int libros[]= new int[arrayListLibros.size()];
}
Run Code Online (Sandbox Code Playgroud)