Jump to content

Referenced booleans returning NPE


Recommended Posts

Posted (edited)

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

Posted (edited)
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
Posted (edited)

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
Posted (edited)
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
Posted
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());

 

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

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

Posted
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

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