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.

Referenced booleans returning NPE

Featured Replies

I am experiencing an issue when trying to implement a boolean from class B to Class A. instead of validating and returning the truth value. a NPE is outputted in the console window. I have Initialised class B in class A:

WalkingHandler w = new WalkingHandler();

and have attempted to implement like so:

if (m.players.closest(p -> p != null && !p.equals(m.myPlayer()) && (m.myPosition().getY() > 3522) && p.getPosition().distance(m.myPosition()) < 20) == null
        || m.settings.getRunEnergy() > 20) {

  w.Walk(WildyPath[i]);

}

This is my class B:

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.event.WebWalkEvent;
import org.osbot.rs07.utility.Condition;

public class WalkingHandler{
    
    main m = new main();

    boolean loggedIn = m.getClient().isLoggedIn();
    boolean inDanger =  m.players.closest(p -> p != null && !p.equals(m.myPlayer()) && (m.myPosition().getY() > 3522) && p.getPosition().distance(m.myPosition()) < 20) != null;
    boolean lowStamina =  m.settings.getRunEnergy() < 20 && m.getInventory().contains(item -> item.getName().contains("Stamina") && item != null);

    

    public void Walk(Position NextStep){
        WebWalkEvent WalkthePath = new WebWalkEvent(NextStep);
        WalkthePath.setBreakCondition(new Condition() {

            @Override
            public boolean evaluate() {
                return (!loggedIn || inDanger || lowStamina);
            }
        });


        m.execute(WalkthePath);
    }
    }

and this is the error I am receiving in the log output:

 

Error in script onStart(): MyScript Build
java.lang.NullPointerException
    at WalkingHandler.<init>(WalkingHandler.java:12)
    at WalkToDestination.<init>(WalkToDestination.java:35)
    at main.onStart(main.java:47)
    at org.osbot.rs07.event.ScriptExecutor.iIIiiiiiiIII(zf:209)
    at org.osbot.rs07.event.ScriptExecutor.start(zf:11)
    at org.osbot.db.iIIiiiiiiIII(pab:171)
    at org.osbot.s.iIiIIiiiIiiI(ex:1)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Any help would be greatly appreciated.

 

Edited by ryank645

Declare the booleans inside the walk() method

I would change those 3 booleans to functions as once they're assigned in the current state they wont update.

Edited by Ragnar Lothbrok

  • Author

@jca after adding the boolean values to the walk() method, the script will no longer crash on start, however once the script reaches the point when the walk method is called, it again produces the NPE on the first boolean in the list.


@Ragnar Lothbrok

like this for example?

boolean loggedIn(){
    return m.getClient().isLoggedIn();
}
8 minutes ago, ryank645 said:

@jca after adding the boolean values to the walk() method, the script will no longer crash on start, however once the script reaches the point when the walk method is called, it again produces the NPE on the first boolean in the list.


@Ragnar Lothbrok

like this for example?


boolean loggedIn(){
    return m.getClient().isLoggedIn();
}

Probably caused by the getClient() 

Does Main extend Script? If so instead of doing main = new Main(), you should  pass getBot().getMethods() to a MethodProvider const in the constructor of your WalkingHandler class and then call getClient() on that 

19 minutes ago, ryank645 said:

@jca after adding the boolean values to the walk() method, the script will no longer crash on start, however once the script reaches the point when the walk method is called, it again produces the NPE on the first boolean in the list.


@Ragnar Lothbrok

like this for example?


boolean loggedIn(){
    return m.getClient().isLoggedIn();
}

 

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.event.WebWalkEvent;
import org.osbot.rs07.utility.Condition;

public class WalkingHandler{
    
    private main m = new main();

    public void Walk(Position NextStep){
        WebWalkEvent WalkthePath = new WebWalkEvent(NextStep);
        WalkthePath.setBreakCondition(new Condition() {
            @Override
            public boolean evaluate() {
                return (!isLoggedIn() || isInDanger() || isLowStamina());
            }
        });
        m.execute(WalkthePath);
    }

    private boolean isLoggedIn() {
        return m.getClient().isLoggedIn();
    }

    private boolean isInDanger() {
        return m.players.closest(p -> p != null && !p.equals(m.myPlayer()) && (m.myPosition().getY() > 3522) && p.getPosition().distance(m.myPosition()) < 20) != null;
    }

    private boolean isLowStamina() {
        m.settings.getRunEnergy() < 20 && m.getInventory().contains(item -> item.getName().contains("Stamina") && item != null);
    }

}

I'd advise changing this to extend MethodProvider rather than creating a new instance of the main class.

Edited by Ragnar Lothbrok

  • Author

okay this is my WalkingHandler class now:

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.event.WebWalkEvent;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.Condition;

public class WalkingHandler extends MethodProvider{

    MethodProvider m;
    public WalkingHandler(MethodProvider m) {m.getBot().getMethods();}

    public void Walk(Position NextStep){
        WebWalkEvent WalkthePath = new WebWalkEvent(NextStep);
        WalkthePath.setBreakCondition(new Condition() {
            @Override
            public boolean evaluate() {
                return (!isLoggedIn() || isInDanger() || isLowStamina());
            }
        });
        execute(WalkthePath);
    }

    private boolean isLoggedIn() {
        return getClient().isLoggedIn();
    }

    private boolean isInDanger() {
        return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null;
    }

    private boolean isLowStamina() {
        return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null);
    }

}

Still no luck, have I maybe implemented the Constructor wrong? extending the class from MethodProvider still seams to produce the error. In the other class, the variable m is an instance of the main class. is this correct or should this be replaced?

WalkingHandler w = new WalkingHandler(m);

The npe now points to the execute line of the Walk() method in the WalkingHandler class also if that is any help.

 

@jca @Ragnar Lothbrok

Edited by ryank645

21 minutes ago, ryank645 said:

okay this is my WalkingHandler class now:


import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.event.WebWalkEvent;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.Condition;

public class WalkingHandler extends MethodProvider{

    MethodProvider m;
    public WalkingHandler(MethodProvider m) {m.getBot().getMethods();}

    public void Walk(Position NextStep){
        WebWalkEvent WalkthePath = new WebWalkEvent(NextStep);
        WalkthePath.setBreakCondition(new Condition() {
            @Override
            public boolean evaluate() {
                return (!isLoggedIn() || isInDanger() || isLowStamina());
            }
        });
        execute(WalkthePath);
    }

    private boolean isLoggedIn() {
        return getClient().isLoggedIn();
    }

    private boolean isInDanger() {
        return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null;
    }

    private boolean isLowStamina() {
        return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null);
    }

}

Still no luck, have I maybe implemented the Constructor wrong? extending the class from MethodProvider still seams to produce the error. In the other class, the variable m is an instance of the main class. is this correct or should this be replaced?


WalkingHandler w = new WalkingHandler(m);

The npe now points to the execute line of the Walk() method in the WalkingHandler class also if that is any help.

 

@jca @Ragnar Lothbrok

Almost... 

private final MethodProvider mp; 

public WalkingHandler(final MethodProvider mp){
	this.mp = mp; 
}

Doesn't need to extend MethodProvider and that'll give you a false result. 

Initialise from your onStart in Main 

new WalkingHandler(getBot().getMethods())

Then call methods like...

mp.getClient().isLoggedIn()

Also the boolean isLowStamina()  I would change to isLowEnergy() 

getSettings().getRunEnergy() < 20 

Then you can check for and interact with Stamina potion if the event stops. 

Edited by jca

20 minutes ago, ryank645 said:

okay this is my WalkingHandler class now:


import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.event.WebWalkEvent;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.Condition;

public class WalkingHandler extends MethodProvider{

    MethodProvider m;
    public WalkingHandler(MethodProvider m) {m.getBot().getMethods();}

    public void Walk(Position NextStep){
        WebWalkEvent WalkthePath = new WebWalkEvent(NextStep);
        WalkthePath.setBreakCondition(new Condition() {
            @Override
            public boolean evaluate() {
                return (!isLoggedIn() || isInDanger() || isLowStamina());
            }
        });
        execute(WalkthePath);
    }

    private boolean isLoggedIn() {
        return getClient().isLoggedIn();
    }

    private boolean isInDanger() {
        return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null;
    }

    private boolean isLowStamina() {
        return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null);
    }

}

Still no luck, have I maybe implemented the Constructor wrong? extending the class from MethodProvider still seams to produce the error. In the other class, the variable m is an instance of the main class. is this correct or should this be replaced?


WalkingHandler w = new WalkingHandler(m);

The npe now points to the execute line of the Walk() method in the WalkingHandler class also if that is any help.

 

@jca @Ragnar Lothbrok

 

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.event.WebWalkEvent;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.Condition;

public class WalkingHandler extends MethodProvider {

    public void Walk(Position NextStep){
        WebWalkEvent WalkthePath = new WebWalkEvent(NextStep);
        WalkthePath.setBreakCondition(new Condition() {
            @Override
            public boolean evaluate() {
                return (!isLoggedIn() || isInDanger() || isLowStamina());
            }
        });
        execute(WalkthePath);
    }

    private boolean isLoggedIn() {
        return getClient().isLoggedIn();
    }

    private boolean isInDanger() {
        return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null;
    }

    private boolean isLowStamina() {
        return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null);
    }

}

Then:

WalkngHandler w = new WalkingHandler();
w.exchangeContext(getBot());

 

2 minutes ago, Ragnar Lothbrok said:

 


import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.event.WebWalkEvent;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.Condition;

public class WalkingHandler extends MethodProvider {

    public void Walk(Position NextStep){
        WebWalkEvent WalkthePath = new WebWalkEvent(NextStep);
        WalkthePath.setBreakCondition(new Condition() {
            @Override
            public boolean evaluate() {
                return (!isLoggedIn() || isInDanger() || isLowStamina());
            }
        });
        execute(WalkthePath);
    }

    private boolean isLoggedIn() {
        return getClient().isLoggedIn();
    }

    private boolean isInDanger() {
        return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null;
    }

    private boolean isLowStamina() {
        return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null);
    }

}

Then:


WalkngHandler w = new WalkingHandler();
w.exchangeContext(getBot());

 

Yeah... I prefer exchangeContext(getBot()); 

However it is deprecated and marked for internal use. If the devs decide to remove it the script will break. 

  • Author

that seemed to do the trick! thanks for the fast replies and the help :D

1 hour ago, jca said:

Yeah... I prefer exchangeContext(getBot()); 

However it is deprecated and marked for internal use. If the devs decide to remove it the script will break. 

Don't worry about that warning. @Alek has said in the past it's used for internal use, so it's not likely to be removed and it's perfectly fine to use.

57 minutes ago, liverare said:

Don't worry about that warning. @Alek has said in the past it's used for internal use, so it's not likely to be removed and it's perfectly fine to use.

Yeah... I’ve heard lots of people saying different things about that. Anyhow, I was just making sure the OP knew that it was deprecated and what that means. 

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.