Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

why null error wtf

Featured Replies

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

 

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/

  • Author
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

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.