Null Pointer Exception (Last Reply)

Discussion in 'Plugin Development' started by GeekyCompz, Jul 4, 2014.

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

    GeekyCompz

    So I have an array list called 'groups' and I go through a config file and add all the groups names to it.

    Now I need to get all the keys inside each of the group by its name from the groups arraylist.

    Code:
        private ArrayList<CourseObject> courses = new ArrayList<CourseObject>();
        private ArrayList<String> courseGroups = new ArrayList<String>();
        private ArrayList<String> courseNames = new ArrayList<String>();
       
       
        public void setup() {
            for (String name : plugin.courses.getConfigurationSection("").getKeys(false)) {
                courseGroups.add(name);
            }
           
            for (String group : courseGroups.???) {
                courseNames.add(name);
            }
        }
     
  2. Offline

    1Rogue

    Code:java
    1. for (String group : courseGroups) {
    2. for (String key : plugin.courses.getConfigurationSection(group)) {
    3. //code
    4. }
    5. }
     
  3. Offline

    GeekyCompz

    This is my updated code but I cannot get it to create a new class object.
    Code:
        private ArrayList<CourseObject> courses = new ArrayList<CourseObject>();
        private ArrayList<String> courseGroups = new ArrayList<String>();
        private ArrayList<String> courseNames = new ArrayList<String>();
       
       
        public void setup() {
            for (String name : plugin.courses.getConfigurationSection("").getKeys(false)) {
                courseGroups.add(name);
            }
           
            for(int i=0;i<courseGroups.size();i++) {
                for (String n : plugin.courses.getConfigurationSection(courseGroups.get(i)).getKeys(false)) {
                    courseNames.add(n);
                }
            }
           
            for(int i=0;i<courseNames.size();i++) {
                for (String name : courseNames.get(i)) {
                    courses.add(new CourseObject(name));
                }
        }
    Here is the CourseObject class.
    Code:
    public class CourseObject {
     
        private CourseObject() { }
       
        private static CourseObject instance = new CourseObject();
       
        public static CourseObject getInstance() {
            return instance;
        }
       
        EnhancedParkour plugin;
        CourseObject (EnhancedParkour plugin) {
            this.plugin = plugin;
        }
       
        protected String name;
        protected String group;
        protected Location start, end;
        protected ArrayList<Player> players = new ArrayList<Player>();
       
        public void Course(String n) {
            this.name = n;
           
            ConfigurationSection cs = plugin.courses.getConfigurationSection(n);
           
            this.start = getLocation(cs.getConfigurationSection("start"));
            this.start = getLocation(cs.getConfigurationSection("end"));
        }
       
        public Location getLocation(ConfigurationSection path) {
            return new Location(
                    Bukkit.getWorld(path.getString("World")),
                    path.getDouble("X"),
                    path.getDouble("Y"),
                    path.getDouble("Z"),
                    (float)path.getInt("Yaw"),
                    (float)path.getInt("Pitch"));
        }
       
        public String getName() {
            return name;
        }
       
        public String getGroup() {
            return group;
        }
       
        public Location getStart() {
            return start;
        }
       
        public Location getEnd() {
            return end;
        }
       
        public boolean containsPlayer(Player p) {
            if (players.contains(p)) return true;
            return false;
        }
       
    }
    
     
  4. Offline

    fireblast709

  5. Offline

    GeekyCompz

    fireblast709 Thanks, but how would I go about accessing the EnhancedParkour class as well as it not being a singleton and having other classes get into that one?
     
  6. Offline

    teej107

  7. Offline

    GeekyCompz

    teej107 How? I am not a java expert, just a beginner not a full time thingo... Sorry.
     
  8. Offline

    teej107

  9. Offline

    GeekyCompz

    teej107 I know about this but how can I allow any class to access the class. How can I fix the main problem of this topic? Could you give me code please?
     
  10. Offline

    fireblast709

    GeekyCompz two things:
    1. parameters are something you should be able to use as a beginner, since they are a fundamental part of Java you will need.
    2. No spoonfeeding.
      • Remove the singleton part
      • Either load all courses in your main class and add a getter method for aquiring them or load them when you need them (the first is generally better since you don't need to access the files all the time)
      • read the page teej107 linked you
      • your Course constructor sets start to start, then start to end. This should be end to end :p (just to get a bug out there already)
     
  11. Offline

    GeekyCompz

    fireblast709 All I need to know is how to make a new standalone of the CourseObject class and send it the group and name.
    Code:
    public class CourseManager {
     
    private CourseManager() { }
     
    private static CourseManager instance = new CourseManager();
     
    public static CourseManager getInstance() {
    return instance;
    }
     
    EnhancedParkour plugin;
     
    CourseManager(EnhancedParkour plugin) {
    this.plugin = plugin;
    }
     
    private ArrayList<CourseObject> courses = new ArrayList<CourseObject>();
    private Map<String, String> courseDetails = new HashMap<String, String>();
     
     
    public void setup() {
    for (String group : plugin.courses.getConfigurationSection("").getKeys(false)) {
    for (String name : plugin.courses.getConfigurationSection(group).getKeys(false)) {
    courseDetails.put(group, name);
    }
    }
     
    for(int i=0;i<courseDetails.size();i++) {
    for (String group : courseDetails.values()) {
    courses.add(new CourseObject(group, courseDetails.get(group)));
    }
    }
    }
     
    public CourseObject getCourse(Player p) {
    for (CourseObject cO : courses) {
    if (cO.containsPlayer(p)) return cO;
    }
    return null;
    }
     
    public CourseObject getCourse(String n) {
    for (CourseObject cO : courses) {
    if (cO.getName() == n) return cO;
    }
    return null;
    }
     
    }
    
    Code:
    public class CourseObject {
     
    EnhancedParkour plugin;
     
    CourseObject(EnhancedParkour plugin) {
    this.plugin = plugin;
    }
     
    protected static String group, name;
     
    protected Location start, end;
    protected ArrayList<Player> players = new ArrayList<Player>();
     
    protected CourseObject(String g, String n) { 
    CourseObject.name = n;
    CourseObject.group = g;
     
    ConfigurationSection cs = plugin.courses.getConfigurationSection(n);
     
    this.start = getLocation(cs.getConfigurationSection("start"));
    this.end = getLocation(cs.getConfigurationSection("end"));
     
    plugin.logger.info("The course of " + name + " in " + group + " has been initialized.");
    }
     
     
    public Location getLocation(ConfigurationSection path) {
    ConfigurationSection cs = plugin.courses.getConfigurationSection("");
     
    if (path == cs.getConfigurationSection("start")) {
    return new Location(
    Bukkit.getWorld(path.getString("World")),
    path.getDouble("X"),
    path.getDouble("Y"),
    path.getDouble("Z"),
    (float)path.getInt("Yaw"),
    (float)path.getInt("Pitch"));
    } else {
    return new Location(
    Bukkit.getWorld(path.getString("World")),
    path.getDouble("X"),
    path.getDouble("Y"),
    path.getDouble("Z"),
    (float)path.getInt("Yaw"),
    (float)path.getInt("Pitch"));
    }
    }
     
    public String getName() {
    return name;
    }
     
    public String getGroup() { 
    return group;
    }
     
    public Location getStart() {
    return start;
    }
     
    public Location getEnd() { 
    return end;
    }
     
    public boolean containsPlayer(Player p) {
    if (players.contains(p)) return true;
    return false;
    }
     
    }
    
    I get an error in the CourseManager on line 30 - NULL POINTER. By the way I am following this; https://github.com/pogostick29dev/MagicBattle/tree/master/src/me/pogostick29dev/magicbattle the ArenaManager and Arena class.
     
  12. Offline

    fireblast709

    GeekyCompz the fact that you create multiple CourseObjects and add them to a List shows that you don't need a 'standalone'. Also you get a NPE (NullPointerException) because you do not seem to understand how member variables work. You never assigned plugin. You did in your CourseManager(EnhancedParkour) constructor you say? Expected reply, but is that constructor even being used ;)
     
  13. Offline

    GeekyCompz

    fireblast709 I thought I did assign the plugin though? What am I doing wrong?
     
  14. Offline

    fireblast709

    GeekyCompz you create a CourseManager with an no args constructor. Which you defined on the first line of the class. That is why plugin is null.
     
Thread Status:
Not open for further replies.

Share This Page