Jump to content

why null error wtf


alqqu

Recommended Posts

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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 :)!

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...