Solved How to properly instance these classes

Discussion in 'Plugin Development' started by Lightcaster5, Feb 19, 2020.

  1. Offline

    Lightcaster5

    I'm facing an issue with multiple classes...
    Class1.java is trying to instance Cass2.java
    BUT
    Class2.java is trying to instance Class1.java
    When this happens and the server starts, I get a StackOverflowError because they are constantly trying to pull from each other.
    Does anyone know what I should do to solve this?
    Console Error (It's long) (open)

    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.StackOverflowError
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
    Caused by: java.lang.StackOverflowError
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:7) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]
        at me.lightcaster5.hostilityminigame.SQL.SQLManager.<init>(SQLManager.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.utils.LevelEvaluator.<init>(LevelEvaluator.java:9) ~[?:?]


    EDIT: For those who are wondering, the lines from the console error message are the lines where I'm attempting to instance the other class.
     
    Last edited: Feb 19, 2020
  2. Offline

    Wick

    Can we see relevant code please?
     
  3. Offline

    bowlerguy66

    @Lightcaster5 Make an instance of both of your classes in your main plugin class, then you can pass your main plugin class to each of your new classes. Then, in your new classes, you can access the instance of your other class from your main class; using your main plugin class as a bridge between your new classes so that you're not creating a new object every time
     
  4. Offline

    Lightcaster5

    Alright, I add a bit to the classes and after uploading it, restarting my server, I got a very weird error. Here's a bit of my code along with the error I received

    Code:
    public class Main extends JavaPlugin {
        public final LevelEvaluator levelevaluator;
        public final SQLManager sqlmanager;
     
        private static Main instance;
    
        public Main(LevelEvaluator levelevaluator, SQLManager sqlmanager){
            if(Main.instance != null) {
                throw new Error("Plugin already initialized!");
            }
    
            Main.instance = this;
            this.levelevaluator = levelevaluator;
            this.sqlmanager = sqlmanager;
        }
    
        public static Main getInstance(){
            return instance;
        }
    // Below this is my onEnable and whatnot...
    Code:
    public class SQLManager {
    
        private final Main plugin = Main.getInstance();
        private final LevelEvaluator levelevaluator = plugin.levelevaluator;
    Code:
    public class LevelEvaluator {
    
        private final Main plugin = Main.getInstance();
        private final SQLManager sql = plugin.sqlmanager;
    Code:
    org.bukkit.plugin.InvalidPluginException: Abnormal plugin type
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:80) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
    Caused by: java.lang.InstantiationException: me.lightcaster5.hostilityminigame.Main
        at java.lang.Class.newInstance(Class.java:427) ~[?:1.8.0_222]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        ... 7 more
    Caused by: java.lang.NoSuchMethodException: me.lightcaster5.hostilityminigame.Main.<init>()
        at java.lang.Class.getConstructor0(Class.java:3082) ~[?:1.8.0_222]
        at java.lang.Class.newInstance(Class.java:412) ~[?:1.8.0_222]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        ... 7 more
     
  5. Offline

    timtower Moderator Moderator

    @Lightcaster5 You can't use a constructor for your main class.
     
  6. Offline

    Lightcaster5

    Oh dang. I changed where I instance the two classes and got this:
    Code:
        public final LevelEvaluator levelevaluator = new LevelEvaluator();
        public final SQLManager sqlmanager = new SQLManager();
    
        private static Main instance;
    
        public Main() {
            if (Main.instance != null) {
                throw new Error("Plugin already initialized!");
            }
    
            Main.instance = this;
        }
    
        public static Main getInstance() {
            return instance;
        }
    How would I create instances of my main class in different classes without using this method?
     
  7. Offline

    timtower Moderator Moderator

    @Lightcaster5 No constructors in your main class.
    You have the onEnable for that.
    In the onEnable you make a new LevelEvaluator where you pass the main instance to it.
    You do the same for the SQLManager

    Then you make a getter for both in the main class.
    As they can both access the main class then you access each other.
    No need for the static either.
     
  8. Offline

    Lightcaster5

    I understand that part but I don't know how to go about instancing my main class in other classes without using this getInstance method I had
     
  9. Offline

    timtower Moderator Moderator

    You pass it along using a constructor.
     
  10. Offline

    Lightcaster5

    The second I try to add anything like this to access the main class, all of my other classes relating to SQLManager give me an error which is why I have been trying to eliminate using this
    Code:
        private final Main plugin;
       
        public SQLManager(Main plugin) {
            this.plugin = plugin;
        }
     
  11. Offline

    timtower Moderator Moderator

    But that is exactly how it should be done.
     
  12. Offline

    Lightcaster5

    ok :(
     
  13. Offline

    timtower Moderator Moderator

    That is a constructor that passes along the main instance.
    What errors do you get then?
     
  14. Offline

    Lightcaster5

    Well
    Well I remember I kept getting null pointer exceptions from both classes. I just left for school so unfortunately this will have to wait for me. I’ll be able to provide more about the error at around 6:30 pm EST. Have a good day Tim!
     
  15. Offline

    timtower Moderator Moderator

    Then we will try to fix them when you get back ;)
     
  16. Offline

    Lightcaster5

    Alright I'm back... Here is what I currently have:
    Console Error (open)

    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
    Caused by: java.lang.NullPointerException
        at me.lightcaster5.hostilityminigame.utils.ChatPrefixEvaluator.<init>(ChatPrefixEvaluator.java:18) ~[?:?]
        at me.lightcaster5.hostilityminigame.Main.<init>(Main.java:48) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_222]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_222]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_222]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_222]
        at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_222]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        ... 6 more

    Main class (Important stuff) (open)

    Code:
    Line 48:     final ChatPrefixEvaluator chatprefixevaluator = new ChatPrefixEvaluator(this);
    
    private SQLManager sqlmanager = new SQLManager(this);
    
    public SQLManager getSQL() {
        return sqlmanager;
    }

    ChatPrefixEvaluator class (the line that is causing the problem is the last one) (open)

    Code:
        private Main plugin;
    
        public ChatPrefixEvaluator(Main plugin) {
            this.plugin = plugin;
        }
    
        private final SQLManager sql = plugin.getSQL();
     
    Last edited: Feb 20, 2020
  17. Offline

    timtower Moderator Moderator

    @Lightcaster5 Initialize it in the constructor. Not outside of it.
     
  18. Offline

    Lightcaster5

    Slight change of things actually... I completely removed the LevelEvaluator class because I honestly didnt need it and I could put it all in the SQLManager class... Now im facing the issue with instancing my main class.
    Code:
        private Main plugin;
    
        public SQLManager(Main plugin) {
            this.plugin = plugin;
        }
    This doesnt work and I keep getting a nullpointerexception
     
  19. Offline

    timtower Moderator Moderator

    Are you accessing main outside a method in that class?
     
  20. Offline

    Lightcaster5

    No im defining a new instance of it and calling it with 'plugin'
     
  21. Offline

    timtower Moderator Moderator

    Please post your full class and stack trace.
     
  22. Offline

    Lightcaster5

    Hmmm....

    Well here is my main class:
    Main (open)

    Code:
    package me.lightcaster5.hostilityminigame;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.lightcaster5.hostilityminigame.SQL.MySQL;
    import me.lightcaster5.hostilityminigame.SQL.SQLManager;
    import me.lightcaster5.hostilityminigame.commands.BuilderCMD;
    import me.lightcaster5.hostilityminigame.commands.ClearChatCMD;
    import me.lightcaster5.hostilityminigame.commands.ExperienceCMD;
    import me.lightcaster5.hostilityminigame.commands.GoldCMD;
    import me.lightcaster5.hostilityminigame.commands.LevelCMD;
    import me.lightcaster5.hostilityminigame.commands.MainCMD;
    import me.lightcaster5.hostilityminigame.commands.MainCMDTabCompleter;
    import me.lightcaster5.hostilityminigame.commands.MuteChatCMD;
    import me.lightcaster5.hostilityminigame.commands.PrestigeCMD;
    import me.lightcaster5.hostilityminigame.commands.RepeatsCMD;
    import me.lightcaster5.hostilityminigame.commands.RulesCMD;
    import me.lightcaster5.hostilityminigame.listeners.BlockPlaceListener;
    import me.lightcaster5.hostilityminigame.listeners.PlayerAsyncChatListener;
    import me.lightcaster5.hostilityminigame.listeners.PlayerHitPlayerListener;
    import me.lightcaster5.hostilityminigame.listeners.PlayerInventoryClickListener;
    import me.lightcaster5.hostilityminigame.listeners.PlayerJoinListener;
    import me.lightcaster5.hostilityminigame.listeners.PlayerQuitListener;
    import me.lightcaster5.hostilityminigame.scoreboard.ScoreboardManager;
    import me.lightcaster5.hostilityminigame.utils.AttackActionBar;
    import me.lightcaster5.hostilityminigame.utils.MapHandler;
    
    public class Main extends JavaPlugin {
    
        final AttackActionBar aab = new AttackActionBar();
    
        public Connection connection = null;
        private String host, database, username, password, port;
    
        private final MapHandler maphandler = new MapHandler();
        private final ScoreboardManager scoreboardmanager = new ScoreboardManager(this);
    
        private static Main instance;
       
        public static Main getInstance() {
            return instance;
        }
       
        @Override
        public void onEnable() {
            saveDefaultConfig();
            if (getConfig().getBoolean("enabled")) {
    
                instance = this;
               
                host = getConfig().getString("host");
                port = getConfig().getString("port");
                database = getConfig().getString("database");
                username = getConfig().getString("username");
                password = getConfig().getString("password");
    
                MySQL mysql = new MySQL(host, port, database, username, password);
                connect(mysql);
    
                new SQLManager(this);
               
                getServer().getPluginManager().registerEvents(new PlayerHitPlayerListener(this), this);
                getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
                getServer().getPluginManager().registerEvents(new PlayerQuitListener(), this);
                getServer().getPluginManager().registerEvents(new PlayerAsyncChatListener(this), this);
                getServer().getPluginManager().registerEvents(new BlockPlaceListener(this), this);
                getServer().getPluginManager().registerEvents(new PlayerInventoryClickListener(), this);
    
                this.getCommand("hostilityminigame").setExecutor(new MainCMD(this));
                this.getCommand("hostilityminigame").setTabCompleter(new MainCMDTabCompleter());
                this.getCommand("prestige").setExecutor(new PrestigeCMD(this));
                this.getCommand("gold").setExecutor(new GoldCMD(this));
                this.getCommand("experience").setExecutor(new ExperienceCMD(this));
                this.getCommand("level").setExecutor(new LevelCMD(this));
    
                this.getCommand("builder").setExecutor(new BuilderCMD());
                this.getCommand("repeats").setExecutor(new RepeatsCMD());
                this.getCommand("clearchat").setExecutor(new ClearChatCMD(this));
                this.getCommand("rules").setExecutor(new RulesCMD());
                this.getCommand("mutechat").setExecutor(new MuteChatCMD());
                maphandler.resetRepeats();
                maphandler.onlineListReset();
    
                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    @Override
                    public void run() {
                        Bukkit.getServer().getConsoleSender().sendMessage("§bNow starting updater...");
                        scoreboardmanager.start();
                    }
                }, 20L);
            } else {
                getServer().getPluginManager().disablePlugin(this);
                Bukkit.getServer().getConsoleSender().sendMessage("§4§lDisabling plugin automatically.");
                Bukkit.getServer().getConsoleSender()
                        .sendMessage("§4To re-enable, change \'enabled\' to true in config.yml");
            }
        }
    
        @Override
        public void onDisable() {
            Bukkit.getServer().getConsoleSender().sendMessage("§cSaving and disabling HostilityMinigame...");
            saveConfig();
            maphandler.onlineListReset();
            Bukkit.getServer().getConsoleSender().sendMessage("§cSave complete and disabled.");
        }
    
        public void connect(MySQL mysql) {
            Bukkit.getServer().getConsoleSender().sendMessage("Now attepting to connect to SQL Database...");
            try {
                connection = mysql.openConnection();
                Statement statement = connection.createStatement();
                String create = "CREATE TABLE IF NOT EXISTS PlayerData (`UUID` varchar(255), `Name` varchar(255), `Experience` int, `Level` int, `Prestige` int, `Gold` decimal(8,2));";
                statement.execute(create);
                Bukkit.getServer().getConsoleSender().sendMessage("§aSQL Database is now connected!");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                Bukkit.getServer().getConsoleSender().sendMessage(
                        "§4THERE WAS AN ERROR TRYING TO CONNECT TO THE SQL DATABASE, PLEASE SEE ERROR REPORT ABOVE!");
            } catch (SQLException e) {
                e.printStackTrace();
                Bukkit.getServer().getConsoleSender().sendMessage(
                        "§4THERE WAS AN ERROR TRYING TO CONNECT TO THE SQL DATABASE, PLEASE SEE ERROR REPORT ABOVE!");
            }
        }
    
        public String getVersion() {
            return "v" + this.getDescription().getVersion().toString();
        }
    
    }
    


    SQLManager class:
    SQLManager (open)

    Code:
    package me.lightcaster5.hostilityminigame.SQL;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    import me.lightcaster5.hostilityminigame.Main;
    import me.lightcaster5.hostilityminigame.utils.TablistPrefixEvaluator;
    
    public class SQLManager {
    
        private final Main plugin = Main.getInstance();
    
        public SQLManager(Main plugin) {
        }
    
        private final TablistPrefixEvaluator tabEvaluator = new TablistPrefixEvaluator();
    
        /* Main Functions */
    
        public void addNewPlayer(Player player) {
            try {
                Statement statement = plugin.connection.createStatement();
                if (checkPlayerWithUUID(player.getUniqueId())) {
                    return;
                } else {
                    String add = "INSERT INTO `PlayerData` (`UUID`, `Name`, `Experience`, `Level`, `Prestige`, `Gold`) VALUES ('"
                            + player.getUniqueId().toString() + "','" + player.getName() + "','0','1','0','0.00')";
                    statement.execute(add);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public boolean checkPlayerWithUUID(UUID uuid) {
            boolean exists = false;
            try {
                Statement statement = plugin.connection.createStatement();
                ResultSet getnumbers = statement
                        .executeQuery("SELECT * FROM PlayerData WHERE UUID ='" + uuid.toString() + "';");
                if (getnumbers.next()) {
                    exists = true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return exists;
        }
    
        public boolean checkPlayerWithString(String playerName) {
            boolean exists = false;
            try {
                Statement statement = plugin.connection.createStatement();
                ResultSet getnumbers = statement.executeQuery("SELECT * FROM PlayerData WHERE Name ='" + playerName + "';");
                if (getnumbers.next()) {
                    exists = true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return exists;
        }
    
        public void updatePlayerName(UUID uuid, Player player) {
            try {
                Statement statement = plugin.connection.createStatement();
                String update = ("UPDATE `PlayerData` SET `Name`= '" + player.getName() + "' WHERE UUID ='"
                        + uuid.toString() + "';");
                statement.execute(update);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        /* Player Experience */
    
        public void setPlayerExperience(UUID uuid, int experience) {
            int finalExperience;
            int prestige = getPlayerPrestige(uuid);
            if (((5 + prestige) * ((120) * (120))) < experience) {
                finalExperience = ((5 + prestige) * ((120) * (120)));
            } else {
                finalExperience = experience;
            }
    
            try {
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Experience`= '" + finalExperience + "' WHERE UUID ='"
                        + uuid.toString() + "';");
                statement1.execute(set);
                updateLevel(uuid);
                updateTabListWithUUID(uuid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void addPlayerExperience(UUID uuid, int amount) {
            try {
    
                int currentExperience = getPlayerExperience(uuid);
                int finalExperience = currentExperience + amount;
    
                int prestige = getPlayerPrestige(uuid);
                if (((5 + prestige) * ((120) * (120))) < finalExperience) {
                    finalExperience = ((5 + prestige) * ((120) * (120)));
                }
    
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Experience`= '" + finalExperience + "' WHERE UUID ='"
                        + uuid.toString() + "';");
                statement1.execute(set);
                updateLevel(uuid);
                updateTabListWithUUID(uuid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void removePlayerExperience(UUID uuid, int amount) {
            try {
    
                int currentExperience = getPlayerExperience(uuid);
                int finalExperience = currentExperience - amount;
    
                if (finalExperience <= 0) {
                    resetPlayerExperience(uuid);
                    updateLevel(uuid);
                    updateTabListWithUUID(uuid);
                } else {
                    Statement statement1 = plugin.connection.createStatement();
                    String set = ("UPDATE `PlayerData` SET `Experience`= '" + finalExperience + "' WHERE UUID ='"
                            + uuid.toString() + "';");
                    statement1.execute(set);
                    updateLevel(uuid);
                    updateTabListWithUUID(uuid);
                }
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public int getPlayerExperience(UUID uuid) {
            int experience = 0;
            try {
                Statement statement = plugin.connection.createStatement();
                ResultSet getnumbers = statement
                        .executeQuery("SELECT * FROM PlayerData WHERE UUID ='" + uuid.toString() + "';");
                getnumbers.next();
    
                experience = getnumbers.getInt("Experience");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return experience;
        }
    
        public void resetPlayerExperience(UUID uuid) {
            try {
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Experience`= '0' WHERE UUID ='" + uuid.toString() + "';");
                statement1.execute(set);
                updateTabListWithUUID(uuid);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        /* Player Level */
    
        public int getPlayerLevel(UUID uuid) {
            int level = 0;
            try {
                Statement statement = plugin.connection.createStatement();
                ResultSet getnumbers = statement
                        .executeQuery("SELECT * FROM PlayerData WHERE UUID ='" + uuid.toString() + "';");
                getnumbers.next();
                level = getnumbers.getInt("Level");
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
            return level;
        }
    
        public void setPlayerLevel(UUID uuid, int level) {
            try {
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Level`= '" + level + "' WHERE UUID ='" + uuid.toString() + "';");
                statement1.execute(set);
                updateTabListWithUUID(uuid);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void resetPlayerLevel(UUID uuid) {
            try {
                Statement statement1 = plugin.connection.createStatement();
                String setLevel = ("UPDATE `PlayerData` SET `Level`= '1' WHERE UUID ='" + uuid.toString() + "';");
                String setExperience = ("UPDATE `PlayerData` SET `Experience`= '0' WHERE UUID ='" + uuid.toString() + "';");
                statement1.execute(setLevel);
                statement1.execute(setExperience);
                updateTabListWithUUID(uuid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        /* Player Gold */
    
        public void setPlayerGold(UUID uuid, double gold) {
            try {
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Gold`= '" + gold + "' WHERE UUID ='" + uuid.toString() + "';");
                statement1.execute(set);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void addPlayerGold(UUID uuid, double amount) {
            try {
    
                double currentGold = getPlayerGold(uuid);
                double finalGold = currentGold + amount;
    
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Gold`= '" + finalGold + "' WHERE UUID ='" + uuid.toString() + "';");
                statement1.execute(set);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void removePlayerGold(UUID uuid, double amount) {
            try {
    
                double currentGold = getPlayerGold(uuid);
                double finalGold = currentGold - amount;
    
                if (finalGold <= 0) {
                    resetPlayerGold(uuid);
                } else {
                    Statement statement1 = plugin.connection.createStatement();
                    String set = ("UPDATE `PlayerData` SET `Gold`= '" + finalGold + "' WHERE UUID ='" + uuid.toString()
                            + "';");
                    statement1.execute(set);
                }
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public double getPlayerGold(UUID uuid) {
            double gold = 0.00;
            try {
                Statement statement = plugin.connection.createStatement();
                ResultSet getnumbers = statement
                        .executeQuery("SELECT * FROM PlayerData WHERE UUID ='" + uuid.toString() + "';");
                getnumbers.next();
                gold = getnumbers.getDouble("Gold");
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
            return gold;
        }
    
        public void resetPlayerGold(UUID uuid) {
            try {
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Gold`= '0.00' WHERE UUID ='" + uuid.toString() + "';");
                statement1.execute(set);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        /* Player Prestige */
    
        public void setPlayerPrestige(UUID uuid, int prestige) {
            try {
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Prestige`= '" + prestige + "' WHERE UUID ='" + uuid.toString()
                        + "';");
                statement1.execute(set);
                updateLevel(uuid);
                updateTabListWithUUID(uuid);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public int getPlayerPrestige(UUID uuid) {
            int prestige = 0;
            try {
                Statement statement = plugin.connection.createStatement();
                ResultSet getnumbers = statement
                        .executeQuery("SELECT * FROM PlayerData WHERE UUID ='" + uuid.toString() + "';");
                getnumbers.next();
                prestige = getnumbers.getInt("Prestige");
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
            return prestige;
        }
    
        public void addPlayerPrestige(UUID uuid, int amount) {
            try {
    
                int currentPrestige = getPlayerPrestige(uuid);
                int finalPrestige = currentPrestige + amount;
    
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Prestige`= '" + finalPrestige + "' WHERE UUID ='" + uuid.toString()
                        + "';");
                statement1.execute(set);
                updateLevel(uuid);
                updateTabListWithUUID(uuid);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void removePlayerPrestige(UUID uuid, int amount) {
            try {
    
                int currentPrestige = getPlayerPrestige(uuid);
                int finalPrestige = currentPrestige - amount;
    
                if (finalPrestige <= 0) {
                    resetPlayerPrestige(uuid);
                } else {
                    Statement statement1 = plugin.connection.createStatement();
                    String set = ("UPDATE `PlayerData` SET `Prestige`= '" + finalPrestige + "' WHERE UUID ='"
                            + uuid.toString() + "';");
                    statement1.execute(set);
                    updateLevel(uuid);
                    updateTabListWithUUID(uuid);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void resetPlayerPrestige(UUID uuid) {
            try {
                Statement statement1 = plugin.connection.createStatement();
                String set = ("UPDATE `PlayerData` SET `Prestige`= '0' WHERE UUID ='" + uuid.toString() + "';");
                statement1.execute(set);
                updateLevel(uuid);
                updateTabListWithUUID(uuid);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void updateTabListWithUUID(UUID uuid) {
            try {
                Statement statement5 = plugin.connection.createStatement();
                ResultSet get = statement5.executeQuery("SELECT * FROM PlayerData WHERE UUID ='" + uuid.toString() + "';");
                get.next();
                String name = get.getString("Name");
                Player player = Bukkit.getPlayer(name);
                updateTablistName(player);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public void updateTablistName(Player player) {
            if (player == Bukkit.getPlayer("Lightcaster5")) {
                String prefix = tabEvaluator.tablistPrefix(player.getUniqueId(), getPlayerLevel(player.getUniqueId()));
                player.setPlayerListName(prefix + " §4Li§cgh§6tc§eas§2te§ar5");
            } else {
                String prefix = tabEvaluator.tablistPrefix(player.getUniqueId(), getPlayerLevel(player.getUniqueId()));
                player.setPlayerListName(prefix + " " + tabEvaluator.rankPrefixColor(player) + player.getName());
            }
        }
    
        public void updateLevel(UUID uuid) {
            int newLevel = getLevel(uuid);
            setPlayerLevel(uuid, newLevel);
        }
    
        public String experienceToNextLevelScoreboard(UUID uuid) {
    
            if (getPlayerLevel(uuid) == 120) {
                return "§bMAXED";
            }
    
            return "§b" + experienceToNextLevel(uuid);
        }
    
        public int experienceToNextLevel(UUID uuid) {
            int xp = getPlayerExperience(uuid);
            int prestige = getPlayerPrestige(uuid);
            int level = getPlayerLevel(uuid);
            int nextLevel = level + 1;
            int nextLevelXp = (5 + prestige) * ((nextLevel) * (nextLevel));
            return nextLevelXp - xp;
        }
    
        public int getLevel(UUID uuid) {
            int xp = getPlayerExperience(uuid);
            int prestige = getPlayerPrestige(uuid);
            if (xp == 0) {
                return 1;
            } else {
                int finalLevel = (int) Math.sqrt((xp) / (5 + prestige));
                if (finalLevel == 0) {
                    return 1;
                } else {
                    return finalLevel;
                }
            }
        }
    
        /*
         * public int getPlayer(String name) { int playerLevel = 0; Statement statement;
         * try { statement = plugin.connection.createStatement(); ResultSet getnumbers =
         * statement.executeQuery("SELECT * FROM PlayerData WHERE Name ='" + name +
         * "';"); getnumbers.next();
         *
         * playerLevel = getnumbers.getInt("Level"); } catch (SQLException e) {
         * e.printStackTrace(); } return playerLevel; }
         *
         * public void setPlayer(String name, Integer level) { try { Statement statement
         * = plugin.connection.createStatement(); String set =
         * ("UPDATE `PlayerData` SET `Level`= '" + level + "' WHERE Name ='" + name +
         * "';"); statement.execute(set);
         *
         * } catch (SQLException e) { e.printStackTrace(); } }
         *
         * public void updatePlayerName(String uuid, Player player) { try { Statement
         * statement = plugin.connection.createStatement(); String update =
         * ("UPDATE `PlayerData` SET `Name`= '" + player.getName() + "' WHERE UUID ='" +
         * uuid + "';"); statement.execute(update);
         *
         * } catch (SQLException e) { e.printStackTrace(); } }
         *
         * public void resetAllPlayerLevel() { try { Statement statement =
         * plugin.connection.createStatement(); String reset =
         * ("UPDATE PlayerData SET Level = 0;"); statement.execute(reset); } catch
         * (SQLException e) { e.printStackTrace(); } }
         *
         * public void resetAllPlayerGold() { try { Statement statement =
         * plugin.connection.createStatement(); String reset =
         * ("UPDATE PlayerData SET Gold = 0;"); statement.execute(reset); } catch
         * (SQLException e) { e.printStackTrace(); } }
         */
    
    }
    

    Lastly, the error I keep getting... Let's say I use the method 'checkPlayerWithUUID(uuid), the null error occurs at 'Statement statement = plugin.connection.createStatement();'
    Error (open)

    Code:
    21.02 05:14:05 [Server] INFO java.lang.NullPointerException
    21.02 05:14:05 [Server] INFO at me.lightcaster5.hostilityminigame.SQL.SQLManager.checkPlayerWithUUID(SQLManager.java:43) ~[?:?]
    21.02 05:14:05 [Server] INFO at me.lightcaster5.hostilityminigame.scoreboard.ScoreboardManager.updateScoreBoard(ScoreboardManager.java:150) ~[?:?]
    21.02 05:14:05 [Server] INFO at me.lightcaster5.hostilityminigame.scoreboard.ScoreboardManager$1.run(ScoreboardManager.java:237) ~[?:?]
    21.02 05:14:05 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    21.02 05:14:05 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    21.02 05:14:05 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    21.02 05:14:05 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    21.02 05:14:05 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    21.02 05:14:05 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
     
    Last edited: Feb 21, 2020
  23. Offline

    timtower Moderator Moderator

  24. Offline

    Lightcaster5

    Because apparently nothing works. When I do what you told me to do it doesnt work and neither does this.
    I tried using this before
    Code:
    private Main plugin;
    
    public SQLManager(Main plugin) {
        this.plugin = plugin;
    }
     
  25. Offline

    timtower Moderator Moderator

    Then post the error that you get with this.
    And it might be that plugin.connection is null, you probably want to check that.
     
  26. Offline

    Lightcaster5

    I still got the same error. The problem is in the runnable though because it can create the scoreboard just fine... when updating thats when I get all the null errors.
     
  27. Offline

    timtower Moderator Moderator

    Then find out what is null.
    Add print statements.
    Print if plugin is null.
    Print if plugin.connection is null.
    At least find the line that is causing it.
     
  28. Offline

    Lightcaster5

    IT'S THE PLUGIN!
    Code:
    21.02 05:43:47 [Server] Lightcaster5 ran command Message of the Day
    21.02 05:43:48 [Server] INFO Plugin was null...
    21.02 05:43:48 [Server] INFO Something went very wrong...
    21.02 05:43:48 [Server] INFO Player: Lightcaster5
    21.02 05:43:48 [Server] INFO UUID: 49466bd2-3a23-49ad-9f0d-38d75077beb5
    21.02 05:43:48 [Server] INFO Plugin was null...
    21.02 05:43:48 [Server] INFO Exists in SQL (String): false
    21.02 05:43:49 [Server] INFO Plugin was null...
    21.02 05:43:49 [Server] INFO Something went very wrong...
    21.02 05:43:49 [Server] INFO Player: Lightcaster5
    21.02 05:43:49 [Server] INFO UUID: 49466bd2-3a23-49ad-9f0d-38d75077beb5
    21.02 05:43:49 [Server] INFO Plugin was null...
    21.02 05:43:49 [Server] INFO Exists in SQL (String): false
    21.02 05:43:50 [Server] INFO Plugin was null...
    21.02 05:43:50 [Server] INFO Something went very wrong...
    21.02 05:43:50 [Server] INFO Player: Lightcaster5
    21.02 05:43:50 [Server] INFO UUID: 49466bd2-3a23-49ad-9f0d-38d75077beb5
    21.02 05:43:50 [Server] INFO Plugin was null...
    21.02 05:43:50 [Server] INFO Exists in SQL (String): false
    21.02 05:43:51 [Server] INFO Plugin was null...
    21.02 05:43:51 [Server] INFO Something went very wrong...
    21.02 05:43:51 [Server] INFO Player: Lightcaster5
    21.02 05:43:51 [Server] INFO UUID: 49466bd2-3a23-49ad-9f0d-38d75077beb5
    21.02 05:43:51 [Server] INFO Plugin was null...
    21.02 05:43:51 [Server] INFO Exists in SQL (String): false
    21.02 05:43:51 [Disconnect] User Lightcaster5 has disconnected, reason: Disconnected
    21.02 05:43:51 [Server] INFO Lightcaster5 left the game.
    EDIT: Either way, I must go to the great prison-like building most call school... I'll reply when I can but if you have any ideas on ways to fix it, please don't hesitate. Have a good day Tim!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 21, 2020
  29. Offline

    timtower Moderator Moderator

    @Lightcaster5 That is because you keep initializing variables outside of the onEnable.
    private final ScoreboardManager scoreboardmanager = new ScoreboardManager(this);
    this = invalid at this point.
    Fill variables in the onEnable, not before.
     
  30. Offline

    bowlerguy66

    I see this in your SQL manager class
    Code:
        private final Main plugin = Main.getInstance();
    
        public SQLManager(Main plugin) {
        }
    Instead of doing private final Main plugin = Main.getInstance(); (Which will return nothing useful), set your plugin value in the constructor, like this:
    Code:
        private Main plugin;
    
        public SQLManager(Main plugin) {
           this.plugin = plugin
        }
    
    Then you will get a working instance of your main class. Good luck!
     

Share This Page