[Solved] Calling a method from another class

I have a plugin in my main class which gets a bunch of strings from a config file and sets them as static Strings. I am using that as a sort of update for all of those. I would like to make another class call that method when it needs to refresh/update those values. I can call that method perfectly fine from my main class, but when it comes to others I get a null pointer when I try. I am guessing that it is a problem with my method of calling that method but I am not sure what the issue is hence why I’m posting here :slight_smile:
Below is my main class barring a few things of null importance.
`@Inject
public static main instance;

@Inject
public main mn;

@Inject
private Logger logger;

//config stuff
private ConfigurationNode cfg;

@Inject
@DefaultConfig(sharedRoot = false)
private File defaultCfg;

@Inject
@DefaultConfig(sharedRoot = false)
private ConfigurationLoader<CommentedConfigurationNode> cfgMgr;
//End of Config Stuff
//DefineRules
public static String R01 = "null";
public static String R02 = "null";
public static String R03 = "null";
public static String R04 = "null";
public static String R05 = "null";
public static String R06 = "null";
public static String R07 = "null";
public static String R08 = "null";
public static String R09 = "null";
public static String R10 = "null";
public static String R11 = "null";
public static String R12 = "null";
public static String R13 = "null";
public static String R14 = "null";
public static String R15 = "null";

public static String RD01 = "null";
public static String RD02 = "null";
public static String RD03 = "null";
public static String RD04 = "null";
public static String RD05 = "null";
public static String RD06 = "null";
public static String RD07 = "null";
public static String RD08 = "null";
public static String RD09 = "null";
public static String RD10 = "null";
public static String RD11 = "null";
public static String RD12 = "null";
public static String RD13 = "null";
public static String RD14 = "null";
public static String RD15 = "null";

@Listener
public void onPreInitialization(GamePreInitializationEvent e) {
    //new reload(this);

    commands();
    this.getLogger().info("Checking for Config File...");
    try {
        if (!defaultCfg.exists()) {
            getLogger().info("Config was not found. Generating one with default information.");
            defaultCfg.createNewFile();
            this.cfg = getCfgMgr().load();
            //Config Bits
            //Rules
            this.cfg.getNode("rules", "01").setValue("null");
            this.cfg.getNode("rules", "02").setValue("null");
            this.cfg.getNode("rules", "03").setValue("null");
            this.cfg.getNode("rules", "04").setValue("null");
            this.cfg.getNode("rules", "05").setValue("null");
            this.cfg.getNode("rules", "06").setValue("null");
            this.cfg.getNode("rules", "07").setValue("null");
            this.cfg.getNode("rules", "08").setValue("aha you can see this!!!");
            this.cfg.getNode("rules", "09").setValue("null");
            this.cfg.getNode("rules", "10").setValue("null");
            this.cfg.getNode("rules", "11").setValue("null");
            this.cfg.getNode("rules", "12").setValue("null");
            this.cfg.getNode("rules", "13").setValue("null");
            this.cfg.getNode("rules", "14").setValue("null");
            this.cfg.getNode("rules", "15").setValue("null");

            this.cfg.getNode("details", "01").setValue("null");
            this.cfg.getNode("details", "02").setValue("null");
            this.cfg.getNode("details", "03").setValue("null");
            this.cfg.getNode("details", "04").setValue("null");
            this.cfg.getNode("details", "05").setValue("null");
            this.cfg.getNode("details", "06").setValue("null");
            this.cfg.getNode("details", "07").setValue("null");
            this.cfg.getNode("details", "08").setValue("null");
            this.cfg.getNode("details", "09").setValue("null");
            this.cfg.getNode("details", "10").setValue("null");
            this.cfg.getNode("details", "11").setValue("null");
            this.cfg.getNode("details", "12").setValue("null");
            this.cfg.getNode("details", "13").setValue("null");
            this.cfg.getNode("details", "14").setValue("null");
            this.cfg.getNode("details", "15").setValue("null");
            //Other
            this.cfg.getNode("other", "WelcomeMessageIsEnabled").setValue("true");
            getCfgMgr().save(cfg);
            this.getLogger().info("Config File with default values has been created successfully!");
        } else {
            getLogger().info("Config is already created. :)");
            this.cfg = getCfgMgr().load();
        }
    } catch (Exception err) {
        getLogger().info("Error creating getting/creating config");
        getLogger().info("Error: " + err);
    }
}


public void commands() {
    //CommandSpec
    CommandSpec help = CommandSpec.builder()
            .description(Text.of("Command Help Section"))
            .permission("bhoputils.help")
            .executor(new help())
            .build();
    CommandSpec reload = CommandSpec.builder()
            .description(Text.of("Reload Configs for bhopUtils"))
            .permission("bhoputils.reload")
            .executor(new reload())
            .build();
    CommandSpec setrule = CommandSpec.builder()
            .description(Text.of("idc"))
            .permission("bhoputils.rules.setrule")
            .arguments(
                    GenericArguments.onlyOne(GenericArguments.string(Text.of("RuleNumber"))),
                    GenericArguments.remainingJoinedStrings(Text.of("StringRule"))
            )
            .executor(new setrule())
            .build();
    CommandSpec rule = CommandSpec.builder()
            .description(Text.of("Host Command."))
            .permission("bhoputils.player.host")
            .child(setrule, "set")
            .build();
    CommandSpec bhoputils = CommandSpec.builder()
            .description(Text.of("bhopUtils Host Command"))
            .permission("bhoputils")
            .child(help, "help")
            .child(reload, "reload")
            .build();
    CommandSpec rules1 = CommandSpec.builder()
            .description(Text.of("Page 1 of the Server Rules"))
            .permission("bhoputils.rules.base")
            .executor(new rules1())
            .build();
    CommandSpec rules2 = CommandSpec.builder()
            .description(Text.of("Page 2 of the Server Rules"))
            .permission("bhoputils.rules.base")
            .executor(new rules2())
            .build();
    CommandSpec rules3 = CommandSpec.builder()
            .description(Text.of("Page 3 of the Server Rules"))
            .permission("bhoputils.rules.base")
            .executor(new rules3())
            .build();
    CommandSpec burules = CommandSpec.builder()
            .description(Text.of("View the server rules!"))
            .permission("bhoputils.rules.base")
            .child(rules1, "1")
            .child(rules2, "2")
            .child(rules3, "3")
            .build();
    CommandSpec rules = CommandSpec.builder()
            .description(Text.of("Display the server Rules"))
            .permission("bhoputils.rules.base")
            .executor(new rules1())
            .build();
    //RegisterCommands
    Sponge.getCommandManager().register(this, rule, "rule");
    Sponge.getCommandManager().register(this, burules, "burules");
    Sponge.getCommandManager().register(this, bhoputils, "bhoputils", "bu");
    Sponge.getCommandManager().register(this, rules, "rules");

}

public void updateRules() {
    //Get Rules
    R01 = this.cfg.getNode("rules", "01").getString();
    R02 = this.cfg.getNode("rules", "02").getString();
    R03 = this.cfg.getNode("rules", "03").getString();
    R04 = this.cfg.getNode("rules", "04").getString();
    R05 = this.cfg.getNode("rules", "05").getString();
    R06 = this.cfg.getNode("rules", "06").getString();
    R07 = this.cfg.getNode("rules", "07").getString();
    R08 = this.cfg.getNode("rules", "08").getString();
    R09 = this.cfg.getNode("rules", "09").getString();
    R10 = this.cfg.getNode("rules", "10").getString();
    R11 = this.cfg.getNode("rules", "11").getString();
    R12 = this.cfg.getNode("rules", "12").getString();
    R13 = this.cfg.getNode("rules", "13").getString();
    R14 = this.cfg.getNode("rules", "14").getString();
    R15 = this.cfg.getNode("rules", "15").getString();
    //Get Details
    RD01 = this.cfg.getNode("details", "01").getString();
    RD02 = this.cfg.getNode("details", "02").getString();
    RD03 = this.cfg.getNode("details", "03").getString();
    RD04 = this.cfg.getNode("details", "04").getString();
    RD05 = this.cfg.getNode("details", "05").getString();
    RD06 = this.cfg.getNode("details", "06").getString();
    RD07 = this.cfg.getNode("details", "07").getString();
    RD08 = this.cfg.getNode("details", "08").getString();
    RD09 = this.cfg.getNode("details", "09").getString();
    RD10 = this.cfg.getNode("details", "10").getString();
    RD11 = this.cfg.getNode("details", "11").getString();
    RD12 = this.cfg.getNode("details", "12").getString();
    RD13 = this.cfg.getNode("details", "13").getString();
    RD14 = this.cfg.getNode("details", "14").getString();
    RD15 = this.cfg.getNode("details", "15").getString();
    this.getLogger().info("Rules Reloaded");
    try {
        getCfgMgr().save(cfg);
        this.getLogger().info("Config Saved.");
    } catch (IOException e) {
        e.printStackTrace();
    }

Below is my second class which I want to access “updateRules” from.

package com.bhopahk.bhoputils.commands;

import com.bhopahk.bhoputils.main;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;

public class reload implements CommandExecutor {
    private main mn;

@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    mn = main.instance.mn;

    mn.updateRules();
    return CommandResult.success();
}

Thanks anyone for help on this. Not sure if its something obvious which I’m missing. Also this is the first time I have posted something like this so I’m not really sure if im missing anything.

You can’t inject statics.

Also, that’s really spaghetti. Why not just store rules in a list?

Less experience with lists. I am aware that I could use a list then just use pagination and it would be much more simple but that doesn’t give the detail that I’m after. I will likely look into lists in the future. Thanks though for that bit with statics I’ll try to fix that

First of all, actually learning Java is a good start.
RD01 and so on could easily be replaced with this:

for (int i = 0; i <= 15; i++) {
    getClass().getField("RD" + (i < 10 ? "0" : "") + i).set(this, this.cfg.getNode("details", Integer.toString(i));
}

Also, just use dependency injection, as in normal Java dependency injection, not Guice injection.

Okay so temporarily ignoring the “better” option which you mentioned. When using Java DI it gives the same null pointer.

What null pointer? You haven’t ever actually posted an exception, nor a line number.
Where is it? What? Why?

Also, to use that singleton setup, use something like this:

class Main  {
    private static Main instance;
    public static Main getInstance() { return instance; }

    @Listener
    public void construction(GameConstructionEvent event) { instance = this; }
}

main line 253 is
R01 = this.cfg.getNode("rules", "01").getString();
reload line 15 is
this.mn.updateRules();

I changed the reload class was for ^
private main mn = new main(); @Override public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException { this.mn.updateRules(); return CommandResult.success(); }

That is the where and what but I am here because I don’t know why.

Holy mother of statics

1 Like

learning Java is a good start.

good start

^useful. very useful.

Thanks Proximyst by the way. I have actually looked into for statements and plan on using them a lot more. (also your other solution worked :slight_smile: )

What do you mean, normal Java dependency injection?

private final SomeClass someClass;
public ThisClass(final SomeClass someClass) {
    this.someClass = someClass;
}

And not @Inject stuff.