HELP | please help me resolve this problem!

Discussion in 'Spigot Plugin Development' started by eyalfx, May 19, 2016.

  1. Me and my friend are trying to make a motd command but it doesnt works,
    can any 1 help?

    Code:
    Code (Text):
    String update = plugin.getConfig().getString("update");
     
    Error:
    java.lang.NullPointerException
    at me.bukkit.<init>(Test.java:19) ~[?:?]
    Line 19: String update = plugin.getConfig().getString("update");

    Config file:
    update: test

    thanks in advance! :)
     
  2. Can you show all your main class and any others?
     
  3. sure
    Code (Text):
    public class Test implements Listener {

    String update = plugin.getConfig().getString("update");

    @EventHandler
    @setMotd(ServerListPingEvent e) {
    e.setMotd(update);
     
  4. That's not your main class, I mean the class extending javaplugin
     
  5. public class main extends JavaPlugin {

    @Override
    public void onEnable(){
    Bukkit.getServer().getPluginManager().registerEvents(Test(this), this);
    System.out.println("ENABLED");
     
  6. Have you defined "plugin"
     
  7. yes.
    I forgot to add it

    Code (Text):
    Yes, I did define "plugin" I forgot to add it
    public class Test implements Listener {

    public main plugin;

    public Test(main instance){
    plugin = instance;
    }

    String update = plugin.getConfig().getString("update");

    @EventHandler
    @setMotd(ServerListPingEvent e) {
    e.setMotd(update);
    full error:
    http://pastebin.com/6U6597eh
     
  8. Don't bump within 24 hours!
     
    • Agree Agree x 1
  9. You have not save the configuration...

    Add this to your onEnable
    Code (Text):
    saveDefaultConfig();
    And initialize "update" in the event, when if you change your motd it automatically change the motd on ping. ;)
     
  10. your "plugin" is intialized when the Constructor of the Class is called,
    however the update String is intialzied before, so when plugin wasn't intialzied yet

    Try that:

    Code (Text):
    public main plugin;

    public Test(main instance){
    plugin = instance;
    update = plugin.getConfig().getString("update");
    }

    String update;
     
  11. Use boolean no string for this
    if (getConfig.."Update" == true){
    //code
    } else { return }
     
  12. You love boolean! xD
     
  13. Or use:
    e.setMotd("getConfiStringHere");
     
  14. he wants to be able to set the server motd to whats in the config, not to be able to tell wheter it says Update in config or not, if he wanted to do so, your code would still be incorrect though, you would need to do
    Code (Text):
    if(getConfig.getBoolean("Update") == true)
    if he wanted to do so
     
  15. Ahh update is string sorry me error, i no read "e,setMotd(update);" :p
     
  16. I
    I use prefix..is ez complet this.
     
  17. Code (Text):
    try {
    e.setMotd(plugin.getConfig().getString("update"));
    } catch (NullPointerException ex){
    System.out.println("Error");
    }
     
    • Optimistic Optimistic x 1
  18. No recommended use "Class Objects" for describe a simple string..
     
  19. Thanks! I will try later :)