Solved NoSuchMethodError

Discussion in 'Plugin Development' started by Paul122, Mar 15, 2021.

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

    Paul122

    Hello,

    I've created a class with that reads the config file and returns values for specific strings in the config. Here's what that looks like:

    Code:
        public String getHost() {
            return PlayTime.plugin.getConfig().getConfigurationSection("Database").getString("host");
        }
        public String getPort() {
            return PlayTime.plugin.getConfig().getConfigurationSection("Database").getInt("port") + "";
        }
        public String getName() {
            return PlayTime.plugin.getConfig().getConfigurationSection("Database").getString("database-name");
        }
        public String getUsername() {
            return PlayTime.plugin.getConfig().getConfigurationSection("Database").getString("username");
        }
        public String getPassword() {
            return PlayTime.plugin.getConfig().getConfigurationSection("Database").getString("password");
        }
    
    }
    
    Despite this, I'm getting a NoSuchMethodError when I'm trying to access them. I've tried accessing the config both with inheritance and statically (as seen above), but in both cases I get that error when I try to access these methods. Here is the class that is accessing my config class:

    Code:
    public class MySQL {
    
        Config config = new Config();
    
        private final String HOST = config.getHost();
        private final String PORT = config.getPort();
        private final String DATABASE = config.getName();
        private final String USERNAME = config.getUsername();
        private final String PASSWORD = config.getPassword();
    
         // OTHER METHODS BELOW
    }

    I'm still relatively new to Java, so there's a good chance that I'm misunderstanding how this would work, though I've been able to successfully use this method in some of my other plugins, which is why I'm confused as to why this is giving me an error.

    Any insight is greatly appreciated,
    Thanks

    EDIT: It seems that accessing these config values from any class other than the main is causing them to be null. I can't imagine why this would be the case.
     
    Last edited: Mar 15, 2021
  2. Offline

    davidclue

    I don't really know that much about what methods your using here but if you want a string from the config you just use
    Code:
    plugin.getConfig().getString("string.substring");
    As plugin is an instance of your plugin don't make a static reference to your JavaPlugin main class that will interfere with other plugins so just pass the instance to other classes in your onEnable method.
     
  3. Offline

    KarimAKL

    @Paul122 What method is nonexistent?

    Are the values null, or are you getting a NullPointerException?
     
  4. Offline

    Paul122

    When I was getting the NoSuchMethodError, it was saying that the Config.getHost() method is nonexistent. Now that I have changed my code to what @davidclue recommended, I'm getting a NullPointerException, though the values in the config are not null. Here's my config:

    Code:
    Database:
      host: 'localhost'
      port: '3306'
      db-name: 'playtime'
      username: 'root'
      password: ''
    I've made sure to run saveDefaultConfig() and this.getConfig().options.copyDefaults(true) in my main class, but when accessing them in this class:

    Code:
    public class MySQL {
    
        private PlayTime plugin;
    
        public MySQL(PlayTime plugin) {
            this.plugin = plugin;
        }
    
        private String HOST = plugin.getConfig().getString("Database.host");
        private String PORT = plugin.getConfig().getString("Database.port");
        private String DATABASE = plugin.getConfig().getString("Database.db-name");
        private String USERNAME = plugin.getConfig().getString("Database.username");
        private String PASSWORD = plugin.getConfig().getString("Database.password");
    
        private Connection connection;
    
    
    
    }
    
    I am getting a nullpointerexception for all five values that get data from the config.
     
  5. Offline

    Kars

    You can't access plugin on class-level before instantiation like that. At the time when plugin.getConfig is called for all the capitalized variables, the constructor was not yet called and plugin is null.

    Set the capitalized variables in the constructor.
     
    davidclue likes this.
Thread Status:
Not open for further replies.

Share This Page