NPE Help

Discussion in 'Plugin Development' started by Buizelfan2, Jul 9, 2014.

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

    Buizelfan2

    I'm getting a NPE
    Error:
    Code:
    [14:30:16] [Server thread/ERROR]: Error occurred while enabling Servers v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at net.buizelfan2dev.mobs.Mobs.spawnVillager(Mobs.java:52) ~[?:?]
        at net.buizelfan2dev.mobs.Mobs.onEnable(Mobs.java:43) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:350) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:389) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:439) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:375) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.m(MinecraftServer.java:342) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.g(MinecraftServer.java:319) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.a(MinecraftServer.java:275) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:175) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:424) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    [14:30:16]
    And it's saying line 52 and 43

    Here's all the code
    Code:java
    1. package net.buizelfan2dev.mobs;
    2.  
    3. import de.kumpelblase2.remoteentities.EntityManager;
    4. import de.kumpelblase2.remoteentities.RemoteEntities;
    5. import de.kumpelblase2.remoteentities.api.EntitySound;
    6. import de.kumpelblase2.remoteentities.api.RemoteEntity;
    7. import de.kumpelblase2.remoteentities.api.RemoteEntityType;
    8. import de.kumpelblase2.remoteentities.api.events.RemoteEntityInteractEvent;
    9. import java.io.ByteArrayOutputStream;
    10. import java.io.DataOutputStream;
    11. import java.util.logging.Logger;
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.ChatColor;
    14. import org.bukkit.Location;
    15. import org.bukkit.command.Command;
    16. import org.bukkit.command.CommandSender;
    17. import org.bukkit.entity.Entity;
    18. import org.bukkit.entity.EntityType;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.entity.Villager;
    21. import org.bukkit.event.EventHandler;
    22. import org.bukkit.event.Listener;
    23. import org.bukkit.event.entity.EntityDamageEvent;
    24. import org.bukkit.plugin.java.JavaPlugin;
    25.  
    26. public class Mobs extends JavaPlugin
    27. implements Listener
    28. {
    29. public final Logger logger = Logger.getLogger("Minecraft");
    30. public static Mobs main;
    31. private Shop shop;
    32.  
    33. public void onEnable()
    34. {
    35. this.shop = new Shop(this);
    36. getServer().getPluginManager().registerEvents(this, this);
    37. getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
    38. for (Entity e : Bukkit.getWorld("world").getEntities()) {
    39. if ((e instanceof Villager)) {
    40. e.remove();
    41. }
    42. }
    43. spawnVillager();
    44. for (Entity e : Bukkit.getWorld("world").getEntities())
    45. if ((e instanceof Villager))
    46. ((Villager)e).setProfession(Villager.Profession.LIBRARIAN);
    47. }
    48.  
    49. public void spawnVillager()
    50. {
    51. Player p = Bukkit.getPlayer(getName());
    52. Location player = p.getLocation();
    53. Location loc1 = new Location(Bukkit.getWorld("world"), 448.37535000000003D, 136.0D, 578.52853000000005D);
    54. Location loc2 = new Location(Bukkit.getWorld("world"), -26.21721D, 89.000D, -0.60274D);
    55. EntityManager manager = RemoteEntities.createManager(this);
    56. RemoteEntity creative = manager.createEntity(RemoteEntityType.Villager, loc1, false);
    57. RemoteEntity survival = manager.createEntity(RemoteEntityType.Villager, loc2, false);
    58. creative.setSound(EntitySound.RANDOM, null);
    59. creative.lookAt(player);
    60. creative.setPushable(false);
    61. creative.getBukkitEntity().setCustomName("§2Join Creative");
    62. creative.getBukkitEntity().setCustomNameVisible(true);
    63. survival.setSound(EntitySound.RANDOM, null);
    64. survival.lookAt(player);
    65. survival.setPushable(false);
    66. survival.getBukkitEntity().setCustomName("§2Join Survival");
    67. survival.getBukkitEntity().setCustomNameVisible(true);
    68. }
    69.  
    70. @EventHandler
    71. public void openShop(RemoteEntityInteractEvent e) {
    72. Player p = e.getInteractor();
    73. RemoteEntity entity = e.getRemoteEntity();
    74. if (entity.getName() == "§2Join Survival") {
    75. e.setCancelled(true);
    76. this.shop.showS(p);
    77. } else if (entity.getName() == "§2Join Creative") {
    78. e.setCancelled(true);
    79. this.shop.showC(p);
    80. }
    81. }
    82.  
    83. @EventHandler
    84. public void onDamage(EntityDamageEvent e) {
    85. Entity entity = e.getEntity();
    86. if (entity.getType() == EntityType.VILLAGER)
    87. e.setCancelled(true);
    88. }
    89.  
    90. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    91. {
    92. if (commandLabel.equalsIgnoreCase("Join")) {
    93. Player p = (Player)sender;
    94. if (!(sender instanceof Player)) {
    95. sender.sendMessage("Cant't Do This Command In Console");
    96. return true;
    97. }
    98. if (args.length == 0) {
    99. p.sendMessage("Server Not Specified");
    100. return true;
    101. }
    102. joinServer(p, args[0]);
    103. }
    104.  
    105. return false;
    106. }
    107.  
    108. public void joinServer(Player p, String targetServer) {
    109. try {
    110. out.writeUTF("Connect");
    111. out.writeUTF(targetServer);
    112. } catch (Exception e) {
    113. p.sendMessage(ChatColor.RED + "Survival Server Is Offline!");
    114. }
    115. p.sendPluginMessage(this, "BungeeCord", b.toByteArray());
    116. }
    117. }


    Thanks In advance
     
  2. Offline

    ZodiacTheories

    Buizelfan2

    You are getting an NPE because, look at line 52, you are getting a player, no specific player, so the player is null. So when you do Location player = p.getLocation() - p doesn't have a location
     
  3. Offline

    Zupsub

    What's the implementation of getName()?
    Your NPE assumes, that p is null => Bukkit.getPlayer(getName()) returns null.

    oO?
    The location isn't null. The p is null.
    If location would be null, you wouldn't get a NPE there, you'd get one when you call any method of that location object.
     
  4. Offline

    ZodiacTheories

    Zupsub

    That is what I meant :p
     
  5. Offline

    Buizelfan2

    So what are you suggesting i do?
     
  6. Offline

    ZodiacTheories

    Buizelfan2

    What do you want your player to be? A command sender? A player that fires an event?
     
  7. Offline

    Buizelfan2

    When the villager spawns it looks to the left and I want it to either look at where the player is or at the spawn location
     
  8. Offline

    ZodiacTheories

    Buizelfan2

    How are you planning to use the method spawnVillager() ?
     
  9. Offline

    LordVakar

    public void spawnVillager(Player player)
    {
    //Change this and the above params
    Player p = player;
    }

    To spawn:
    spawnVillager(player);
     
  10. Offline

    Buizelfan2

    I put it in the onEnable I mean everything works it just turns out like this
    http://imgur.com/myKEURt
     
  11. Offline

    ZodiacTheories

    Buizelfan2

    Remove your Player p line, and in your spawnVillager() line add spawnVillager(Player p).

    EDIT:

    I Didn't see LordVakar :p
     
  12. Offline

    LordVakar

    Buizelfan2
    Why would you put it on the onEnable?
    When the server enables, there is no player on...
    EDIT: Why do you even need to get the player, you don't utilize it
    EDIT 2: You only use it to look at the player hm
     
Thread Status:
Not open for further replies.

Share This Page