1.14.4 NullPointer even though it works in my other classes?

Discussion in 'Spigot Plugin Development' started by HarleySwtfus, Mar 23, 2020.

  1. I have a class here that is a method that basically gets the string from the config then translates it, then returns the value: https://hastebin.com/avunihadez.java

    When I use this method in class A, using "text.getStringFromConfig("gui-names-main.withdraw")", it works perfectly, but when I use it in class B, "p.sendMessage(GetText.getStringFromConfig("messages.player-not-online"));", it throws a NullPointer?
    Why does it do this? I'm initializing the class both in the same way in both classes, so what is the difference? Also I made sure the path exists in the config.
  2. Can you post the log of the error?
  3. Maybe at this point SettingsManager.instance is null? Or there's no config value for "messages.player-not-online"?
  4. Can you show us the classes you are referencing in your post? The class where it works and the class where it doesn't.
  5. messages.player-not-online is not null
    Works on this class: https://hastebin.com/ivuyayuron.java

    doesnt work here: https://hastebin.com/weheyagelo.java
  6. Choco


  7. How can I avoid using static?

    EDIT: It says the error is on line 40 which is this:
    if (!(argPlayer.isOnline())) {
    #7 HarleySwtfus, Mar 24, 2020
    Last edited: Mar 24, 2020
  8. Choco


    I'd hope you understand what a constructor is. In the three classes you've shown, none of them have constructors. Create an instance of your SettingsManager in your main class and pass necessary values through constructors where necessary. I recommend reading on how to properly structure OOP code to avoid messes like this.
  9. I do understand what constructors are. Can I create an instance like this:

    SettingsManager config = new SettingsManager();
    Then what do you mean by passing information through the constructor?
    #9 HarleySwtfus, Mar 24, 2020
    Last edited: Mar 24, 2020
  10. Choco


    I mean, for instance, in your commands class. You have 2 fields that could easily be arguments in your constructor. Though I'd recommend instead just passing your plugin instance and fetching an instance of any required classes through that.
    Code (Java):
    public class Commands implements CommandExecutor {

        private final YourPlugin plugin;

        public Commands(YourPlugin plugin) {
            this.plugin = plugin;

        public boolean onCommand(CommandSender sender, Command cmd, String arg, String[] args) {

    Your GetText class just seems like an unnecessary wrapper around your SettingsManager so I see no purpose for it to exist at all.
  11. Then the argPlayer is a null object, just check beforehand
  12. Where will define the "getSettingsManager" method be created and how?

    EDIT: Would I do this?
    Code (Java):

        public SettingsManager getSettingsManager() {
            return new SettingsManager();
    #12 HarleySwtfus, Mar 24, 2020
    Last edited: Mar 24, 2020
  13. Instead of using !argPlayer.isOnline(), could I use

    if (argPlayer == null) {
  14. Code (Text):
    package me.harley.Banker;
    public class GetText {
        private SettingsManager config = SettingsManager.instance;
        public static GetText instanceText = new GetText();
        public static GetText getInstance() {
            return instanceText;

        public String getStringFromConfig(String path) {
            return config.getConfig().getString(path);
    public static GetText instanceText = new GetText(); This makes my brain hurt...

    instanceText = this; so you are not creating another copy of the class from within the class

    In your main class, the one that extends JavaPlugin (the class that has all the config stuff in it) needs to be passed to these other classes by constructor (or in a pinch create a getInstance() method) assuming the variable name that holds the main class instance is plugin, then you can do plugin.getConfig().etc
  15. How would I create the getInstance method or pass my main class to my other classes?

    I have done this in my other classes so I can access my Main class:

    Code (Java):

        private final Banker plugin;

        public Events(Banker plugin) {
            this.plugin = plugin;