Azias Azias - 1 year ago 30
Java Question

Java function executing before the previous one ended

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.util.ArrayList;

import com.azias.awbe.Utils;
import com.azias.awbe.mod.ModInfo;
import com.azias.awbe.mod.ModLoader;

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);


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);
} catch (IOException e) {
System.err.println("Unable to load the \"modInfo.json\" file in \""+folder.getName()+"\"");
} 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.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()) {
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(

The Exception in thread "main" doesn't always show up.

Answer Source

Reading the comments, it seems there was a typo that caused the error. However, I'll still address the timing issue.

The apparent timing mismatch is because of the way the output streams work: Exception stack traces are printed to System.err, but you are printing the error message to System.out. When your IDE displays program output, sometimes one stream is a bit slower than another, and the order gets messed up.

To fix this, try printing all error messages through System.err. Within a single stream, messages never get reordered like this.