Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Combat Script null pointer exception


    JAG98

    Recommended Posts

    Just getting into scripting, okay-ish knowledge about JAVA. Second attempt at a combat script. Did not get this problem with the first one. Cross-checked and couldn't find any diff.

    Code snippet:

    Spoiler
    
    private enum State{
            HEALING, FIGHTING, LOOKING, MOVING
        }
    
        private State getState(){
    
            if(healArea.contains(getLocalPlayer())){
                if(getCombat().getHealthPercent() != 100){
                    state = State.HEALING;
                }
                else{
                    state = State.MOVING;
                }
            }
            if(combatArea.contains(getLocalPlayer())){
                if(getLocalPlayer().isInCombat()){
                    if(getLocalPlayer().getHealthPercent()<30){
                        state = State.MOVING;
                    }
                    else{
                        state = State.FIGHTING;
                    }
                }
                else{
                    state = State.LOOKING;
                }
            }
            return state;
        }
    }

    The error:

    Spoiler
    • [ERROR] Exception has occurred while running! Please report error to developer if problem persists: java.lang.NullPointerException: Cannot invoke "Main$State.ordinal()" because the return value of "Main.getState()" is null at Main.onLoop(Main.java:33) at org.dreambot.api.script.AbstractScript.run(AbstractScript.java) at java.base/java.lang.Thread.run(Thread.java:832)

    Pretty sure the return value of getState() is not null. Any help will be much appreciated!

    Link to comment
    Share on other sites

    the bottom line `return state` returns null as its never set within your `getState` loop. either give it a default value at the top of your function or ensure your states are complete to the point where it would never return a null state. Currently if the local player is outside of the heal area and the combat area it will fail

    Link to comment
    Share on other sites

    3 hours ago, TheCloakdOne said:

    the bottom line `return state` returns null as its never set within your `getState` loop. either give it a default value at the top of your function or ensure your states are complete to the point where it would never return a null state. Currently if the local player is outside of the heal area and the combat area it will fail

    Thanks! that helped! can't believe i overlooked that!

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.