I'm trying to create an ArrayList of custom objects loaded with gson to get informations from it later on, but it seems like Java is executing other functions before the first one returned anything and it's causing a NullPointerException.
It seems to always happen after the first object has been loaded with gson.
Main class:
package com.azias.awbe.tests;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import com.azias.awbe.Utils;
import com.azias.awbe.mod.ModInfo;
import com.azias.awbe.mod.ModLoader;
import com.google.gson.Gson;
public class ModLoadingTest {
public static void main(String[] args) {
ArrayList<ModInfo> availableMods = loadModsList();
String[] modIds = args[0].split(";");
ModInfo[] modsToLoad = new ModInfo[modIds.length];
for(int i=0; i<modIds.length; i++) {
for(ModInfo mod : availableMods) {
if(mod.getId().equals(modIds[i])) { //This is the line 26.
modsToLoad[i] = mod;
}
}
}
ModLoader modLoader = new ModLoader(modsToLoad);
modLoader.loadModCode();
}
public static ArrayList<ModInfo> loadModsList() {
ArrayList<ModInfo> mods = new ArrayList<ModInfo>();
ArrayList<File> folders = Utils.listFolders("./assets");
for(File folder: folders) {
File modInfoFile = new File(folder.getAbsolutePath()+"/modinfo.json");
if(modInfoFile.isFile()) {
try {
String json = Utils.fileToString(modInfoFile.getAbsolutePath());
ModInfo mod = new Gson().fromJson(json, ModInfo.class);
mods.add(mod);
} catch (IOException e) {
System.err.println("Unable to load the "modInfo.json" file in ""+folder.getName()+""");
e.printStackTrace();
}
} else {
System.out.println("The folder named ""+folder.getName()+"" does not countain a modinfo.json file.");
}
}
return mods;
}
}
Utils class:
package com.azias.awbe;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class Utils {
/**
* List all the folder under a directory
* @param directoryName to be listed
*/
public static ArrayList<File> listFolders(String directoryName) {
ArrayList<File> files = new ArrayList<File>();
File directory = new File(directoryName);
File[] fList = directory.listFiles();
for(File file : fList) {
if (file.isDirectory()) {
files.add(file);
}
}
return files;
}
public static String fileToString(String path) throws IOException {
return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
}
}
Console log:
The folder named "customMaps" does not countain a modinfo.json file.
Exception in thread "main"
The folder named "launcher" does not countain a modinfo.json file.
The folder named "modtest" does not countain a modinfo.json file.
java.lang.NullPointerException at com.azias.awbe.tests.ModLoadingTest.main(ModLoadingTest.java:26)
The Exception in thread "main" doesn't always show up.
Aucun commentaire:
Enregistrer un commentaire