alqqu Posted March 28, 2019 Share Posted March 28, 2019 hi, all im just trying to do is seperate some functions to some other .java files in my project so they all aren't in the same main.java, but whenever i try to use them i get null error. main.java import org.osbot.rs07.api.ui.Message; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import java.awt.*; @ScriptManifest(name = "script", author = "Alkku", version = 1.0, info = "", logo = "") public class main extends Script { TutorialIsland tutisland; static boolean TutorialIslandCompleted = false, XFER = false; @Override public void onStart() { tutisland = new TutorialIsland(); } @Override public int onLoop() { if (!TutorialIslandCompleted) tutisland.handle(); return (random(150, 350)); } public void onMessage(Message msg) throws InterruptedException { String text = msg.getMessage().toLowerCase(); } } and my TutorialIsland.java public class TutorialIsland extends main { public void handle() { log(getConfigs().get(281)); } } Quote Link to comment Share on other sites More sharing options...
Apaec Posted March 28, 2019 Share Posted March 28, 2019 Not sure why you're extending main in your TutorialIsland class, there's no need for that. Quote Link to comment Share on other sites More sharing options...
asdttt Posted March 29, 2019 Share Posted March 29, 2019 Just as @Apaec said above, you are not suppose to/don't need to extend your "main". Basically you're creating a new "main" instance when you create a new TutorialIsland instance because it's defined as a subclass of "main". So the "log" function is most likely causing a nullpointer because being that "TutorialIsland" is now a subclass of "main", it contains all of it's own objects that require initialization/setting. What you should instead do is something called "dependency injection". To do that, you simply need to pass the reference of "main" into "TutorialIsland" so you can then use it's methods. Here's a basic example: tutisland = new TutorialIsland(this); //this = "main" public class TutorialIsland { private main main_; //Where we store the reference public TutorialIsland(main main_) { this.main_ = main_; } public void handle() { main_.log(getConfigs().get(281)); } } I also HIGHLY recommend you keep class names starting with a capital letter. Read up on Java Naming Conventions, it'll help you keep your code looking pretty: https://www.geeksforgeeks.org/java-naming-conventions/ 1 Quote Link to comment Share on other sites More sharing options...
alqqu Posted March 30, 2019 Author Share Posted March 30, 2019 On 3/29/2019 at 10:57 AM, asdttt said: Just as @Apaec said above, you are not suppose to/don't need to extend your "main". Basically you're creating a new "main" instance when you create a new TutorialIsland instance because it's defined as a subclass of "main". So the "log" function is most likely causing a nullpointer because being that "TutorialIsland" is now a subclass of "main", it contains all of it's own objects that require initialization/setting. What you should instead do is something called "dependency injection". To do that, you simply need to pass the reference of "main" into "TutorialIsland" so you can then use it's methods. Here's a basic example: tutisland = new TutorialIsland(this); //this = "main" public class TutorialIsland { private main main_; //Where we store the reference public TutorialIsland(main main_) { this.main_ = main_; } public void handle() { main_.log(getConfigs().get(281)); } } I also HIGHLY recommend you keep class names starting with a capital letter. Read up on Java Naming Conventions, it'll help you keep your code looking pretty: https://www.geeksforgeeks.org/java-naming-conventions/ thank you, just what i was looking for! no more null errors :)! Quote Link to comment Share on other sites More sharing options...