Null pointer help

Discussion in 'Plugin Development' started by JJSfluffyduck, Nov 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    JJSfluffyduck

    I get a null pointer from this any help would be great

    I call the method by this
    Code:
                        if (Protect == true) {
                            for (int i = 0; i < PlayerName.length; i++) {
                                Player PlayerBeingUnBlocked = PlayerName[i];
                                PlayerUnBlock(PlayerBeingUnBlocked);
                            }
                            sender.sendMessage(ChatColor.RED + "Protection Off!");
    And the method is this
    Code:
        public void PlayerUnBlock(Player p) {
            for (int i = 0; i < PlayerName.length; i++) {
                Player PlayerNum = PlayerName[i];
                if (p.equals(PlayerNum)) {  //This line give the null pointer
                    Inventory playerStuff = PL[i];
                    p.openInventory(playerStuff);
                    p.setOp(true);
                    p.setGameMode(GameModePlayer[i]);
                    if (OP.PJ.containsKey(p)) {
                        OP.PJ.remove(p);
                    }break;
                }
            }
        }
    It looks like p sometimes give a null pointer some how would i cancel when p == null;?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  2. Offline

    CorrieKay

    By surrounding it with an if statement :p

    if(p == null){
    return;
    }
     
  3. Offline

    JJSfluffyduck

    Thanks that worked

    Just another question when someone does this what u happening
    For(var1 : var2){ }?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    fireblast709

    JJSfluffyduck it is called a foreach loop. When var2 it Iteratable (like an ArrayList object or an array), it will execute the loop for any item in that ArrayList or array
     
Thread Status:
Not open for further replies.

Share This Page