[SOLVED] getPlayer() method + cannot make a static reference to a nonstatic method blah blah

Discussion in 'Plugin Development' started by Relentless_x, Apr 1, 2011.

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

    Relentless_x

    1st why wont getPlayer() work for me? I've imported the right things, im pretty sure of that the method getPlayer(string) is undefined for the type....

    2nd when im trying to call methods i wrote in other classes for my pluign in the class that extends java pluign, it says cannot make a static reference to the non static method... it does this for all my methods i have written ( all in separate classes to the main one) nothing i nthe class that extends javaplugin is static so why is this and how can i fix it?

    Thanks in advance.
     
  2. Offline

    willurd

    My guess is you're trying to do something like Server.getPlayer(), rather than getServer().getPlayer() (one calls getPlayer() on a class (which is a "static reference"), the other calls it on an instance).

    It would help if you paste the code that's not working.
     
  3. Offline

    Edward Hand

    Pasting in the exact errors as well your code would be helpful
     
  4. Offline

    Relentless_x

    aha thanks thats exactly what i was doing :p Thanks a bunch!

    I'm still having trouble with #2 though :/
     
  5. Offline

    willurd

    With #2, you're probably doing the same thing (Calling YourClass.someMethod(), rather than using an instance of YourClass inside YourPlugin. Inside YourPlugin, onEnable, you should create instances of the classes it needs to work with. Do this:

    Code:
    public class YourPlugin extends JavaPlugin {
    	YourClass yc;
    	public void onEnable() {
    		yc = new YourClass();
    		// Now you can do this:
    		yc.someMethod();
    	}
    }
    
    Don't do this:

    Code:
    YourClass.someMethod();
    
    If you have your code up online somewhere (like github) I can take a look and (hopefully) tell you for sure what your problem is. Or maybe just pasting your plugin class might be enough? Also, paste the exact error messages you're getting (stack traces and all...if you don't know what that is, paste your entire console output).

    NOTE: At the risk of conflating my answer, YourClass.someMethod is totally acceptable in Java, as long as someMethod is a static method. But don't worry about that now, just follow what I said above...and definitely paste your code/errors.
     
    Felixx61 likes this.
  6. Offline

    Relentless_x

    It worked thanks so much for your help, it is much appreciated! I'm still fairly new to java!
     
  7. Offline

    willurd

    No problem at all :) Happy coding!

    Also, I recommend updating your thread title with [SOLVED], like "[SOLVED] getPlayer() method + cannot make a static reference to a nonstatic method blah blah".

    There should be a link on the page like "Thread Tools" or something.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
Thread Status:
Not open for further replies.

Share This Page