Solved String is returning as null

Discussion in 'Plugin Development' started by Sabersamus, May 23, 2013.

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

    Sabersamus

    Before I being, no, it's not an NPE, just "null".

    Okay, so. I'm creating a zone plugin, and when I enter a zone, it's supposed to send a message saying you have entered zone.getName().

    Here's where that happens ^

    Code:java
    1.  
    2. public class PlayerMoveListener implements Listener{
    3.  
    4. @EventHandler
    5. public void onMove(PlayerMoveEvent event){
    6. Player player = event.getPlayer();
    7. Location to = event.getTo();
    8. Location from = event.getFrom();
    9. List<Zone> zones = null;
    10.  
    11. try{
    12. zones = sZones.getZones();
    13. }catch(SQLException ex){
    14. ex.printStackTrace();
    15. }
    16. for(Zone zone: zones){
    17. if(zone.contains(to) && zone.contains(from))return;
    18.  
    19. if(zone.contains(from) && !zone.contains(to)){ //Player exits a zone
    20. player.sendMessage(ChatColor.BLUE + "[sZones] " + ChatColor.RED + zone.getExitMessage());
    21. }
    22.  
    23. if(zone.contains(to) && !zone.contains(from)){ //Player enters a zone
    24. ZonePlayer zp = null;
    25. try{
    26. zp = sZones.getZonePlayer(player.getName(), zone.getName());
    27. }catch(SQLException ex){
    28. ex.printStackTrace();
    29. }
    30. if(zp.getRank().equalsIgnoreCase("banned")){
    31. event.setCancelled(true);
    32. player.sendMessage(ChatColor.BLUE + "[sZones] " + ChatColor.RED + "You are not allowed in " + zone.getName());
    33. }else{
    34. player.sendMessage(ChatColor.BLUE + "[sZones] " + ChatColor.RED + zone.getEnterMessage()); //Right here, but in game is says [sZones] null
    35. }
    36. }
    37. }
    38.  
    39. }
    40. }


    here is sZones.getZones()

    Code:java
    1.  
    2. public static List<Zone> getZones() throws SQLException{
    3. Statement stm = null;
    4. String query = "SELECT * FROM " + TABLE_ZONES + " WHERE 1";
    5. List<Zone> zones = new ArrayList<Zone>();
    6. try{
    7. stm = ConnectionManager.getConnection().createStatement();
    8. ResultSet rs = stm.executeQuery(query);
    9. while(rs.next()){ //Wow this method is gonna be huge :F
    10. //TODO: Shorten this
    11. int id = rs.getInt("id");
    12. String name = rs.getString("name");
    13. int x1 = rs.getInt("x1");
    14. int z1 = rs.getInt("z1");
    15. int x2 = rs.getInt("x2");
    16. int z2 = rs.getInt("z2");
    17. String world = rs.getString("world");
    18. boolean freeBuild = rs.getBoolean("freeBuild");
    19. /*
    20. * Might have to do a workaround for the booleans
    21. */
    22. boolean pvp = rs.getBoolean("pvp");
    23. boolean hostile = rs.getBoolean("hostile");
    24. boolean whiteList = rs.getBoolean("whiteList");
    25.  
    26. Zone zone = new Zone();
    27. zone.setId(id);
    28. zone.setName(name);
    29. zone.setX1(x1);
    30. zone.setZ1(z1);
    31. zone.setX2(x2);
    32. zone.setZ2(z2);
    33. zone.setWorld(world);
    34. zone.setFreeBuild(freeBuild);
    35. zone.setPvp(pvp);
    36. zone.setHostile(hostile);
    37. zone.setWhitelisted(whiteList); //Yes I know this is very long :p
    38.  
    39. zones.add(zone);
    40. }
    41. }catch(SQLException ex){
    42. ex.printStackTrace();
    43. }finally{
    44. if(stm != null) stm.close();
    45. }
    46.  
    47.  
    48. return zones;
    49. }



    Now, when I do print(sZones.getZone(name).getEnterMessage()); in onEnable()
    it says "You have entered blah", but when I move into the zone, it just says null.
     
  2. Offline

    Milkywayz

    You don't provide the stack trace, not

    This code makes me cringe to be quite honest, PlayerMoveEvent is called very frequently and the fact that you're making MySQL connections every time that happens to fill the zones list is insane.

    Forget the null string your design needs some major rethinking if you want it to be stable and not a major drain on performance. The Zones list, why does it need to be refreshed from config whenever a player 'moves'? AFAIK, PME is called whenever the players location changes, and the location consists of World, x,y,z, pitch, and yaw- meaning even spinning in circles will call this event?
     
  3. Offline

    Sabersamus

    There is no stack trace, everything runs smoothly, it just says "null" instead of the message.

    What do you mean why does the zones list need to be refreshed when a player moves?

    This is my first time using regular SQL statements instead of the built in database classes in bukkit's api, so I'm a little confused on how to go about doing this.
     
  4. Offline

    Milkywayz

    You load your zones from the MySQL database on PlayerMoveEvent, why not load them into memory on enable and then update as needed...
     
  5. Offline

    Sabersamus

    ^ I thought about that last night, and I did it.

    I also found out why it was returning null, very obvious and stupid mistake -_-.

    in getZones() i forgot these two things

    Code:java
    1.  
    2. zone.setEnterMessage(rs.getString("enterMessage"));
    3. zone.setExitMessage(rs.getString("exitMessage"));
    4.  


    Thanks for the help
     
  6. Offline

    lenis0012

    Please mark as resolved
     
Thread Status:
Not open for further replies.

Share This Page