Help

I am working on a plugin updater / version checker. I will start by sending the code first so it is easier to explain the issue
public final class SkriptPlus extends JavaPlugin {

private static SkriptPlus instance;
private static final Map<String, AddonService> services = Map.of(
"github", new GithubService(),
"skunity", new SkUnityService()
);

@Override
public void onEnable() {
instance = this;
...
public final class SkriptPlus extends JavaPlugin {

private static SkriptPlus instance;
private static final Map<String, AddonService> services = Map.of(
"github", new GithubService(),
"skunity", new SkUnityService()
);

@Override
public void onEnable() {
instance = this;
...
public class SkUnityService implements AddonService {

private static final String API_KEY;

static {
API_KEY = SkriptPlus.getInstance().getConfig().getString("update-checker.skunity-api-key", "disabled");
...
}
public class SkUnityService implements AddonService {

private static final String API_KEY;

static {
API_KEY = SkriptPlus.getInstance().getConfig().getString("update-checker.skunity-api-key", "disabled");
...
}
Since SkUnityService is loaded before onEnable() the static block errors. java.lang.NullPointerException: Cannot invoke "me.eren.skriptplus.SkriptPlus.getConfig()" because the return value of "me.eren.skriptplus.SkriptPlus.getInstance()" is null How would I go about doing this?
9 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @eren.! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2mo ago
What does the getInstance method do? Doesn't it create the instance if necessary? if it doesn't, what creates the instance?
eren.
eren.OP2mo ago
its the instance of a minecraft spigot plugin you either instance = this or Bukkit.getPluginManager().getPlugin(string) to get it, no other way as far as i know i tried both ways i didnt include the minecraft modding tag because i thought it would be more of a java issue maybe i should do some sort of loadServices() method rather than loading them in the field?
dan1st
dan1st2mo ago
oh I see so calling Bukkit.getPluginManager.getPlugin(...) in the static block returns null as well?
eren.
eren.OP2mo ago
yes because the plugin wasnt loaded yet
private static final Map<String, AddonService> services = new HashMap<>();

@Override
public void onEnable() {
instance = this;
loadAddonServices();
...
}

private static void loadAddonServices() {
services.put("github", new GithubService());
services.put("skunity", new SkUnityService());
}
private static final Map<String, AddonService> services = new HashMap<>();

@Override
public void onEnable() {
instance = this;
loadAddonServices();
...
}

private static void loadAddonServices() {
services.put("github", new GithubService());
services.put("skunity", new SkUnityService());
}
this works, unless you have a better idea ill close the thread
dan1st
dan1st2mo ago
Can you lazily initialize the API key?
eren.
eren.OP2mo ago
i could but i would rather not since im also validating the key
if (!API_KEY.equalsIgnoreCase("disabled") && API_KEY.length() != 32)
throw new RuntimeException("SkUnity API key is not 32 characters long. Are you sure you entered a valid key? " +
"You should set it to \"disabled\" if you don't want to use the SkUnity service.");
if (!API_KEY.equalsIgnoreCase("disabled") && API_KEY.length() != 32)
throw new RuntimeException("SkUnity API key is not 32 characters long. Are you sure you entered a valid key? " +
"You should set it to \"disabled\" if you don't want to use the SkUnity service.");
dan1st
dan1st2mo ago
oh yeah that's fine as well you could also do that when initializing it
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@587604022349791274>.
Want results from more Discord servers?
Add your server