Impensus Posted May 25, 2020 Share Posted May 25, 2020 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 Quote Link to comment Share on other sites More sharing options...
ExtraBotz Posted May 25, 2020 Share Posted May 25, 2020 (edited) 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 May 25, 2020 by ExtraBotz Added Clarification Quote Link to comment Share on other sites More sharing options...
dreameo Posted May 25, 2020 Share Posted May 25, 2020 You have two different 'bank' objects and only one is being initialized using 'exchangeContext'. What you could do is make those classes singletons and ensure the main class exchange context prior to them being used elsewhere Quote Link to comment Share on other sites More sharing options...
BravoTaco Posted May 25, 2020 Share Posted May 25, 2020 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. Quote Link to comment Share on other sites More sharing options...