Jump to content

why null error wtf


Recommended Posts

Posted

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));
	}
}

 

Posted

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/

  • Like 1
Posted
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 :)!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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