-1

I'm trying to put keys and values in a HashMap from another class. The NPE should not happen. I have my instancing setup correctly, as I can access the methods in RingCreation without an issue, but when I try to put objects into a HashMap in Main, it returns a null pointer error, but when I make a debug message of the HashMap, it detects it not being null. I do not want to be a nuisance by posting "yet another NPE" log. I am usually able to fix most errors that appear.

Main class

HashMap<Integer,ItemStack> ringHolder = new HashMap<Integer,ItemStack>();
private RingCreation createRing;

private static Main instance;

public Main() {
    this.createRing = new RingCreation();

}

@Override
public void onEnable() {
    if(ringHolder.isEmpty()) System.out.println("ringholder is empty");
    instance = this;
    createRing.CreateLifeRing();
    createRing.CreateRegenerationRing();
    createRing.CreateStarterRing();
}

public static Main getInstance() {
    return instance;
}

}

RingCreation class:

private RingOfLife ringLife;
private StarterRing startRing;
private RingOfRegeneration regenRing;

private Main main = Main.getInstance();

public RingCreation() { 
    this.ringLife = new RingOfLife();
    this.startRing = new StarterRing();
    this.regenRing = new RingOfRegeneration();
}

ItemStack ringOfLife;
ItemMeta ringOfLifeMeta;

ItemStack starterRing;
ItemMeta starterRingMeta;

ItemStack ringOfRegeneration;
ItemMeta ringOfRegenerationMeta;

public void CreateLifeRing() {
    ringOfLife = new ItemStack(Material.RECORD_4);
    ringOfLifeMeta = ringOfLife.getItemMeta();
    ringOfLifeMeta.setDisplayName(ChatColor.GRAY+"Ring of"+ChatColor.GREEN+" Life");

    ringOfLifeMeta.addItemFlags(ItemFlag.values());
    ringOfLife.setItemMeta(ringOfLifeMeta);
    System.out.println("createlifering working");
    int loc = 2;
    main.ringHolder.put(loc, ringOfLife);
}

public void CreateStarterRing() {

    starterRing = new ItemStack(Material.RECORD_11);
    starterRingMeta = starterRing.getItemMeta();
    starterRingMeta.setDisplayName(ChatColor.GRAY+"Starter Ring");

    //removes the music disc lore V
    starterRingMeta.addItemFlags(ItemFlag.values());
    starterRing.setItemMeta(starterRingMeta);

    int loc = 1;
    main.ringHolder.put(loc, starterRing);
}

public void CreateRegenerationRing() {

    ringOfRegeneration = new ItemStack(Material.RECORD_5);
    ringOfRegenerationMeta = ringOfRegeneration.getItemMeta();
    ringOfRegenerationMeta.setDisplayName(ChatColor.GRAY+"Ring of"+ChatColor.GREEN+" Regeneration");
    ringOfRegenerationMeta.addItemFlags(ItemFlag.values());
    ringOfRegeneration.setItemMeta(ringOfRegenerationMeta);

    int loc = 3;
    main.ringHolder.put(loc, ringOfRegeneration);
}

Error:

[14:17:37 ERROR]: Error occurred while enabling VelocusRings v1.0 (Is it up to date?)
java.lang.NullPointerException: null

at me.redshadus.velocus.RingCreation.CreateLifeRing(RingCreation.java:44) ~[?:?]
at me.redshadus.velocus.Main.onEnable(Main.java:33) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at java.lang.Thread.run(Unknown Source) [?:?]

Main line 33:

createRing.CreateLifeRing();

RingCreation line 44:

main.ringHolder.put(loc, starterRing);

The console also outputs: createlifering working

Redshade
  • 35
  • 5
  • private Main main = Main.getInstance(); at that point (afaik) instance is still null – Stultuske Aug 29 '18 at 12:26
  • I decided to close this question as a duplicate after all, as it appears to be "yet another NPE" question. It does not differ in a substantial way. Have a look at the comments and answers for getting help. – Seelenvirtuose Aug 29 '18 at 12:28

2 Answers2

1

The issue is that you never initialize the instance of Main:
Change:

public static Main getInstance() {
    return instance;
}

to

public static Main getInstance() {
    if(instance == null) {
        instance = new Main();
    }
    return instance;
}

This is the typical pattern for a "lazy-loaded" singleton.
Also you should make the constructor private private Main() since I expect there should only be one instance of Main.

xtratic
  • 4,600
  • 2
  • 14
  • 32
  • It won't work. If I make the constructor private, it'll tell me that the constructor is not public, thus it can't run. If I use your method, it creates another Main instance which it not allowed in the Bukkit API, as JavaPlugin can only be extended once. I forgot to add class level code in the original question. – Redshade Aug 29 '18 at 12:36
  • hmm? In Java you don't need a public constructor for a program to run. My `getInstance` will only create an instance of main if there isn't one already and making the constructor private prevents anything else from creating another instance. It sounds like 'Bukkit' is the one trying to create an instance of `Main` (if it requires a public constructor) and maybe you shouldn't be creating it yourself. – xtratic Aug 29 '18 at 12:49
0

I think you forgot to create a Main instance.

Florian Salihovic
  • 3,921
  • 2
  • 19
  • 26