Solved Shorten method name?

Discussion in 'Plugin Development' started by KarimAKL, Jul 25, 2018.

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

    KarimAKL

    Okay, i have these lines of code:
    ChatColorUtil class:
    Code:Java
    1. public String here(String s) {
    2. return ChatColor.translateAlternateColorCodes('&', s);
    3. }

    Main class:
    Code:Java
    1. private ChatColorUtil color = new ChatColorUtil();
    2. public ChatColorUtil Color() {
    3. return color;
    4. }

    Command Class:
    Code:Java
    1. private Main plugin;
    2. @Override
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    4. if (cmd.getName().equalsIgnoreCase("test")) {
    5. sender.sendMessage(plugin.Color().here("works"));
    6. return true;
    7. }
    8. }

    But if i do this instead:
    Command Class:
    Code:Java
    1. private Main plugin;
    2. private ChatColorUtil Color = plugin.Color();
    3. @Override
    4. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    5. if (cmd.getName().equalsIgnoreCase("test")) {
    6. sender.sendMessage(Color.here("doesn't work"));
    7. return true;
    8. }
    9. }

    It causes a NullPointerException here:
    Code:Java
    1. private ChatColorUtil Color = plugin.Color();

    EDIT: Nvm, think i know why. I really need sleep. Sorry for the inconvenience. :/
     
    Last edited by a moderator: Jul 25, 2018
  2. Offline

    Reflxction

    plugin is null. You must assign a value to it using a constructor. Also just in future, variables should be named in camelCase.

    Sent from my TRT-L21A using Tapatalk
     
  3. Offline

    KarimAKL

    @Reflxction
    Yeah i figured but my current problem is how i would get the Main class. (trying some abstraction even though i can't really find any tutorials or stuff that works for me :7) Also, where is a variable that isn't camelCase in the code above?
    EDIT: Do you mean the "color" variable? If so then that's because i didn't think i would be able to call it that because of some other stuff named "Color". :p
     
  4. Offline

    Zombie_Striker

    @KarimAKL
    1. It's literately one line, and I am assuming that is the one method in the class. Why not just have the translation where it is needed.
    2. If it is needed for some reason, the method should be static, so you don't need to store the instance of the class anywhere.
    3. The reason why it does not work is because you are getting the color method before the color has been set. You need to make sure the .color() is not null before setting Color.
     
  5. Offline

    KarimAKL

    @Zombie_Striker
    1. I see your point, i should probably just do that. :7
    2. I'll just make it in the class if needed.
    3. I'm pretty sure plugin was null, not color, unless i misunderstood something.
    EDIT: Rewrote the plugin and it now works, thanks for the help. :)
     
    Last edited by a moderator: Jul 25, 2018
Thread Status:
Not open for further replies.

Share This Page