Reading String from config issues

Discussion in 'Plugin Development' started by OptimalBread, May 4, 2014.

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

    OptimalBread

    Title says most of what I am asking. Here is my test.java:
    Code:
    package me.Caam.Test;
     
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class EM implements CommandExecutor{
        Test1 plugin;
        public Test(Test1 test1) {test1= plugin;}
     
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args){
            Player p = (Player) sender;
            if(commandLabel.equalsIgnoreCase("test")){
                p.sendMessage(plugin.getConfig().getString("Test.Test1.test2"));
            }
            return false;
        }
    }
    And my config.yml:
    Code:java
    1. #Test Default Config.yml
    2. Test:
    3. Test1:
    4. test2:'HI!'

    I get an error in console when I type the command /test on the line p.sendMessage. Can someone help me
     
  2. Offline

    jthort

    OptimalBread What's the error? Are you passing the main class through the constructor correctly?

    It looks like you are grabbing it from the config correctly..
     
  3. Offline

    OptimalBread

    jthort
    Code:java
    1. package me.Caam.Test1;
    2.  
    3. import org.bukkit.configuration.file.FileConfiguration;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Emoticons extends JavaPlugin implements Listener{
    8. public void onEnable(){
    9. final FileConfiguration config = this.getConfig();
    10. config.options().copyDefaults(true);
    11. saveConfig();
    12. getLogger().info("Test has been enabled!");
    13.  
    14. getCommand("test").setExecutor(new Test(this));
    15.  
    16.  
    17. }
    18.  
    19. public void onDisable(){
    20. getLogger().info("Test has been disabled");
    21. }
    22. }
    23.  

    My error is cause by: java.lang.NullPointerException
    at me.Caam.Test1.Test.onCommand(Test.java:17) ~[?;?]
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    From the code you have included here, this plugin should not even compile.
     
  5. Offline

    OptimalBread

    Got it stupid mistake
     
  6. Offline

    Drkmaster83

    Was it this?
    Code:
    Test1 plugin;
        public Test(Test1 test1) {test1= plugin;}
    
     
  7. Offline

    OptimalBread

  8. Offline

    kakaruso

    Code:java
    1. package me.Caam.Test;
    2.  
    3.  
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class EM implements CommandExecutor{
    10. private final Main plugin;// where Main is the name of you main class
    11. public EM (Main plugin) {
    12. this.plugin = plugin;
    13. }
    14.  
    15. public boolean onCommand(CommandSender sender, Command cmd,
    16. String commandLabel, String[] args){
    17. Player p = (Player) sender;
    18. if(cmd.getName().equalsIgnoreCase("test")){
    19. p.sendMessage(plugin.getConfig().getString("Test.Test1.test2"));
    20. return true; // if u have more thing in this class you need to add a value return or it's possible u get messages from another if...
    21. }
    22. return false;
    23. }
    24. }


    and then in your onEnable in your Main class:

    Code:java
    1. getCommand("test").setExecutor(new EM (this));
     
  9. Offline

    Drkmaster83

    I had thought so. xD, just needed to make sure.
     
Thread Status:
Not open for further replies.

Share This Page