Java question

Discussion in 'Plugin Development' started by Sammy, Mar 30, 2011.

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

    Sammy

    Hey ppl
    Since I started programing in java I haven't found a good way to get around this type of problem
    For example:
    Code:
        public void msgPly(String msg) {
            plugin.getServer().getPlayer(maker).sendMessage(msg);
            Object[] players = scores.keySet().toArray();
            for (int x = 0; x < scores.size(); x++) {
                Player ply = plugin.getServer().getPlayer(players[x].toString());
                ply.sendMessage(msg);
            }
        }
    If the player on the variable maker isn't online I'll get a "java.lang.NullPointerException"...
    If I use a try/catch the method ends but,

    What could I do to make java acknowledge the error but still continue to the next line of code?
     
  2. Offline

    Edward Hand

    Code:
    Player player = plugin.getServer().getPlayer(maker);
    if(player != null)
       player.sendMessage(msg);
    //then do other stuff
     
  3. Offline

    Sammy

    Let me see if I get it, every time there's a change I get a null value that will gives me an error, I should declare a variable that let's me check if I returned null or not ?
     
  4. Offline

    Edward Hand

    Yes. Better to do checks to prevent errors than to try and deal with the aftermath of what happens when you get one I think.
     
  5. Offline

    Sammy

    Thanks Edward, the tutorials I used to learn Java always expressed the need of using try/catch, but I never liked using them on a regular basis, 'cause it's like what you said, we are just dealing with them aftermath.
    Thanks again :)
     
Thread Status:
Not open for further replies.

Share This Page