stupid NullPointerException

Discussion in 'Plugin Development' started by Carts, Jan 25, 2012.

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

    Carts

    I'm trying to Spawn a Monster/Animal, but it gives me NullPointerException.

    The code:

    Code:
     try
    {
    Location loc = new Location(Bukkit.getWorld(Cunfig.db.get(i+".World").toString()),Double.valueOf(Cunfig.db.get(i+".X").toString()) ,Double.valueOf(Cunfig.db.get(i+".Y").toString()) ,Double.valueOf(Cunfig.db.get(i+".Z").toString()) ,Float.valueOf(Cunfig.db.get(i+".Yaw").toString()) ,Float.valueOf(Cunfig.db.get(i+".Pitch").toString()));
    for (int ii = 0; ii < 11;) {
    /* Here */ Bukkit.getWorld(Cunfig.db.get(i+".World").toString()).spawnCreature(loc, CreatureType.fromName(Cunfig.db.get(i+".Mob").toString()));
    ii++;
    }
    }
    catch (Exception e)
    {
    e.printStackTrace(System.err);
    }
    Any Help? Looked for the error and found nothing.

    All the Code:
    Show Spoiler
    Code:
    package com.github.DCarts;
    import net.milkbowl.vault.permission.Permission;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.CreatureType;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    public class MineSpawner extends JavaPlugin {
    public static Permission Perm = null;
    public static String Nome = "[MineSpawner] ";
    public static String CNome = ChatColor.AQUA+Nome+ChatColor.GREEN;
    private boolean LigPerms()
    {
    RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
    if (permissionProvider != null) {
    Perm = permissionProvider.getProvider();
    }
    return (Perm != null);
    }
    public static boolean P(CommandSender s, String pm) {
    if (s instanceof Player) {
    Player p = (Player) s;
    if (p.isOp()) {
    return true;
    }
    return MineSpawner.Perm.has(s, pm);
    } else {
    return true;
    }
    }
    public void onEnable() {
    CommandExecutor CmdExecutor = new CmdExecutor();
    getCommand("setarmob").setExecutor(CmdExecutor);
    try { Cunfig.firstRun() ;
    } catch(Exception ex) {
    ex.printStackTrace(System.err); }
     
    LigPerms();
    Main();
    System.out.println(Nome+"Ativado com Sucesso.");
    }
    public void onDisable() {
    System.out.println(Nome+"Desativado com Sucesso.");
    }
    public static int cNum() {
    if (!Cunfig.dbfile.exists()) {
    try {
    Cunfig.firstRun();
    }
    catch (Exception ex) {
    ex.printStackTrace(System.err);
    }
    }
    Cunfig.loadDB();
    Cunfig.db.set("i", Integer.valueOf(Cunfig.db.get("i").toString())+1);
    Cunfig.saveDB();
    return Integer.valueOf(Integer.valueOf(Cunfig.db.get("i").toString()));
    }
    public void Main() {
    Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    public void run() {
    Exec();
    }
    }, 60L, 60L);
    }
    public void Exec() {
    Cunfig.loadDB();
    String st = Cunfig.db.get("i").toString();
    for (int i = 1; i <= Integer.valueOf(st);i++) {
    System.out.println(i);
    if(Cunfig.db.contains(i+".Ligado")) {
    try
    {
    Location loc = new Location(Bukkit.getWorld(Cunfig.db.get(i+".World").toString()),Double.valueOf(Cunfig.db.get(i+".X").toString()) ,Double.valueOf(Cunfig.db.get(i+".Y").toString()) ,Double.valueOf(Cunfig.db.get(i+".Z").toString()) ,Float.valueOf(Cunfig.db.get(i+".Yaw").toString()) ,Float.valueOf(Cunfig.db.get(i+".Pitch").toString()));
    for (int ii = 0; ii < 11;) {
    Bukkit.getWorld(Cunfig.db.get(i+".World").toString()).spawnCreature(loc, CreatureType.fromName(Cunfig.db.get(i+".Mob").toString().toUpperCase()));
    ii++;
    }
    }
    catch (Exception e)
    {
    e.printStackTrace(System.err);
    }
    }
    }
    }
    }


    And, How do I use the code:java in this Forum ?
     
  2. Offline

    Bruno Lanevik

    Where do you get "Cunfig" from?
    Also what row did you get the nullpointer?
     
  3. Offline

    Carts

    Cunfig is from this:
    Code:
    package com.github.DCarts;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.YamlConfiguration;
     
    public class Cunfig {
        static File dbfile = new File(Bukkit.getPluginManager().getPlugin("MineSpawner").getDataFolder(), "db.yml");
        static YamlConfiguration db = new YamlConfiguration();
        public static void firstRun() throws Exception {
            if(!dbfile.exists()){
                dbfile.getParentFile().mkdirs();
                copy(Bukkit.getPluginManager().getPlugin("MineSpawner").getResource("db.yml"), dbfile);
            }
        }
        private static void copy(InputStream in, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while((len=in.read(buf))>0){
                    out.write(buf,0,len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void saveDB() {
            try {
                db.save(dbfile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static void loadDB() {
            try {
                db.load(dbfile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    I Have all the specified Strings in the db.yml

    and the nullpointer is from line 335 of CraftWorld, in Craftbukkit, and it breaks my Bukkit.getWorld(world).spawnCreature(loc, creaturetype);
     
  4. In your for loop you declare 'ii' but then still use 'i'. So mabey thats the problem, because in the config the keys doesn't exists.
     
  5. Offline

    Carts

    'i' is Defined in the loop before this.
    And the Keys exists in Config.
     
  6. Offline

    DirtyStarfish

    I guess the world or mob is null. Is the world defined in the config an actual world on the server, also, is the mob listed in the config actually a valid mob? Check your capitals etc just incase.
     
  7. Offline

    Carts

    The world is correct (Aden (The name of my default world) ), and the mob is valid (COW).

    The full error is this:
    Code:
    [SEVERE] java.lang.NullPointerException
    [SEVERE]at org.bukkit.craftbukkit.CraftWorld.spawnCreature(CraftWorld.java:335)
    [SEVERE]at com.github.DCarts.MineSpawner.Exec(MineSpawner.java:99)
    [SEVERE]at com.github.DCarts.MineSpawner$1.run(MineSpawner.java:85)
    [SEVERE]at org.bukkit.craftbukkit.scheduler.CraftWorker.run(CraftWorker.java:34)
    [SEVERE]at java.lang.Thread.run(Unknown Source)
    the 99 line is exacty the line of Bukkit.getWorld(blablabla).spawnCreature . . .
     
  8. Offline

    tkausl

    Code:
    CreatureType.fromName(Cunfig.db.get(i+".Mob").toString())
    returns null.
    You have to find out why.
     
  9. Offline

    Carts

    Now i'll never use .fromName D=
    tried with .valueOf and worked without Problems.

    Thanks tkausl.
     
Thread Status:
Not open for further replies.

Share This Page