Solved Creating instance of Main Class on another class.

Discussion in 'Spigot Plugin Development' started by Gober, Jun 14, 2018.

Thread Status:
Not open for further replies.
  1. I've been working on a plugin, for this month.
    But, couldn't make it to work on the first attempt (Made the Plugin, and Fails) and then I reset it again to make a new plugin with the same Idea. Still fail. Every attempt I tried to made the code cleaner doesn't work. And Instead made it fails.
    Now, I'm attempting to do it again, and it fails. So I decided to ask the spigot community about what am I doing wrong.
    Code (Text):
    [17:04:22 ERROR]: Error occurred while enabling ShipmentBox v1.0.0 (Is it up to date?)
    java.lang.NullPointerException: null
            at com.gmail.JyckoSianjaya.ShipmentBox2.TheBox.<init>(TheBox.java:22) ~[?:?]
            at com.gmail.JyckoSianjaya.ShipmentBox2.Events.<init>(Events.java:16) ~[?:?]
            at com.gmail.JyckoSianjaya.ShipmentBox2.ShipmentBox.onEnable(ShipmentBox.java:71) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[PaperSpigot-latest.jar:git-Paper-1288]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:316) [PaperSpigot-latest.jar:git-Paper-1288]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [PaperSpigot-latest.jar:git-Paper-1288]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:383) [PaperSpigot-latest.jar:git-Paper-1288]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:332) [PaperSpigot-latest.jar:git-Paper-1288]
            at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:442) [PaperSpigot-latest.jar:git-Paper-1288]
            at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:403) [PaperSpigot-latest.jar:git-Paper-1288]
            at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:341) [PaperSpigot-latest.jar:git-Paper-1288]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:292) [PaperSpigot-latest.jar:git-Paper-1288]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:616) [PaperSpigot-latest.jar:git-Paper-1288]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]

    Code (Java):
    TheBox class.
    ShipmentBox main;
    public TheBox() {
            main = main.getInstance(); //This is line 22
    }
    Code (Java):
    Events class
        public Events() { //This is line 16
            main = main.getInstance();
        }
    Code (Java):
            Bukkit.getServer().getPluginManager().registerEvents(new Events(), this); //Line 71 ShipmentBox (Main Class)
     
    Code (Java):
    Full Events.java class
    public class Events implements Listener {
        ShipmentBox main;
        public Events() {
            main = main.getInstance();
        }
        private void onBlockPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
            Block block = e.getBlockPlaced();
            if (block.getType() == Material.CHEST) {
                UUID uuid = p.getUniqueId();
                Chest chest = (Chest) block;
                Location b = chest.getLocation();
                Inventory inv = chest.getInventory();
                if (inv.getTitle().equals(main.Name)) {
                    main.sendActionBar(p, "&2&L** &2Shipment Box Placed &2&l**");
                }
            }
        }

    }
     
  2. We can't help you if you don't post the code that's throwing the exception.
     
  3. I just did, please take a look xD
     
  4. In the constructor TheBox(), you can say main = this; I think that should prevent the nullpointer
     
    • Informative Informative x 1
  5. Code (Text):
    main = main.getInstance(); //This is line 22
    What are you expecting? main is obviously null when you create a new instance of an object, yet you're using main.getInstance(). Did you mean to use a static method for getInstance()?
     
  6. No its not a static.
    Code (Java):
        public ShipmentBox getInstance() {
            return this;
        }
    I tought that should work xD. Tell me if im wrong
     
  7. No, I want to get the Main class instance.
     
  8. That doesn't make any sense, maybe you should go learn Java a bit more before getting into plugin development.
     
  9. Then use main = Shipmentbox.getInstance()
     
  10. I've learned java like dozens of times (at Sololearn.com).
    If I try using static Method I cannot return a this.
    If I return a ShipmentBox variable it will show up a error on console.
     
  11. Read up on static and dependency injection.
     
  12. You're trying to use something called singleton pattern, atleast a half baked version of it. The "getInstance()" stuff is so you can access the same ShipmentBox object, and there should only be one. In your ShipmentBox class, you should have a static ShipmentBox variable. In the constructor or onEnable, you'd set that variable to "this", meaning the object that was just created. Then you can access the same ShipmentBox object over and over anywhere by using ShipmentBox.variablename. The "getInstance()" method would be "return variablename", and variablename should likely be private.

    You need to understand what the keywords mean before you try to actually make something with Java. Static variables belong to the class itself, not an object. Thus what you're doing is giving the class itself a variable that holds your one object, that you can access anywhere.
     
    • Winner Winner x 1
  13. Thanks for the information :D!
    Hopefully I can get this to work.
     
  14. Oops, when I try to use the variables from it, it returns a null.
    Any solution?
    How I use it:
    Code (Java):

    // Command.java
    private ShipmentBox main = ShipmentBox.getInstance();
    private String msg = main.message;
    Code (Java):
    // ShipmentBox.java
        private static ShipmentBox maininstance;
        public static ShipmentBox getInstance() {
            return maininstance;
        }
        public String message = "Welcome! How are you?";
        public void onEnable() {
            maininstance = this;
     
  15. It shouldn't be returning null unless you're calling geInstance() before the plugin has been enabled/initialized. Maybe something else is throwing an NPE?

    Edit: You should use "ShipmentBox.maininstance = this;" in your onEnable(), this won't affect anything (atleast it shouldn't), but it makes it a lot more readable. Clarifies that it's a static variable that belongs to the class.
     
    • Like Like x 1
  16. Code (Text):
    private ShipmentBox main = ShipmentBox.getInstance();
    private String msg = main.message;

    You need to define:
    Code (Java):
    private ShipmentBox main = ShipmentBox.getInstance();
    first before using the "main" variable anywhere else.

    What I suggest you do is simply set the value of String msg on your Command.java constructor (make one if you haven't done this already)

    Should look like this:

    Code (Java):
    private ShipmentBox main;
    private String msg;

    Command(ShipmentBox main) {
    this.main = main;
    msg = main.message;
    }
     
    • Winner Winner x 1
Thread Status:
Not open for further replies.

Share This Page