+ Reply to Thread
Results 1 to 6 of 6

Thread: null pointer exception on getApi() calll

  1. #1
    Senior Member
    Join Date
    May 2011
    Posts
    146
    Thanks
    2
    Thanked 0 Times in 0 Posts

    null pointer exception on getApi() calll

    I get a null pointer exception on this line - getApi().sendPluginMessageToUser(user, messageOut) in the function below -

    Code:
    public void sendAndLog(String user, EsObject messageOut) { 
       getApi().sendPluginMessageToUser(user, messageOut);
    }
    The class which has sendAndLog method extends BasePlugin. The plugin is a part of my extension and I can see the plugin the list of plugins in my extension in EsAdmin. How should I debug the issue?

    Thanks!

  2. #2
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,213
    Thanks
    80
    Thanked 1,086 Times in 1,075 Posts
    Add the following lines to the method so we can tell which object is null:
    Code:
    getApi().getLogger().debug("getApi works!");
    getApi().getLogger().debug("user is {}", user);
    getApi().getLogger().debug("messageOut is{}", messageOut.toString());
    Please note that if you have a plugin that creates an instance of this class, it's not going to act as a plugin and won't be able to getApi(). The only classes that can use getApi() are those that are either server level components or are attached to a room (and so are room level plugins). If this is the problem, you just need to pass the other plugin's api and store it. Let me know if you need help with that.
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

  3. #3
    Senior Member
    Join Date
    May 2011
    Posts
    146
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This is the first line inside my sendAndLog method now - getApi().getLogger().debug("getApi works!"); and I get a null pointer exception on this line.


    Here's how my system is setup - Plugin A has sendAndLog method and all the other plugin's, let's say Plugin B creates an instance of PluginA and calls sendAndLog on that instance.

  4. #4
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,213
    Thanks
    80
    Thanked 1,086 Times in 1,075 Posts
    You can't CREATE an instance of a plugin. Plugins are either server level plugins or they are attached to a room when the room is created. If you need to use getApi() methods from some other class, what you do is just have the other class be a plain Java object class, and you have your plugin either pass "this" or "getApi()" either in the constructor or using a setter.

    When a plugin is instanciated, all kinds of things are done by the core server to make it work properly, including injecting the api object, which is why your getApi() is giving you null.
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

  5. #5
    Senior Member
    Join Date
    May 2011
    Posts
    146
    Thanks
    2
    Thanked 0 Times in 0 Posts
    So, if I understand this correctly, i should move sendAndLog method from Plugin A to some other regular java class. All the plugin's would now call newClass.sendAndLog (getApi(), String user, EsObject messageOut) ? Basically, PluginApi should be an argument to sendAndLog in the new class? Does that look right?
    Last edited by vai; 07-20-2012 at 03:17 PM.

  6. #6
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,213
    Thanks
    80
    Thanked 1,086 Times in 1,075 Posts
    When you talk about "all the plugins" are you talking about real plugins, or about objects that you created using new MyPluginName()?

    What I usually do with something useful such as sendAndLog is have an abstract class that extends BasePlugin and contains all the useful methods that all my game plugins will need, but then each of my game plugins extends THAT class. For example:
    Code:
    public abstract class CommonPluginMethods extends BasePlugin {
        public void sendAndLog(String user, EsObject messageOut) { 
           getApi().sendPluginMessageToUser(user, messageOut);
        }
    }
    
    public class MyPlugin extends CommonPluginMethods {
       // normal plugin stuff
    }
    If by plugins you mean objects that you created using "new MyPlugin()" then those are not plugins, even if they extend BasePlugin. Let me know if that is what you meant and I'll try to explain more clearly how to fix it.
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts