Runtime Error

I am trying to do a /spawn and /setspawn command but nothing is working, I get these constant reflect errors in the console.

##Main Plugin File

@Plugin(id = "LS-Core", name = "Last Stand Core", version = "1.0")
public class Core {

    public static Core instance;

    @Inject public Logger logger;
    @Inject public Game game;

    @Inject @ConfigDir(sharedRoot = false)
    private File configDir;

    public ConfigurationLoader<CommentedConfigurationNode> config;
    public ConfigurationLoader<CommentedConfigurationNode> data;
    public ConfigurationNode configCache;
    public ConfigurationNode dataCache;

    public ConfigurationLoader<CommentedConfigurationNode> getLoader(String configName) throws IOException {
        File configFile = new File(this.configDir, configName);
        if(!configFile.exists()){
            configFile.getParentFile().mkdirs();
            configFile.createNewFile();
        }
        return HoconConfigurationLoader.builder().setFile(configFile).build();
    }

    @Subscribe
    public void onPreInitialization(PreInitializationEvent event) {
        try {
            config = this.getLoader("config.conf");
            data = this.getLoader("data.conf");
            configCache = config.load();
            dataCache = data.load();
       } catch (IOException e) { e.printStackTrace(); }
    }

    @Subscribe
    public void onInitialization(InitializationEvent event) {

        // Other Commands

        CommandSpec setspawn = CommandSpec.builder()
                .description(Texts.of("Set the Spawn of the Server"))
                .permission("core.command.admin.setspawn")
                .executor(new SetSpawn())
                .build();

        CommandSpec spawn = CommandSpec.builder()
                .description(Texts.of("Teleport Player to spawn point"))
                .permission("core.command.default.spawn")
                .executor(new Spawn())
                .build();

        CommandService cmdService = game.getCommandDispatcher();

        cmdService.register(this, setspawn, "setspawn");
        cmdService.register(this, spawn, "spawn");

}

And now I haven’t even gotten to working on the /spawn command, but so far this is what I have for my /setspawn command.

##Set Spawn File
public class SetSpawn implements CommandExecutor {

    private ConfigurationNode config;
    private ConfigurationLoader<CommentedConfigurationNode> configManager;
    private Game game;

    public SetSpawn() {
        this.config        = Core.instance.dataCache;
        this.configManager = Core.instance.data;
        this.game          = Core.instance.game;
    }

    @Override
    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {

    if (src instanceof Player) {
        try {

            Location playerLocation = Core.instance.game.getServer().getPlayer(src.getName()).get().getLocation();
            Vector3d playerRot = Core.instance.game.getServer().getPlayer(src.getName()).get().getRotation();

            config.getNode("spawn", "position", "X").setValue(playerLocation.getX());
            config.getNode("spawn", "position", "Y").setValue(playerLocation.getY());
            config.getNode("spawn", "position", "Z").setValue(playerLocation.getZ());
            config.getNode("spawn", "rotation", "X").setValue(playerRot.getX());
            config.getNode("spawn", "rotation", "Y").setValue(playerRot.getY());
            config.getNode("spawn", "rotation", "Z").setValue(playerRot.getZ());
            configManager.save(config);

        } catch (IOException e) { e.printStackTrace(); }
    }

And now the errors I get in the console…

[14:46:23] [Server thread/ERROR]: [Plugin Class: laststandstudio.core.Core][Handler: onInitialization]
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_45]
    at org.spongepowered.mod.plugin.SpongeModPluginContainer.handleModStateEvent(SpongeModPluginContainer.java:126)     [SpongeModPluginContainer.class:1.8-1487-2.1DEV-516+unknown-b516.git-unknown]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_45]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_45]
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) [minecraft_server-1.8.jar:?]
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) [minecraft_server-1.8.jar:?]
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) [minecraft_server-1.8.jar:?]
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) [minecraft_server-1.8.jar:?]
at com.google.common.eventbus.EventBus.post(EventBus.java:275) [minecraft_server-1.8.jar:?]
at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:212) [LoadController.class:?]
at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:190) [LoadController.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_45]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_45]
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) [minecraft_server-1.8.jar:?]
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) [minecraft_server-1.8.jar:?]
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) [minecraft_server-1.8.jar:?]
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) [minecraft_server-1.8.jar:?]
at com.google.common.eventbus.EventBus.post(EventBus.java:275) [minecraft_server-1.8.jar:?]
at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:119) [LoadController.class:?]
at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:710) [Loader.class:?]
at net.minecraftforge.fml.server.FMLServerHandler.finishServerLoading(FMLServerHandler.java:97) [FMLServerHandler.class:?]
at net.minecraftforge.fml.common.FMLCommonHandler.onServerStarted(FMLCommonHandler.java:360) [FMLCommonHandler.class:?]
at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:210) [po.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:438) [MinecraftServer.class:?]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
Caused by: java.lang.NullPointerException
at laststandstudio.core.commands.SetSpawn.<init>(SetSpawn.java:29) ~[SetSpawn.class:?]
at laststandstudio.core.Core.onInitialization(Core.java:81) ~[Core.class:?]
... 32 more

This is basicly saying that

    this.config = Core.instance.dataCache;

from the SetSpawn Constructor returns null when I set the value to this.getLoader(“data.conf”).load() right here.

@Subscribe
    public void onPreInitialization(PreInitializationEvent event) {
        try {
            config = this.getLoader("config.conf");
            data = this.getLoader("data.conf");
            configCache = config.load();
            dataCache = data.load();
        } catch (IOException e) { e.printStackTrace(); }
    }

How does this make any sence if the PRE-Initialiazation code is executed before any player could execute a /setspawn command…??

Instead of using static everywhere. Try using an constructor for your SetSpawn class. Static is in most cases bad practice ;).

Anyway to fix your error you need to add this line to the PreInitializationEvent.

instance = this;
1 Like

I only use static for the instance, but you are saying that I should pass all of the values I need for the function through the constructor? Instead of trying to use public instance variables acessed via a static instance.

Thank you, I can’t believe I forgot something as stupid as that! xD <3

Not exactly. Pass an instance of Core to your SetSpawn class. And use getter’s to get these fields from your core class. Gives you exactly the same setup. Only you don’t need static than :smile:.

Also interesting to read ^

1 Like

I agree this code needs much touching up. I was just trying to rush it done and I was stressed with the terrible Java Reflect Errors :persevere:. But I will definatly make it much much cleaner later. :smile:

1 Like