I don't understand why this is returning null

Discussion in 'Plugin Development' started by Xenoyia, Jun 30, 2015.

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

    Xenoyia

    Hi, I'd just like to start this thread off with that I am an experienced java developer with relevant real-world qualifications, but I am still learning how to code for Minecraft - so please no sarcastic 'learn java' posts like a lot of you seem to reply with.. It doesn't help for people googling why it's not working. I have looked at the bukkit docs and other threads with the same issue but none are working for me. I'm also not posting the real code as it's 3000+ lines over several classes and a private, custom plugin that I don't want shared. That includes PMs.

    The issue:

    I have several classes - let's say Main, ClassA, ClassB and ClassC. Main has the config code so they all have instances of Main to use getConfig(). This is fully initialised in all of the classes as 'plugin', so I can use plugin.getConfig() in all of them. I already checked and thoroughly tested this (for example, making a method in ClassA that runs a System.out.println with a config value, all three of the classes work with this) and this is not the cause of the NPE.

    What I'm trying to do is see if a section exists, returning true if it is and false if it isn't. However, no matter what, even if the section exists, it's returning null.
    Code:
    //This gives a NullPointerException
    if(plugin.getConfig().get("sec1.sec2."+sec3) != null)
    
    //This gives a NullPointerException
    if(plugin.getConfig().getString("sec1.sec2."+sec3) != null)
    
    //This gives a NullPointerException
    if(plugin.getConfig().contains("sec1.sec2."+sec3))
    
    I'm not asking for the correct way to do this, I'm asking why it's returning null so I can figure it out on my own and will update the post when I've got it working so people searching online can actually find out how to fix this issue. It might be something stupid as I've been coding this plugin for weeks now and I wouldn't be surprised if it's something ridiculously easy that I've just forgotten by doing it for so long.

    Any help would be brilliant,
    Many thanks.
     
    Last edited: Jun 30, 2015
  2. Online

    timtower Administrator Administrator Moderator

    @Xenoyia What do you mean with returning null? The third one should only return true or false.
     
  3. Offline

    Xenoyia

    Oops, I just copied and pasted the comment haha. It's giving me a NullPointerException:

    "Caused by: java.lang.NullPointerException
    at <myPlugin>.classA(ClassA.java:53)"

    ClassA, line 53 is: if(plugin.getConfig().contains("sec1.sec2"+sec3))"
     
  4. Offline

    Ruptur

    @Xenoyia
    In your main class extending JavaPlugin you need to copy over the config file with #saveDefaultConfig() or any other custom method
    Code:
    public void onEnable() {
        saveDefaultConfig();
    }
    
     
  5. Offline

    Xenoyia

    That's one of the first things I do.

    [​IMG]
     
  6. Online

    timtower Administrator Administrator Moderator

    @Xenoyia Could you show your constructors?
     
  7. Offline

    Xenoyia

    Certainly. I'm very tired and ill atm, so they could be wrong.
    Code:
    //For all three non-main classes
    public class Class {
    private Main plugin;
    
        public Class(Main plugin) {
            this.plugin = plugin;
        }
    (P.S. Is 'Tahg User' intentional? Had a double-take at it just now haha)
     
  8. Offline

    SuperOriginal

    Most likely that 'plugin' is null.
     
  9. Online

    timtower Administrator Administrator Moderator

    Yes it is, tribute to an old staff member.
    And how are you calling them?
     
  10. Offline

    Xenoyia

    In the Main class, I have:
    Code:
    public final class Main extends JavaPlugin implements Listener{
    ClassA classa = new ClassA(this);
    ClassB classb = new ClassB(this);
    ClassC classc = new ClassC(this);
    I figured that was a bad way to do it, but plugin.getConfig() worked for some things (like returning the value of a node), and the .contains() even works fine in the Main class. That might be the issue now I'm looking at it slightly less tired..
     
  11. Online

    timtower Administrator Administrator Moderator

    @Xenoyia Try to create those in the onEnable instead.
     
  12. Offline

    Xenoyia

    .contains is still throwing a NullPointerException at the same location with that. I did this:
    Code:
    public final class Main extends JavaPlugin implements Listener{
    ClassA classa;
    ClassB classb;
    ClassC classc;
    
    @Override
    public void onEnable() {
    classa = new ClassA(this);
    classb = new ClassB(this);
    classc = new ClassC(this);
    If I declare & initialise the whole thing in onEnable, I can't call methods from those classes in Main as they're not declared. That's probably why it was a bad idea to design it like this..
     
  13. Online

    timtower Administrator Administrator Moderator

    @Xenoyia Most people on here do it like this.
    Could you post the relevant functions of one of the 3 classes?
     
  14. Offline

    Xenoyia

    Certainly, this is the section in ClassA that's throwing the NPE:
    Code:
    public class ClassA {
    private Main plugin;
    
    public ClassA(Main plugin) {
            this.plugin = plugin; // Store the plugin in situations where you need it.
        }
    
    public boolean sectionExists(String section){
            if(plugin.getConfig().contains("sec1.sec2."+section)){
                String values = plugin.getConfig().getString("sec1.sec2."+section);
                if(!values.equals(null) || !values.equals("")){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    }
    Code:
    public final class Main extends JavaPlugin implements Listener{
    ClassA classa;
    
    @Override
    public void onEnable() {
    classa = new ClassA(this);
    }
     
    Last edited: Jun 30, 2015
  15. Online

    timtower Administrator Administrator Moderator

  16. Offline

    Xenoyia

    Added it to the code.
     
  17. Online

    timtower Administrator Administrator Moderator

    @Xenoyia Tried checking which value is null already?
     
  18. Offline

    Xenoyia

    Well, I think it might be the constructor now as for some reason, something I've changed has stopped the config from working at ALL in ClassA.

    I made a config section called "this-exists: 'I am here!'" and attempted to retrieve it before the NPE section ran.

    Config:
    Code:
    this-exists: 'I am here!'
    System.out.println(plugin.getConfig().getString("this-exists"));
    System.out.println(plugin.getConfig().contains("this-exists"));

    That gave a NullPointerException, even though it worked in the beginning. This is strange.
     
  19. Online

    timtower Administrator Administrator Moderator

    @Xenoyia Println the values of everything, I am pretty sure that plugin is somehow null
     
  20. Offline

    Xenoyia

    ...yep, plugin is returning null. That's odd - I've done it this way in other plugins that have worked fine..

    I can't find any reason why it's null here. It's really annoying.

    @timtower is it possible that this is the cause of it? ClassB is the one calling the method from ClassA, ClassB is a command executor.

    WHOOPS. Thought that'd merge the posts, sorry! I'm so used to forums where posting twice merges the posts..

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jun 30, 2015
  21. Online

    timtower Administrator Administrator Moderator

    We are still working on that mechanic.
    Only when you call it before all objects are made
     
  22. Offline

    Xenoyia

    Hmm, I see then. I might just have to scrap this whole part of the code as I can't find any reason for plugin to be null. Not fun as I've been working on it for several hours.

    Thanks for the help anyway, I'll mess around some more but I doubt I can get it working.

    Some extra notes:

    ClassB, that calls the method from ClassA, creates instances of ClassA and ClassC, so it can load methods from them. It can run all getConfig() methods from the Main class fine, without NPEs and the only difference in the constructor is that the plugin is defined as "private final main plugin", and in ClassA/ClassC, it's defined as "private main plugin". Adding final to that breaks other constructors with the error "The blank final field plugin may not have been initialized"
     
Thread Status:
Not open for further replies.

Share This Page