Solved NPE with constructor(?)

Discussion in 'Plugin Development' started by HeadGam3z, Nov 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    HeadGam3z

    I'm getting a NPE on this line
    PHP:
    response economy.depositPlayer(playercost);
    Which belongs to this method
    PHP:
        private boolean giveMoneySuccessful() {
            
    // Debug message start
            
    System.out.println(economy);
            
    System.out.println(player);
            
    System.out.println(cost);
            
    // Debug message end
            
    response economy.depositPlayer(playercost);
            if (
    response.transactionSuccess()) {
                return 
    true;
            } else {
                return 
    false;
            }
        }
    Which belongs to this class
    PHP:
    EconomyResponse response;
        
    Player player;
        
    Main plugin;
        
    Economy economy;
        
    int cost;
     
        public 
    CommandTest(Main instance) {
            
    plugin instance;
            
    economy plugin.econ;
            
    cost plugin.cost;
        }
     
        public 
    boolean onCommand(CommandSender senderCommand cmdString sString[] args) {
            if (
    args.length == 0) {
                if (
    sender instanceof Player) {
                    
    player = (Playersender;
                    if (
    giveMoneySuccessful()) {
                        
    player.sendMessage(cost " added.");
                        return 
    true;
                    } else {
                        
    player.sendMessage("fail whale lol");
                        return 
    false;
                    }
                }
            } else if (
    args.length == 1) {
                
    // change self
            
    } else if (args.length == 2) {
                
    // change player
            
    } else {
                
    // no idea what they typed.
            
    }
            return 
    false;
        }
    When I printed out "economy", it returned as null - yay, 1/2 of the problem solved. My thing is, I don't know why it's null. Here's the main class, where economy is suppose to be getting it's value.
    PHP:
    public class Main extends JavaPlugin {
     
        public 
    Economy econ// Variable for Economy.
        
    public int cost// Price to do stuff.
     
        /*
        * Constructor.
        */
     
        
    public Main() {
            
    cost 420// Setting cost's value.
        
    }
     
        
    /*
        * Public methods.
        */
     
        
    public void onEnable() {
            
    setupCommands(); // Cleaner way to setup the commands.
            
    setupVault(); // Cleaner way to setup Vault.
        
    }
     
        public 
    void onDisable() {
            
    // to-do
        
    }
     
        
    /*
        * Private methods.
        */
     
        
    private boolean setupVault() {
            if (
    getServer().getPluginManager().getPlugin("Vault") == null) {
                
    getLogger().info("Vault was not found! Players can not be charged in-game money when using -snip-.");
                return 
    false;
            }
            
    RegisteredServiceProvider<Economyrsp getServer().getServicesManager().getRegistration(Economy.class);
            if (
    rsp == null) {
                return 
    false;
            }
            
    econ rsp.getProvider();
            
    getLogger().info("Hooked into Vault!");
            return 
    econ != null;
        }
     
        private 
    void setupCommands() {
            
    addCommand("commandtest", new CommandTest(this));
        }
     
        private 
    void addCommand(String commandCommandExecutor executor) {
            
    getCommand(command).setExecutor(executor);
        }
     
    }
    Shouldn't econ return as, well, not null if Vault exists? Any help would be appreciated, thanks!
     
  2. Offline

    sgtcaze

    You are initializing "Economy" in your commands class before your plugin "knows" about it. So your economy variable is null to begin with. Just move setupVault(); above setupCommands();.
     
Thread Status:
Not open for further replies.

Share This Page