+ Reply to Thread
Results 1 to 6 of 6

Thread: nullpointerexception with baseplugin

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 2 Posts

    nullpointerexception with baseplugin

    Hello there , i made a class who should send my plugin response but i got a NullPointerException on my code ( test.getApi().sendPluginMessageToUser(user, message); ) the user and message variable is not empty :

    Code:
    import com.electrotank.electroserver5.extensions.BasePlugin;
    import com.electrotank.electroserver5.extensions.api.value.EsObject;
    
    public class Sender{
        
        private  BasePlugin test;
        
        public void init(){
            
        }
        
        public void SendToUser(String user,EsObject message){
            test.getApi().sendPluginMessageToUser(user, message);
        }
            
         public void SendToRoom(EsObject messageOut){
            
            test.getApi().sendPluginMessageToRoom(test.getApi().getZoneId(),test.getApi().getRoomId(),messageOut);
            
        }
    }
    help me please :'( .

  2. #2
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,214
    Thanks
    80
    Thanked 1,086 Times in 1,075 Posts
    You can't instantiate your own BasePlugin variable, although you can add a setter to the class to pass in a BasePlugin (or whatever the actual class is) variable from a real plugin.

    What you probably want to do is have a Java class that extends BasePlugin. Have you read the Hello World Plugin tutorial?

    I suggest that you look at the dozens of code examples and game examples and choose one that does part of what you want to do, then get it working (as is) on your local ES5. Start adding one new feature at a time, while you learn the ropes.
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 2 Posts
    hello , i no really want a java class that extends baseplugin ! cause i have already one , my sender.java class have mission to do the SendToUser job without access my main plugin class ( who have the extens BasePlugin) , i understand a lot of es5 now , but i just don't understand why i can't use getapi(). , so i try instanciate a baseplugin as variable , but no work anyway

  4. #4
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,214
    Thanks
    80
    Thanked 1,086 Times in 1,075 Posts
    Let's assume that your plugin class is named MyPlugin, which extends BasePlugin, and which has a Sender variable.

    Add a BasePlugin parameter to the init method of Sender, and store that in your test variable.

    When MyPlugin instantiates Sender, have it call init(this). That should fix your NPE.
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 2 Posts
    ho thanks !! that work great now , you are the best .

  6. #6
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,214
    Thanks
    80
    Thanked 1,086 Times in 1,075 Posts
    What I usually do is have a class variable that is PluginApi api, and then the plugin passes getApi() in either an init or a setter (or even a static method). Then instead of using test.getApi().whatever my non-plugin class would just use api.whatever.

    I assume that you will have a fresh instance of your Sender class with each room that has the plugin. This is needed so that getApi().getRoomId() returns the correct roomId for the room.
    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