Unhandled exception executing command

Discussion in 'Plugin Development' started by creppii, Nov 11, 2014.

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

    creppii

    hey com,
    i get an "Unhandled exception executing command" error.
    whats wrong with this code?
    Code:java
    1. //If command is executed
    2. String p = sender.getName();
    3. player.sendMessage("§6Deine Coins: §b" + getCoins(p));
    4.  
    5. //function
    6. public int getCoins(String p) {
    7. int coins = 0;
    8. mysql sql = this.plugin.getMySQL();
    9. Connection con = sql.getConnection();
    10. ResultSet rs = null;
    11. try {
    12. st = con.prepareStatement("SELECT * FROM coins WHERE name = ?");
    13. st.setString(1, p);
    14. rs = st.executeQuery();
    15. rs.last();
    16. if(rs.getRow() != 0) {
    17. rs.first();
    18. coins = rs.getInt("coins");
    19. }
    20. } catch (SQLException e) {
    21. e.printStackTrace();
    22. } finally {
    23. sql.closeRessources(rs, st);
    24. }
    25.  
    26. return coins;
    27. }
     
  2. Offline

    mythbusterma

    What's wrong with it?

    • It doesn't seem you check to see that the sender is actually a Player
    • mysql is all lower case, doesn't seem to be a type
    • You're querying an SQL database from the main thread, a big no-no because SQL databases can take a non-determinate amount of time to respond
    That's just from a cursory glance, there's probably more wrong with it.
     
  3. Offline

    Rocoty

    mythbusterma How do you know he's not querying the database asynchronously. I mean, it's a pretty fair guess, but you can't really know, can you?
     
  4. Offline

    acer5999

    This. You have to check if the player is actually an instanceof player.
    Code:
    if(!(sender instanceof Player)) {
        sender.sendMessage("This command can only be run by players!");
    return;
    }
     
  5. Offline

    JordyPwner

    Ugh i hate Spoonfeeders :p
     
    Funergy likes this.
  6. Offline

    acer5999

  7. Offline

    JordyPwner

  8. Offline

    acer5999

    I gave an explanation
     
  9. Offline

    mythbusterma

    Rocoty

    Because he's returning the value of that function to a command. It would be far above his skill level to figure anything like that out, so I feel it's a fair assessment judging by the layout.
     
Thread Status:
Not open for further replies.

Share This Page