Jump to content

Non-static method cannot be referenced from static context error


Impensus

Recommended Posts

Hi guys,

I'm trying to use a script with multiple classes. For the context of this we have: Main, Farming, Banking.

In Main I have the following: 

public class Main extends Script {
	private Farming farming = new Farming();
    private Banking banking = new Banking();

    public void onStart() {
        farming.exchangeContext(getBot());
        banking.exchangeContext(getBot());
    }

    public int onLoop() {
	//do stuff
	}
}

 

In Farming I have:

public class Farming extends MethodProvider{
	private Banking banking = new Banking();
	public void myMethod(){
		if(xyz == true){
			banking.BankingMethod();
		}
	}
}

 

And finally Banking:


public class Banking extends MethodProvider {
	public void bankingFunction(){
		//Do banking stuff
	}
}

 

Now my issue is, whenever I call the functions from Banking by instantiating the Banking class inside Farming, I always get a NullPointerException and the programme crashes instantly. This happens regardless of the classes I use. 

 

How do I properly instantiate and use classes from within another class, where said class isn't main? I tried doing this by just calling Banking.bankingFunction() and got the error in the title.

 

Many thanks,

 

- Impensus

Link to comment
Share on other sites

I would recommend creating a static instance of MethodProvider in your main class and then passing the class as "this" to it in the onStart() method. Due to the fact your script extends "Script" and Script is a child of the MethodProvider class, you can use the parent class (thanks polymorphism) to reference your main class. This will give you all the same methods without the script methods such as onLoop, onStart, onExit, etc. We don't pass a script object because we only need to call those methods once.

public class Main extends Script {

    public static MethodProvider instance;

    public void onStart() {
        instance = this; 
    }

    public int onLoop() {
        /do stuff
    }
}

Then you can access your single method provider from your other classes like so:

public class Banking {
	public void bankingFunction(){
		Bank bank = Main.instance.getBank();
	}
}

and in your on loop if you want to call your banking function all you would do is instantiate an object an call the method in your onLoop().

    private Banking banking;

    public void onStart() {
        banking = new Banking();
    }

    public int onLoop() {
        banking.bankingFunction();
    }

Even better would be to encapsulate our instance variable and add a getInstance() method in our main class.

private static MethodProvider instance;

public void onStart() {
    instance = this;
}

public static MethodProvider getInstance() {
    return instance;
}

 

Edited by ExtraBotz
Added Clarification
Link to comment
Share on other sites

As dreameo said singletons are great to use and I recommend that as these classes are being used to execute a task and you would only ever need a single instance of them. As for the error you were getting, this is because of you calling the class name followed by the method. You need to use the variable name followed by the method. IE.

Banking banking = new Banking();

banking.method();

Notice the lowercase b. If you use an uppercase than it will reference the class itself.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...