Help with Hashmaps

Discussion in 'Plugin Development' started by jonas_747, Sep 20, 2011.

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

    jonas_747

    Hello, i need some help with hashmaps saving and loading, what i want to do is to save a string plus a "mark"(a location in the world) in a hashmap file.
    So heres my code so far:
    (using the playerlistener for something later after i have figured this out)
    main class:
    Code:
    package com.jonas747.ballontp;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
     
    public class BaloonTP extends JavaPlugin {
     
        private final BaloonTPPlayerListener playerListener = new BaloonTPPlayerListener(this);
        static Logger log = Logger.getLogger("Minecraft");
        protected static Configuration CONFIG;
        public static File m_Folder;
        public File configFile;
        public void onEnable(){
            m_Folder = this.getDataFolder();
            configFile = new File(m_Folder + File.separator + "config.properties");
            File marklist = new File(this.m_Folder.getAbsolutePath() + File.separator + "marks.yml");
               if (!configFile.exists()) {
                    try {
                        if (!this.m_Folder.exists()) {
                            System.out.print("Creating Config");
                            this.m_Folder.mkdir();
                        }
                        configFile.createNewFile();
                        log.info("Done!");
                        if (!marklist.exists()) {
                            log.info("Missing Marklist, creating the file....");
                            marklist.createNewFile();
                            log.info("Done!");
                        }
    
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
               }
            fileHandler.marks = (HashMap<String, Location>) fileHandler.load(BaloonTP.m_Folder.getAbsolutePath() + File.separator + "marks.yml");
            log.info("BTP Enabled!.");
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Event.Priority.Normal, this);
        }
    
        public void onDisable(){
            log.info("BTP DISABLED.");
            fileHandler.save(fileHandler.marks, m_Folder + File.separator + "config.properties");
    
        }
        static Location loc;
        static String LastSave;
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    
            Player player = null;
            try
            {
              player = (Player)sender;
            }
            catch (Exception e)
            {
              sender.sendMessage(ChatColor.RED + "Only players can use this command!");
              return true;
            }
            if(cmd.getName().equalsIgnoreCase("BTP")){
                if(args[0] == "setlink"){
                    LastSave = args[1] + args[2];
                }
                if(args[0] == "setmark"){
                    loc = player.getLocation();
                    fileHandler.marks.put(args[0], player.getLocation());
                    fileHandler.save(fileHandler.marks, m_Folder + File.separator + "config.properties");
                }
                return true;
            }
            return false;
        }
     
    }
    
    fileHandler class:
    Code:
    package com.jonas747.ballontp;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.HashMap;
    
    import org.bukkit.Location;
    
    public class fileHandler implements Serializable  {
        static File file_marks = new File (BaloonTP.m_Folder.getAbsolutePath() + File.separator + "marks.yml");
        static HashMap<String,Location> marks = new HashMap();
        public static void save(HashMap marks, String file_marks)
        {
            try{
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file_marks));
                oos.writeObject(marks);
                oos.flush();
                oos.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public static HashMap<String, Location> load(String file_marks) {
            try{
                ObjectInputStream in = new ObjectInputStream(new FileInputStream(file_marks));
                marks = (HashMap)in.readObject();
                in.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
    
    }
    All help is appreciated!
     
  2. Serializing works a bit different than you probably think.
    You don't need to make the class that does the serializing implement Serializable, but the object that you write with oos.writeObject(obj) needs to be.

    Now HashMaps generally are serializable, but only if their contents are as well (Location isn't). So you'll have to create an own class that contains the data of a Location (worldname, x, y, z) and implements Serializable.

    Code:java
    1. public class SerLocation implements Serializable{
    2. }

    For example as a declaration.

    Things that class would have to have:
    - members for worldname, x, y, z
    - a constructor to fill those members with data from a Location
    - preferably a method that constructs a Location from the data it has (for decoding)

    Then you build your HashMap out of that (HashMap<String, SerLocation>) and then you can serialize it with writeObject.

    Hope that made it clear :)
     
  3. Offline

    jonas_747

    it makes it clearer, but why do this still give me an nullpointerexception?
    Code:
    public static void setmark(){
            try{
                String locationstring =
                BaloonTP.loc.getWorld().getName() + BaloonTP.loc.getX() + ":" +    BaloonTP.loc.getY() + ":" +    BaloonTP.loc.getZ() + ":" +    BaloonTP.loc.getYaw() + ":" +    BaloonTP.loc.getPitch() + ":";
                marks.put(BaloonTP.markName, locationstring);
            }
            catch(Exception e){
                BaloonTP.log.info("setmark failed");
                e.printStackTrace();
            }
    
        }
    
     
  4. Offline

    jonas_747

    I did, and i found out that
    Code:
    marks.put(markname, locationstring);
    is causing the error, but both of the arguments are not null, and are a string, but marks is null. So i cant use .put on anything thats null?
     
  5. No, you can't. You can't do anything with a "." on a null.
    And how could you put something into nothing, anyway?
     
  6. Offline

    jonas_747

    Well, marks is decalred as a new hasmap, i looked at some hashmap tutorials and did what stood there, so how do i get the hashmap marks to not be null?

    Thanks for your help btw :)

    Nvm, found the source of it.
     
  7. Offline

    Frizkie

    I'm having the same problem with a hashmap, what caused your nullpointerexception? I can't do anything with my hashmap without it NPE-ing.
     
  8. Offline

    jonas_747

    Well, the file i was loading the hashmap from was empty, so the hasmap just became null.
     
Thread Status:
Not open for further replies.

Share This Page