Referencing a class

Discussion in 'Plugin Development' started by RoboticPlayer, Oct 28, 2015.

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

    RoboticPlayer

    I don't exactly know how to explain this, but I will try my best. I have a class "ConfigValues" which contains and sets the values of strings defined in the config.yml
    Code:
    private SuperAdmin plugin;
    
    public ConfigValues(SuperAdmin plugin) {
        this.plugin = plugin;
    }
    
    private String tma = plugin.getConfig().getString("tma");
    private String noperm = plugin.getConfig().getString("no-permission");
    private String nullTarget = plugin.getConfig().getString("null-target");
    private String consoleSender = plugin.getConfig().getString("console-sender");
    private String adminEnabled = plugin.getConfig().getString("admin-enabled");
    private String adminDisabled = plugin.getConfig().getString("admin-disabled");
    // Getters for the strings
    I have that in my ConfigValues class because I don't want a cluttered main class. However, in my main class ("SuperAdmin") I have no reference to the ConfigValues class (even though ConfigValues references SuperAdmin). I understand that this is a problem (at least I think it is, as errors are thrown on server startup), but I don't know how I would fix this. I probably need to store a reference to the ConfigValues class within my SuperAdmin class, but I don't know how. Help would be appreciated with this!

    Edit: It is an ExceptionInInitializationError
     
  2. Offline

    adam753

    @henderry2019
    All those variables tma, noperm, etc. get set to the values you've given them before the constructor is called. That means "plugin" is null at that point, so plugin.getConfig() is giving you this error. This is generally why people don't like setting member variables inline like that; you can solve this by setting those values in the constructor instead.
     
  3. Offline

    RoboticPlayer

    @adam753 If I set them inside the constructor, they do not allow privacy modifiers, and I also won't be able to use any getter methods, making the class completely useless.
     
  4. Offline

    adam753

    @henderry2019
    I'm sorry? Private members can be set (exclusively) by the class's methods. Are you thinking of "final"?
    Also it wouldn't affect your getters.
     
  5. Offline

    RoboticPlayer

    @adam753 When I put one of my lines defining a string from the config in the constructor, my IDE gives me the error: Illegal modifier for parameter tma; only final is permitted -End error-
    Also, it does affect my getters. The String fields become local variables, so I cannot get them outside of the method.
     
  6. Offline

    adam753

    @henderry2019
    Ah, I mean keep them as members but set their values in the constructor.
    Code:java
    1. class MyThing {
    2. public MyThing() {
    3. this.myMember = 7;
    4. }
    5.  
    6. private int myMember;
    7.  
    8. public int getMember() {
    9. return this.myMember;
    10. }
    11. }
     
    Last edited: Oct 28, 2015
  7. Offline

    RoboticPlayer

    @adam753 But won't that not actually do anything because the myMember local variable is never actually used? It's defined, but then you have a field called myMember so when you use the getMember method, it will return a null int, wouldn't it?
     
  8. Offline

    adam753

    @henderry2019
    There is no local variable. I'm setting the member from in the constructor. I've edited the example to make that clearer, but the "this." isn't actually needed unless there's a name conflict.
     
  9. Offline

    RoboticPlayer

    @adam753 Ok, thanks. This now works. Now, do I actually need the full constructor? If you don't know what I mean, look at how I made my constructor in the original post. Do I still need the line:
    Code:
    private SuperAdmin plugin;
    As my IDE tells me it is unused (which it technically is).

    Additionally (but somewhat unrelated), I am getting an NPE when sending a command via console (I have it sending a message to the console from the config when the console sends a command.).
    Code:
    if (!(sender instanceof Player)) {
        sender.sendMessage(cv.getConsoleSender());
        return true;
    }
    "cv" is defined as my ConfigValues class. Here is the getConsoleSender method:
    Code:
    public String getConsoleSender() {
        consoleSender = ChatColor.translateAlternateColorCodes('&', consoleSender);
        return consoleSender;
    }
    Here is the log:
    Code:
    [21:22:07] [Server thread/WARN]: Unexpected exception while parsing console command "toggleadmin"
    org.bukkit.command.CommandException: Unhandled exception executing command 'toggleadmin' in plugin SuperAdmin v1.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:606) [craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:372) [craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:336) [craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.NullPointerException
        at org.bukkit.ChatColor.translateAlternateColorCodes(ChatColor.java:210) ~[craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        at me.roboticplayer.superadmin.methods.ConfigValues.getConsoleSender(ConfigValues.java:44) ~[?:?]
        at me.roboticplayer.superadmin.commands.AdminToggle.onCommand(AdminToggle.java:20) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.8.jar:git-Bukkit-18fbb24]
        ... 8 more
    
    Line 44 of ConfigValues:
    Code:
    consoleSender = ChatColor.translateAlternateColorCodes('&', consoleSender);
    Line 20 of AdminToggle just calls the getConsoleSender method. Any help with this (I haven't used ChatColor#translateAlternateColorCodes before, so I might be using it incorrectly).
     
  10. Offline

    adam753

    @henderry2019
    Well, if you're going to have the plugin member, then you would need to set it in the constructor, yes. You can either set it in the constructor as you were before, or not have the field at all (I don't know if you need it.)

    The NPE is caused by cv.consoleSender being null.
     
  11. Offline

    RoboticPlayer

    @adam753 But why would consoleSender be null?
    Config.yml:
    Code:
    # Set this to the message that is sent when the console runs a commamnd
    console-sender: &4Only players can do this!
    consoleSender in ConfigValues.java:
    Code:
    private SuperAdmin plugin;
    
    public ConfigValues(SuperAdmin plugin) {
        consoleSender = plugin.getConfig().getString("console-sender");
        this.plugin = plugin;
    }
    
    private String consoleSender;
    
    public String getConsoleSender() {
        consoleSender = ChatColor.translateAlternateColorCodes('&', consoleSender);
        return consoleSender;
    }
    consoleSender in AdminToggle.java:
    Code:
    if (!(sender instanceof Player)){
        sender.sendMessage(cv.getConsoleSender());
        return true;
    }
     
  12. Offline

    adam753

    @henderry2019
    A few potential reasons. Are you sure it's finding your config? Check the console output when the server loads up, I think it says something in there. The filename is probably case-sensitive too.

    On a partly related note, you could just do this:
    Code:java
    1. public String getConsoleSender() {
    2. return plugin.getConfig().getString("console-sender");
    3. }

    The config file only gets loaded once, so no performance worries.
     
Thread Status:
Not open for further replies.

Share This Page