Ebeans .findList() returns expected number of entries but they are all null

Discussion in 'Plugin Development' started by tr00p3r, Jul 23, 2015.

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

    tr00p3r

    I've been able to successfully save data to my .db file using the Persistence/Database API

    I've used Sqlite DB Browser to look inside the database and it has all the right data I expect it to save (ignore the other table, I changed the name in the model).

    [​IMG]

    When I use this code I get the output of 1 then i get NullPointerException when I try to access .getCode();

    Code:
          this.chests = plugin.getDatabase().find(TeamCompassChest.class).findList();
    
            Utils.Log(this.chests.size());
    
            for(TeamCompassChest chest : this.chests) {
    
                Utils.Log(chest.getCode());
    
                Utils.Log(chest.getLocation().getBlock().getType().toString());
            }
    Anyone got some ideas on what I am doing wrong? I can definitely save data to it consistently and I can pull back a number of null entries that relate to how many items I am querying.



    More details about my code:

    I make sure the database is setup correctly using this snippet:

    Code:
      private void setupDatabase() {
            try {
                getDatabase().find(TeamCompassChest.class).findRowCount();
            } catch (PersistenceException ex) {
                System.out.println("Installing database for " + getDescription().getName() + " due to first time usage");
                installDDL();
            }
        }
    And I override my getDatabaseClasses:

    Code:
        @Override
        public List<Class<?>> getDatabaseClasses()
        {
    
            List<Class<?>> classes = new LinkedList<Class<?>>();
    
            classes.add(TeamCompassChest.class);
    
            return classes;
    
        }
    My class is setup as I think it should be:

    Code:
    @Entity
    @Table(name = "team_compass_chest")
    public class TeamCompassChest
    {
    
        @Id
        private Long id = 0L;
    
        @Column
        private String world = "";
        @Column
        private double x = 0;
        @Column
        private double y = 0;
        @Column
        private double z = 0;
        @Column
        private String code = "";
    
        @Transient
        private Location location;
    
        public Location getLocation()
        {
    
            if (location == null)
            {
                location = Bukkit.getServer().getWorld(world).getBlockAt((int)x,(int)y,(int)z).getLocation();
            }
    
            return location;
    
        }
    
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
    
        public String getWorld() { return world; }
        public void setWorld(String world) { this.world = world; }
    
        public String getCode() { return code; }
        public void setCode(String code) { this.code = code; }
    
        public double getX() { return x; }
        public void setX(double x) { this.x = x; }
    
        public double getY() { return y; }
        public void setY(double y) { this.y = y; }
    
        public double getZ() { return z; }
        public void setZ(double z) { this.z = z; }
    
        public static TeamCompassChest Create(Location location, String code)
        {
            TeamCompassChest newCompass = new TeamCompassChest();
            newCompass.world = location.getWorld().getName();
            newCompass.x = location.getX();
            newCompass.y = location.getY();
            newCompass.z = location.getZ();
            newCompass.code = code;
            return newCompass;
        }
    
    
    }
     
Thread Status:
Not open for further replies.

Share This Page