CONSTRUCTORS!

Discussion in 'Spigot Plugin Development' started by JeffLover, Jul 4, 2018.

?

Am I an idiot?

  1. Yes

    52 vote(s)
    52.5%
  2. maybe

    47 vote(s)
    47.5%
  1. Ahh, so. Constructors, my favorite. I have always tried to work around my issues, but this time I am done and I am going to get help.

    So. I am unsure how to put my problem into words. So i am going to show you my code and I am sure it is just a "basic java" mistake.

    In my class "ConfigurationFiles" I have this constructor for my main class.
    Code (Java):
    Main plugin;

        private ConfigurationFiles(Main instance) {
            plugin = instance;
        }
    So far so good right? Ok.
    in my main class I have this constructor
    Code (Text):
    ConfigurationFiles cf;
    Still good?

    If that wasn't the problem then I am sure this is.

    I have this in my onEnable
    Code (Java):
    cf.load();
    This is my load method.
    Code (Java):
    public void load() {
            defaults();
            plugin.getConfig().options().copyDefaults(true);
            plugin.saveConfig();
        }
    for you curious bunch to what the defaults method is here ya go.
    Code (Java):
    private void defaults() {
            plugin.getConfig().addDefault("chat.prefix", "&4[&3PA&4]");
            plugin.getConfig().addDefault("autosell.enable", true);
        }
    So now that you understand my code here is my fat error.
    Code (Text):
    java.lang.NullPointerException: null
            at me.kerchook.prisonaddons.Main.onEnable(Main.java:17) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:368) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:329) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(CraftServer.java:730) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.Bukkit.reload(Bukkit.java:534) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:629) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchServerCommand(CraftServer.java:615) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.aP(DedicatedServer.java:408) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:372) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:651) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:555) [craftbukkit.jar:git-Bukkit-809c399]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    For some reason this method is returning null. I tried to move it into the main class then run it and what do you know, it worked. So the problem is with my constructors. Yes, I know I can just write out the class name. but that is like so much effort and its about time I got around to understanding these things.
     
  2. No. Not good. Thats not a "constructor." Thats a declaration without an instantiation. So it's null.
     
  3. No, that isn't good. It isn't a constructor.
     
  4. So this is my problem. I am just realizing that I did not say this in the main post. So as you know I cannot have a constructor in my main class so for example if i were to try and add this constructor I would get the Abnormal plugin type error.
    Code (Java):
    ConfigurationFiles cf;
       
        private Main(ConfigurationFiles instance) {
            cf = instance;
        }
    So what is my workaround for this is what I am asking? I would assume i would do something along the lines of passing an instance of the other class into the main class. But then how would i then go on to declare that instance as cf?
     
  5. Wat.

    Solution is to instantiate it...
     
  6. You need to do this in your main class:
    Code (Text):
    ConfigurationFiles cf = new ConfigurationFiles(yourmainclass);
    or
    Code (Text):
    ConfigurationFiles cf = new ConfigurationFiles(this); <-- When in your main class
    Put your constructor back to what you had in the original post. This should solve your issue.

    Its not working because you never initialise the variable.
     
  7. You wanna assign a value to that variable in the onEnable since you cant do it in the constructor because Spigot doesn't allow that.
     
  8. Why couldn't you. I've been doing it without issues :p
     
  9. I have tried this already, and it still returns the Abnormal plugin type error, as shown here.
    Code (Text):
    org.bukkit.plugin.InvalidPluginException: Abnormal plugin type
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:56) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:326) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:248) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.loadPlugins(CraftServer.java:304) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(CraftServer.java:728) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.Bukkit.reload(Bukkit.java:534) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:629) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchServerCommand(CraftServer.java:615) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.aP(DedicatedServer.java:408) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:372) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:651) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:555) [craftbukkit.jar:git-Bukkit-809c399]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    Caused by: java.lang.InstantiationException: me.kerchook.prisonaddons.Main
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_171]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-809c399]
            ... 15 more
    Caused by: java.lang.NoSuchMethodException: me.kerchook.prisonaddons.Main.<init>()
            at java.lang.Class.getConstructor0(Unknown Source) ~[?:1.8.0_171]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_171]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-809c399]
            ... 15 more
    I am quite sure I have done this right, but correct me if I am wrong.
    Code (Java):
    public class Main extends JavaPlugin {

        public static Main plugin;
        PluginManager pm;
        ConfigurationFiles cf;
     
        private Main(ConfigurationFiles instance) {
            cf = instance;
        }

        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            plugin = this;
            regEvents();
    ConfigurationFiles cf = new ConfigurationFiles(this);
            cf.load();
           
        }
     
  10. add this to the configurationfiles class:
    Code (Text):
    private MainClassName plugin = (MainClassName)MainClassName.getPlugin(MainClassName.class);
    and add this to your main class:
    Code (Text):
    ConfigurationFiles configFiles = new ConfigurationFiles();
     
  11. No. No no no no no...

    Code (Java):
    public class Main extends JavaPlugin {
        ConfigurationFiles cf;

        public void onEnable() {
            cf = new ConfigurationFiles(this);
        }
    }
    You're not taking the time to process what you're learning about dependency injection and/or you're not understanding what Objects are.
     
    • Agree Agree x 2
  12. I dont get how this is any different than what I already have. It is just writing it another way.
     
  13. A constructor constructs an object. Your class inherits "JavaPlugin". The way your plugin is loaded is that Spigot creates a new object of your main class. But Spigot doesn't know how to use that constructor.
    Spigot doesn't know what "ConfigurationFiles" is nor can it arbitrarily pass one to your main classes constructor just because you added it.

    You can overload the constructor; that is to say, you NEED a constructor with no parameters,
    Code (Text):
    public Main() {
    and you can overload it by calling it's own constructor. But there's no point, it wouldn't really do anything unique that wouldn't be made more readable by just instantiating inside the normal constructor.
     
  14. This brings us back to the original null pointer error.
     
  15. Because you haven't instantiated something. Variables just refer to something. They can refer to nothing, or null. If they refer to nothing, you can't say "do this nothing's method". That's NullPointer.
     
  16. Well, I wasn't going to vote "yes" to that poll. But since I'm being ignored I'll go ahead and put my vote in. :/
     
  17. Yeah, when you say it like that it clicked. It was just one of those moments.
     
  18. I would do this in your ConfigurationFiles class:
    Code (Java):
    class ConfigurationFiles
    {
        private Main plugin;

        ConfigurationFiles(Main plugin)
        {
            this.plugin = plugin;
        }
    }
     
  19. He already does.
     
  20. I know. But I think it looks slightly cleaner in my opinion :)