Solved Need help with NPE

Discussion in 'Plugin Development' started by Wizardo367, Mar 5, 2014.

Thread Status:
Not open for further replies.
  1. Hello people, I'm trying to figure out why this problem is occurring and I know it's something simple but i just can't put my finger on it...:eek:

    So an NPE occurs on this line
    Code:java
    1. ArrayList<CachedItem> cachedItems = plugin.databaseFunctions.getItemCache();

    which doesn't make sense to me because a variable can equal null, this happens when accessing two other methods as well by using
    Code:java
    1. plugin.databaseFunctions
    but I haven't had any problems accessing methods and functions this way before.

    Here's the lines from the main class:
    Code:java
    1. DatabaseFunctions databaseFunctions = new DatabaseFunctions(mySQL, this);
    2. CacheFunctions cacheFunctions = new CacheFunctions(this);


    Here's the log:
    Code:
    [23:12:33] [Server thread/INFO]: Starting minecraft server version 1.7.2
    [23:12:33] [Server thread/INFO]: Loading properties
    [23:12:33] [Server thread/INFO]: Default game type: SURVIVAL
    [23:12:33] [Server thread/INFO]: Generating keypair
    [23:12:33] [Server thread/INFO]: Starting Minecraft server on *:25566
    [23:12:33] [Server thread/INFO]: This server is running CraftBukkit version git-Bukkit-1.7.2-R0.3-b3020jnks (MC: 1.7.2) (Implementing API version 1.7.2-R0.3)
    [23:12:33] [Server thread/ERROR]: Could not load 'plugins\SuperMarket.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:137) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:313) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:236) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:350) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.<init>(CraftServer.java:312) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.<init>(PlayerList.java:63) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedPlayerList.<init>(SourceFile:14) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:126) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:424) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at me.wizardo367.SuperMarket.CacheFunctions.<init>(CacheFunctions.java:15) ~[?:?]
        at me.wizardo367.SuperMarket.SuperMarket.<init>(SuperMarket.java:43) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_09]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.7.0_09]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.7.0_09]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.7.0_09]
        at java.lang.Class.newInstance0(Unknown Source) ~[?:1.7.0_09]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.7.0_09]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 9 more
    [23:12:33] [Server thread/INFO]: [Vault] Loading Vault v1.2.25-b320
    [23:12:33] [Server thread/INFO]: [WorldEdit] Loading WorldEdit v5.5.6
    [23:12:33] [Server thread/INFO]: [iConomy] Loading iConomy v7.0
    [23:12:33] [Server thread/INFO]: [Vault] Enabling Vault v1.2.25-b320
    [23:12:33] [Server thread/WARN]: iConomy - If you are using Flatfile storage be aware that versions 6, 7 and 8 have a CRITICAL bug which can wipe ALL iconomy data.
    [23:12:33] [Server thread/WARN]: if you're using Votifier, or any other plugin which handles economy data in a threaded manner your server is at risk!
    [23:12:33] [Server thread/WARN]: it is highly suggested to use SQL with iCo6 or to use an alternative economy plugin!
    [23:12:33] [Server thread/INFO]: [Vault][Economy] iConomy 6 found: Waiting
    [23:12:33] [Server thread/INFO]: [Vault][Permission] SuperPermissions loaded as backup permission system.
    [23:12:33] [Server thread/INFO]: [Vault] Enabled Version 1.2.25-b320
    [23:12:33] [Server thread/INFO]: [Vault][Economy] iConomy 7 hooked.
    [23:12:33] [Server thread/INFO]: Preparing level "world"
    [23:12:33] [Thread-6/INFO]: ----- Bukkit Auto Updater -----
    [23:12:33] [Thread-6/INFO]: It appears that you're running a Beta Build, when you've specified in bukkit.yml that you prefer to run Recommended Builds.
    [23:12:33] [Thread-6/INFO]: If you would like to be kept informed about new Beta Build releases, it is recommended that you change 'preferred-channel' in your bukkit.yml to 'beta'.
    [23:12:33] [Thread-6/INFO]: With that set, you will be told whenever a new version is available for download, so that you can always keep up to date and secure with the latest fixes.
    [23:12:33] [Thread-6/INFO]: If you would like to disable this warning, simply set 'suggest-channels' to false in bukkit.yml.
    [23:12:33] [Thread-6/INFO]: ----- ------------------- -----
    [23:12:33] [Server thread/INFO]: Preparing start region for level 0 (Seed: 3577657912297439964)
    [23:12:34] [Server thread/INFO]: Preparing start region for level 1 (Seed: -2722458578954435528)
    [23:12:35] [Server thread/INFO]: Preparing start region for level 2 (Seed: -2722458578954435528)
    [23:12:35] [Server thread/INFO]: [WorldEdit] Enabling WorldEdit v5.5.6
    [23:12:35] [Server thread/INFO]: WEPIF: Using the Bukkit Permissions API.
    [23:12:35] [Server thread/INFO]: [iConomy] Enabling iConomy v7.0
    [23:12:35] [Server thread/INFO]: [iConomy - April Fools] Enabled (22 ms)
    [23:12:35] [Server thread/INFO]: [iConomy] Hello, I'm Nijikokun. Yes, this is an April Fools joke, but '/money top' was fixed! Enjoy :) - Rare Version!
    [23:12:35] [Thread-7/INFO]: [iConomy - April Fools] Purged accounts with default balance.
    [23:12:35] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [23:12:35] [Server thread/INFO]: Done (1.824s)! For help, type "help" or "?"
    
    Any help is appreciated, :D.
     
  2. Offline

    calebbfmv

    Wizardo367
    Did you define 'plugin' as 'this' in your onEnable()
     
  3. Offline

    Jombi

    calebbfmv Never heard of defining plugin in your onEnable() method...

    Wizardo367 Do you have a constructor in your class that contains those methods?
     
  4. Offline

    calebbfmv

    Jombi
    Like this:

    public static <MainClass> plugin;

    public void onEnable(){
    plugin = this;
    }

    But I see what he means.
     
  5. Yes

    Here's some code from the Main class:
    Code:java
    1. private static SuperMarket plugin;
    2.  
    3. MySQL mySQL = new MySQL("", "", "", "", "");
    4. Connection connection = null;
    5.  
    6. PluginFunctions pluginFunctions = new PluginFunctions(this);
    7. DatabaseFunctions databaseFunctions = new DatabaseFunctions(mySQL, this);
    8. CacheFunctions cacheFunctions = new CacheFunctions(this);
    9. SubCommands subCommands = new SubCommands(this);
    10. ShopGUI shopGUI = new ShopGUI(this);
    11.  
    12. @Override
    13. public void onEnable(){
    14.  
    15. plugin = this;

    Here's some code from the DatabaseFunctions class:
    Code:java
    1. SuperMarket plugin;
    2. MySQL mySQL = new MySQL("", "", "", "", "");
    3. Connection connection = null;
    4.  
    5. public DatabaseFunctions(MySQL mySQLInstance, SuperMarket instance){
    6.  
    7. this.plugin = instance;
    8. try{
    9. mySQL = mySQLInstance;
    10. connection = mySQL.open();
    11. }catch (Exception e){}
    12.  
    13. }

    Here's some code fro mthe CacheFunctions class:
    Code:java
    1. SuperMarket plugin;
    2. ArrayList<CachedItem> cachedItems = plugin.databaseFunctions.getItemCache();
    3. ArrayList<CachedEnchantment> cachedEnchantments = plugin.databaseFunctions.getEnchantmentCache();
    4. ArrayList<CachedSign> cachedSigns = plugin.databaseFunctions.getSignCache();
    5.  
    6. public CacheFunctions(SuperMarket instance) {
    7. this.plugin = instance;
    8. }
     
  6. Offline

    Barinade

    mySQL is null for some reason it seems, try
    log("mySQL is null: " + (mySQL === null ? "yes":"no"));
    above the line where you create databaseFunctions

    If mySQL can be null, add a null check before using it
     
  7. Tried it, said MySQL isn't equal to null.
     
  8. Offline

    Barinade

    Check if null for all variables used on these lines
    CacheFunctions.java:15
    SuperMarket.java:43
     
  9. OK did what you said and line 43 is fine, the problem lines are 15, 16 and 17 in the CacheFunctions class
    Code:java
    1. SuperMarket plugin;
    2. ArrayList<CachedItem> cachedItems = plugin.databaseFunctions.getItemCache();
    3. ArrayList<CachedEnchantment> cachedEnchantments = plugin.databaseFunctions.getEnchantmentCache();
    4. ArrayList<CachedSign> cachedSigns = plugin.databaseFunctions.getSignCache();

    The problem still occurs when I define the variables in the constructor
    Code:java
    1. SuperMarket plugin;
    2. ArrayList<CachedItem> cachedItems;
    3. ArrayList<CachedEnchantment> cachedEnchantments;
    4. ArrayList<CachedSign> cachedSigns;
    5.  
    6. public CacheFunctions(SuperMarket instance) {
    7.  
    8. this.plugin = instance;
    9.  
    10. try {
    11. cachedItems = plugin.databaseFunctions.getItemCache();
    12. cachedEnchantments = plugin.databaseFunctions.getEnchantmentCache();
    13. cachedSigns = plugin.databaseFunctions.getSignCache();
    14. } catch (NullPointerException e) {
    15. plugin.logger.severe("*****NULL*****");
    16. }
    17.  
    18. }


    Did some more debugging, the problem line is in the DatabaseFunctions class when i try to get the data to cache

    Code:java
    1. preparedStatement = connection.prepareStatement("SELECT * FROM tbl_items");


    This is the first time I'm using prepared statements and is the statement above valid or must i use normal statements for queries?

    Got it working! :D, thanks for your help turns out I was trying to query the database before the MySQL settings from the config had been collected and applied. :oops:
     
  10. Offline

    Barinade

    Does "tbl_items" exist in the db?

    Edit: Had page loaded from earlier, didn't know it was solved, my b.
     
Thread Status:
Not open for further replies.

Share This Page