Help with a NullPointer

Discussion in 'Plugin Development' started by boysnnoco, Dec 15, 2013.

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

    boysnnoco

    While trying to create a plugin, I ran into a problem with my runnable class. When ever the runnable goes it says there is a nullpointer to my 26th line which is:
    Code:java
    1. Bukkit.broadcastMessage("" + plugin.getConfig().get("DrugDealer.PayAmount"));

    But I know it isn't null as I am generating the config when it is enabled.
    Any Ideas?
     
  2. Offline

    Webster56

    Maybe it's a lower/higher case problem, and then, you don't need to put an empty string before your plugin.getConfig()...
     
  3. Offline

    boysnnoco

    Webster56 herp derp it's supposed to be an int so ignore the
    Code:java
    1. plugin.getConfig().get("DrugDealer.PayAmount");

    it is supposed to be
    Code:java
    1. plugin.getConfig().getInt("DrugDealer.PayAmount");

    My bad (which still doesn't work)
     
  4. Offline

    AoH_Ruthless

    boysnnoco
    Show your config. The nullpointer is most likely referring to something null in the config.
     
  5. Offline

    boysnnoco

    AoH_Ruthless
    This is my config:
    Code:
    Money:
      Start: 50
    Gangsta:
      PayTime: 10
      PayAmount: 50
    DrugDealer:
      PayTime: 10
      PayAmount: 50
    Citizen:
      PayTime: 10
      PayAmount: 50
    Hobo:
      PayTime: 10
      PayAmount: 50
    
    and this is the code generating the config:
    Code:java
    1. final Configuration c = this.getConfig();
    2.  
    3. c.addDefault("Money.Start", 50);
    4. /**
    5.   * TODO: Add Support for differnt types of units (months, years,seconds,days,weeks, ect)
    6.   */
    7. c.addDefault("Gangsta.PayTime", 10);
    8. c.addDefault("Gangsta.PayAmount", 50);
    9. c.addDefault("DrugDealer.PayTime", 10);
    10. c.addDefault("DrugDealer.PayAmount", 50);
    11. c.addDefault("Citizen.PayTime", 10);
    12. c.addDefault("Citizen.PayAmount", 50);
    13. c.addDefault("Hobo.PayTime", 10);
    14. c.addDefault("Hobo.PayAmount", 50);
    15. c.options().copyDefaults(true);
    16. saveConfig();
     
  6. Offline

    AoH_Ruthless

    boysnnoco
    Hmm, I don't see anything wrong with the config.
    Can you post the runnable class?
     
  7. Offline

    boysnnoco

    AoH_Ruthless Sure here is my runnable class:
    Code:
    package me.boysnnoco.Jobs;
     
    import java.io.File;
     
    import me.boysnnoco.DarkRP.Main;
     
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
     
    public class DrugDealer implements Runnable {
     
        public Main plugin;
        public DrugDealer(Main instance) {
            plugin = instance;
        }
        public void run() {
            for(Player p : Bukkit.getOnlinePlayers()){
                FileConfiguration config = null;
                File file = new File("plugins"+File.separator +"DarkRP"+File.separator+"users" +File.separator+p.getName());
                if(file.exists()){
                    config = YamlConfiguration.loadConfiguration(file);
                    if(config.getString("Player.CurrentClass").equalsIgnoreCase("citizen")){
                     
                        Bukkit.broadcastMessage(""  + plugin.getConfig().getInt("DrugDealer.PayAmount"));
                        //plugin.econ.Add(p, plugin.getConfig().getInt("DrugDealer.PayAmount"));
                    }
                }else{
                    p.sendMessage("An error has occured while trying to pay you money for 'Drug Dealer' please report this to any online staff!");
                }
            }
     
        }
     
    }
    
     
  8. Offline

    AoH_Ruthless

    boysnnoco
    Try this:
    Code:java
    1. private Main plugin;

    instead of:
    Code:java
    1. public Main plugin;
     
  9. Offline

    boysnnoco

    AoH_Ruthless That doesn't seem to work, it is still generating a nullpointererror towards line 26.
     
  10. Offline

    iiHeroo



    Try doing a getString() ?

    I don't see why you're getting an Int for a Broadcast unless you're trying to do an auto-broadcast, and if you are, do something like this:

    Code:java
    1. public void run() {
    2. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + settings.getConfig().getString("DrugDealer.PayAmount"));
    3. }
    4. }, 60, 100);
     
  11. Offline

    boysnnoco

    iiHeroo Im getting an int to pay a person a certain amount of money using my economy system
     
  12. Offline

    iiHeroo

  13. Offline

    boysnnoco

  14. Offline

    iiHeroo


    So what do you want to broadcast? Like, what would the broadcast look like?
    For ex:

    Bukkit.broadcastMessage("hi, hi");
     
  15. Offline

    boysnnoco

    iiHeroo the broadcasting part of the plugin was just to track down what was null (which was the plugin.getConfig() part), what im trying to do is use my adding method to add money after a certain amount of time (as you can see commented out). btw, the broadcast should look like 50 (as that is the amount I specify in the configs).
     
  16. Offline

    iiHeroo



    If you want the broadcast to say 50, you need to get a string or simply put "50"
     
  17. Offline

    boysnnoco

    iiHeroo forget the whole broadcast thing, the point is that I put it there to get the problem of the nullpointer. the nullpointer is coming from plugin.getConfig().getInt("DrugDealer.PayAmount"); and I need that pay amount in order to pay a player using the job a certain amount of money.

    bump
    Need this fixed!

    Ok after messing around a bit I seemed to get something that doesn't throw any errors but, it is being counted as 0.
    This is what I have so far:
    Code:java
    1. package me.boysnnoco.Jobs;
    2.  
    3. import java.io.File;
    4.  
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class DrugDealer implements Runnable {
    12. @Override
    13. public void run() {
    14. for(Player p : Bukkit.getOnlinePlayers()){
    15. FileConfiguration config = null;
    16. File file = new File("plugins"+File.separator +"DarkRP"+File.separator+"users" +File.separator+p.getName());
    17. File f = new File("plugins" + File.separator + "DarkRP" + File.separator + "config");
    18. FileConfiguration c = null;
    19. if(file.exists()){
    20. config = YamlConfiguration.loadConfiguration(file);
    21. c = YamlConfiguration.loadConfiguration(f);
    22. if(config.getString("Player.CurrentClass").toLowerCase().equalsIgnoreCase("citizen")){
    23. Bukkit.broadcastMessage("" + c.getInt("DrugDealer.PayAmount"));
    24. }
    25. }else{
    26. p.sendMessage("An error has occured while trying to pay you money for 'Drug Dealer' please report this to any online staff!");
    27. }
    28. }
    29.  
    30. }
    31.  
    32. }
    33.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page