Jump to content
Eco

NPC Trading problem

Recommended Posts

Hey guys this is my first private script and it basically buys an item from a store, hops, and repeats.  However, i keep getting this weird problem where my player trades/closes the store 3-4 times before it actually starts buying the item.  In addition, i keep getting this error below but the script continues to run fine.  However, after literally 20 minutes every time, the script just stops working and sits there (still running though).

 

the error:

 

[ERROR][bot #1][05/10 11:10:14 PM]: Error in script executor!
java.lang.NullPointerException
at org.osbot.rs07.api.Store.getAmount(jg:173)
at main.onLoop(main.java:48)
at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(lk:242)
at java.lang.Thread.run(Thread.java:745)
 
 
Here is the method i use to trade the NPC:
 

NPC example = npcs.closest("Example");

        

        

        if (example != null){

        example.interact("Trade");

        sleep(random(87,167));

        }

        

        Store exampleStore = getStore();

        int amount = exampleStore.getAmount("Example item");

        

        while (amount > 200) {

            exampleStore.buy("Example item", 10);

            amount = exampleStore.getAmount("Example item");

            sleep(random(18,39));

        }

        

        vialStore.close();

        sleep(random(48,107));

 

So in short, my main questions are:

- Why does it keep spam trading/closing 3-4 times?

- Why does the script just stop functioning after 20ish minutes? (Could be due to hopping, interactiong, etc. not sure)

 

If someone wants me to pm them my source code just ask!  It might be helpful in figuring out whats going on.  Thanks so much!

 

 

Link to comment
Share on other sites

Your logic is a bit off, you should only trade the npc when store window is closed and buy when its opened, you are getting that npe because it thinks thw store is open and tries buying stuff.

The error is at .getAmount()

Will EDIT more info in a few gota catch a bus.

 

Ah gotcha, so should I do something like this?

 

if (example != null && !exampleStore.isOpen()){

        example.interact("Trade");

        sleep(random(87,167));

}

Link to comment
Share on other sites

Ah gotcha, so should I do something like this?

if (example != null && !exampleStore.isOpen()){

example.interact("Trade");

sleep(random(87,167));

}

Yes somethink like that, if that doesnt work use a null check on the store.

Because when you did .getAmount() it tried accessing a nulled objects info.

To avoid that you would do:

Store exampleStore = getStore();
if(exampleStore != null && exampleStore.isOpen()){
    if(exampleStore.getAmount("item") > 200){
        //Buy 
   }
}
That should do the buying Edited by Vilius
Link to comment
Share on other sites

Yes somethink like that, if that doesnt work use a null check on the store.

Because when you did .getAmount() it tried accessing a nulled objects info.

To avoid that you would do:

Store exampleStore = getStore();
if(exampleStore != null && exampleStore.isOpen()){
    if(exampleStore.getAmount("item") > 200){
        //Buy 
   }
}
That should do the buying

 

 

Hmm I implemented that and now it opens up the store and just sits there, eventually skipping the buying then hopping worlds.

 

Edit: Actually nevermind, i managed to fix it by not making a Store instance.  Instead, I just used store.getAmount etc without importing the Store class.

Yes somethink like that, if that doesnt work use a null check on the store.

Because when you did .getAmount() it tried accessing a nulled objects info.

To avoid that you would do:

Store exampleStore = getStore();
if(exampleStore != null && exampleStore.isOpen()){
    if(exampleStore.getAmount("item") > 200){
        //Buy 
   }
}
That should do the buying

 

 

Now i get this error for world hopping... what does it mean?

 

[ERROR][bot #1][05/11 01:11:05 AM]: Error in script executor!
java.lang.NullPointerException
at org.osbot.rs07.api.ui.RS2Widget.iiIIiiIIiI(li:106)
at org.osbot.rs07.api.ui.RS2Widget.iiIIiiIIiI(li:76)
at org.osbot.rs07.api.ui.RS2Widget.getAbsX(li:138)
at org.osbot.rs07.api.ui.RS2Widget.getAbsX(li:109)
at org.osbot.rs07.api.ui.RS2Widget.getAbsX(li:109)
at org.osbot.rs07.api.ui.RS2Widget.getAbsX(li:109)
at org.osbot.rs07.api.ui.RS2Widget.getPosition(li:300)
at org.osbot.rs07.api.ui.RS2Widget.getBounds(li:244)
at org.osbot.rs07.api.Worlds.iiIIiiIIiI(jf:213)
at org.osbot.rs07.api.Worlds.iIiiIIIIIi(jf:1)
at org.osbot.rs07.api.Worlds.hop(jf:87)
at main.onLoop(main.java:100)
at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(lk:242)
at java.lang.Thread.run(Thread.java:745)

This is the line for world hopping:

getWorlds().hop(hopWorlds[count]);
Edited by Economics
Link to comment
Share on other sites

Hmm I implemented that and now it opens up the store and just sits there, eventually skipping the buying then hopping worlds.

Edit: Actually nevermind, i managed to fix it by not making a Store instance. Instead, I just used store.getAmount etc without importing the Store class.

Now i get this error for world hopping... what does it mean?

[ERROR][bot #1][05/11 01:11:05 AM]: Error in script executor!

java.lang.NullPointerException

at org.osbot.rs07.api.ui.RS2Widget.iiIIiiIIiI(li:106)

at org.osbot.rs07.api.ui.RS2Widget.iiIIiiIIiI(li:76)

at org.osbot.rs07.api.ui.RS2Widget.getAbsX(li:138)

at org.osbot.rs07.api.ui.RS2Widget.getAbsX(li:109)

at org.osbot.rs07.api.ui.RS2Widget.getAbsX(li:109)

at org.osbot.rs07.api.ui.RS2Widget.getAbsX(li:109)

at org.osbot.rs07.api.ui.RS2Widget.getPosition(li:300)

at org.osbot.rs07.api.ui.RS2Widget.getBounds(li:244)

at org.osbot.rs07.api.Worlds.iiIIiiIIiI(jf:213)

at org.osbot.rs07.api.Worlds.iIiiIIIIIi(jf:1)

at org.osbot.rs07.api.Worlds.hop(jf:87)

at main.onLoop(main.java:100)

at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(lk:242)

at java.lang.Thread.run(Thread.java:745)

This is the line for world hopping:

getWorlds().hop(hopWorlds[count]);
Check line 100 in your code somewhere is a null pointer exception, but it might be something with the api, Im not really sure.
  • Like 1
Link to comment
Share on other sites

Check line 100 in your code somewhere is a null pointer exception, but it might be something with the api, Im not really sure.

just kidding about fixing the trading issue sad.png this is so frustrating haha, basically when I checked if store is null and if the store is open before buying, something is returning false which causes it to skip over the buying process and goes from trading to closing and worldhopping.  When I take away the store.isOpen() the script goes back to working but still trades 3-4 times and gives the getAmount error.  ughhh:(((

 

Also side question: if the logger gives too many errors while the script is running, will the script just stop looping (my theory as to why it stops looping at around 20 minutes >.>)

Link to comment
Share on other sites

 

Hey guys this is my first private script and it basically buys an item from a store, hops, and repeats.  However, i keep getting this weird problem where my player trades/closes the store 3-4 times before it actually starts buying the item.  In addition, i keep getting this error below but the script continues to run fine.  However, after literally 20 minutes every time, the script just stops working and sits there (still running though).

 

the error:

 

[ERROR][bot #1][05/10 11:10:14 PM]: Error in script executor!
java.lang.NullPointerException
at org.osbot.rs07.api.Store.getAmount(jg:173)
at main.onLoop(main.java:48)
at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(lk:242)
at java.lang.Thread.run(Thread.java:745)
 
 
Here is the method i use to trade the NPC:
 

NPC example = npcs.closest("Example");

        

        

        if (example != null){

        example.interact("Trade");

        sleep(random(87,167));

        }

        

        Store exampleStore = getStore();

        int amount = exampleStore.getAmount("Example item");

        

        while (amount > 200) {

            exampleStore.buy("Example item", 10);

            amount = exampleStore.getAmount("Example item");

            sleep(random(18,39));

        }

        

        vialStore.close();

        sleep(random(48,107));

 

So in short, my main questions are:

- Why does it keep spam trading/closing 3-4 times?

- Why does the script just stop functioning after 20ish minutes? (Could be due to hopping, interactiong, etc. not sure)

 

If someone wants me to pm them my source code just ask!  It might be helpful in figuring out whats going on.  Thanks so much!

 

 

 

 

Please in the future when posting in the scripting help section use the code tags, it is the blue button that looks like < > . 

 

With regards to your problem, I feel like you need to structure everything a bit better, adding ConditionalSleeps and more checks.

 

With a better structure it will be easier to debug whatever problem you are having.

 

Perhaps a structure like this would work better for you? (Sorry if there are any syntax errors)

private enum State { WALKING_TO_STORE, BUYING, HOPPING }

private final Area exampleStore = new Area(0, 0, 0, 0);

private boolean shouldHop;

@Override
public int onLoop() throws InterruptedException {
    switch(getState()){
        case WALKING_TO_STORE:
            getWalking().webWalk(exampleStore);
            break;
        case BUYING:
            buyItems();
            break;
        case HOPPING:
            hopWorlds();
            break;
    }
    return random(200, 300);
}

private State getState(){
    if(!exampleStore.contains(myPosition())) return State.WALKING_TO_STORE;
    else if(shouldHop) return State.HOPPING;
    return State.BUYING;
}

private void buyItems() {
    if(!getStore().isOpen()) openStore();
    else if(getStore().getAmount("Example item") > 0) buyAll("Example item");
    else if(getStore().close()) shouldHop = true;
}

private boolean buyAll(final String itemName){
    return getStore().buy(itemName, getStore().getAmount(itemName));
}

private boolean openStore() {
    NPC example = npcs.closest("Example");
    if(example != null && example.interact("Trade")) {
        new ConditionalSleep(5000) {
	    @Override
	    public boolean condition() throws InterruptedException {
		return getStore().isOpen();
	    }
        }.sleep();
        return true;
    }
    return false;
}

private void hopWorlds() {
    if(getWorlds().hopToP2PWorld()) shouldHop = false;
}

Here is what this code does:

While the script is running:

    If the player is not in the store:
        walk to the store

    Else, if the world hop flag has been set to true:
        Attempt to change worlds
        If the player has successfully changed worlds:
            Set the hop flag to false

    Else, if the store is not open:
        Attempt to open the store
        If the interaction was successful:
            sleep until the store is open

    Else the store is open, so if the store contains any of the item:
        Buy all of the item

    Else the store is open and doesn't contains any of the item so:
        Attempt to close the store
        If the interaction was successful:
            Sleep until the store is closed
            Set the hop flag to true

If the getStore().isOpen() method is the issue, then consider writing your own, for example:

private boolean isStoreOpen() {
    return getWidgets.getWidgetContainingText("some text found on the store interface") != null;
}
Edited by Explv
  • Like 2
Link to comment
Share on other sites

Yes somethink like that, if that doesnt work use a null check on the store.

Because when you did .getAmount() it tried accessing a nulled objects info.

To avoid that you would do:

Store exampleStore = getStore();
if(exampleStore != null && exampleStore.isOpen()){
    if(exampleStore.getAmount("item") > 200){
        //Buy 
   }
}
That should do the buying

 

 

You don't need to null check getStore()

Link to comment
Share on other sites

Try to use a conditional sleep when opening the store?

Yes it works now:)

 

Please in the future when posting in the scripting help section use the code tags, it is the blue button that looks like < > . 

 

With regards to your problem, I feel like you need to structure everything a bit better, adding ConditionalSleeps and more checks.

 

With a better structure it will be easier to debug whatever problem you are having.

 

Perhaps a structure like this would work better for you? (Sorry if there are any syntax errors)

private enum State { WALKING_TO_STORE, BUYING, HOPPING }

private final Area exampleStore = new Area(0, 0, 0, 0);

private boolean shouldHop;

@Override
public int onLoop() throws InterruptedException {
    switch(getState()){
        case WALKING_TO_STORE:
            getWalking().webWalk(exampleStore);
            break;
        case BUYING:
            buyItems();
            break;
        case HOPPING:
            hopWorlds();
            break;
    }
    return random(200, 300);
}

private State getState(){
    if(!exampleStore.contains(myPosition())) return State.WALKING_TO_STORE;
    else if(shouldHop) return State.HOPPING;
    return State.BUYING;
}

private void buyItems() {
    if(!getStore().isOpen()) openStore();
    else if(getStore().getAmount("Example item") > 0) buyAll("Example item");
    else if(getStore().close()) shouldHop = true;
}

private boolean buyAll(final String itemName){
    return getStore().buy(itemName, getStore().getAmount(itemName));
}

private boolean openStore() {
    NPC example = npcs.closest("Example");
    if(example != null && example.interact("Trade")) {
        new ConditionalSleep(5000) {
	    @Override
	    public boolean condition() throws InterruptedException {
		return getStore().isOpen();
	    }
        }.sleep();
        return true;
    }
    return false;
}

private void hopWorlds() {
    if(getWorlds().hopToP2PWorld()) shouldHop = false;
}

Here is what this code does:

While the script is running:

    If the player is not in the store:
        walk to the store

    Else, if the world hop flag has been set to true:
        Attempt to change worlds
        If the player has successfully changed worlds:
            Set the hop flag to false

    Else, if the store is not open:
        Attempt to open the store
        If the interaction was successful:
            sleep until the store is open

    Else the store is open, so if the store contains any of the item:
        Buy all of the item

    Else the store is open and doesn't contains any of the item so:
        Attempt to close the store
        If the interaction was successful:
            Sleep until the store is closed
            Set the hop flag to true

If the getStore().isOpen() method is the issue, then consider writing your own, for example:

private boolean isStoreOpen() {
    return getWidgets.getWidgetContainingText("some text found on the store interface") != null;
}

Thank you so much! It works perfectly now that conditional sleep is added after the trade:D.  Also thanks for pointing out my flawed structure and checks this is my first script ever and I'm learning a lot!  

Uh oh... I get this now

 

[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
 
it just spams that out and bot stops functioning (but still running)
after about 5-10 minutes
 

 

Link to comment
Share on other sites

 

Yes it works now:)

Thank you so much! It works perfectly now that conditional sleep is added after the trade:D.  Also thanks for pointing out my flawed structure and checks this is my first script ever and I'm learning a lot!  

Uh oh... I get this now

 

[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
[ERROR][bot #1][05/11 01:15:13 PM]: Error in script executor!
java.lang.NullPointerException
 
it just spams that out and bot stops functioning (but still running)
after about 5-10 minutes
 

 

 

 

I need more information about the error, what line number it was thrown on, what threw it etc.

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.

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