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
  • Try asking for help in the chatbox
  • Combat Script null pointer exception


    JAG98
     Share

    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

    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
     Share

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