Solved NullPointerException

Discussion in 'Plugin Development' started by Quaro, Oct 6, 2013.

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

    Quaro

    Code:java
    1. at test.Main.onEnable(Main.java:38)
    2. at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    3. at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    4. .java:457)
    5. at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    6. r.java:381)
    7. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav
    8. a:282)
    9. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.
    10. java:264)
    11. at net.minecraft.server.v1_6_R3.MinecraftServer.l(MinecraftServer.java:3
    12. 15)
    13. at net.minecraft.server.v1_6_R3.MinecraftServer.f(MinecraftServer.java:2
    14. 92)
    15. at net.minecraft.server.v1_6_R3.MinecraftServer.a(MinecraftServer.java:2
    16. 52)
    17. at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.jav
    18. a:152)
    19. at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    20. :393)
    21. at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    22. 83)


    Code:java
    1.  
    2.  
    3. import com.comphenix.protocol.ProtocolLibrary;
    4. import com.comphenix.protocol.events.ConnectionSide;
    5. import com.comphenix.protocol.events.ListenerPriority;
    6. import com.comphenix.protocol.events.PacketAdapter;
    7. import com.comphenix.protocol.events.PacketContainer;
    8. import com.comphenix.protocol.events.PacketEvent;
    9.  
    10. import java.io.ByteArrayOutputStream;
    11. import java.io.DataOutputStream;
    12. import java.lang.reflect.Field;
    13. import java.util.HashSet;
    14. import java.util.Set;
    15. import java.util.logging.Level;
    16. import java.util.logging.Logger;
    17.  
    18. import net.minecraft.server.v1_6_R3.ContainerMerchant;
    19. import net.minecraft.server.v1_6_R3.EntityPlayer;
    20. import net.minecraft.server.v1_6_R3.IMerchant;
    21. import net.minecraft.server.v1_6_R3.MerchantRecipe;
    22. import net.minecraft.server.v1_6_R3.MerchantRecipeList;
    23. import net.minecraft.server.v1_6_R3.NBTTagCompound;
    24. import net.minecraft.server.v1_6_R3.NBTTagList;
    25.  
    26. import org.bukkit.craftbukkit.v1_6_R3.entity.CraftPlayer;
    27. import org.bukkit.craftbukkit.v1_6_R3.inventory.CraftItemStack;
    28. import org.bukkit.inventory.ItemStack;
    29. import org.bukkit.plugin.java.JavaPlugin;
    30.  
    31. public class Main extends JavaPlugin {
    32.  
    33. private Field cC;
    34.  
    35.  
    36. @Override
    37. public void onEnable() {
    38. try {
    39. cC = EntityPlayer.class.getDeclaredField("containerCounter");
    40. cC.setAccessible(true);
    41. }
    42. catch(Exception e) {
    43. this.setEnabled(false);
    44. }
    45. Set<Integer> packets = new HashSet<Integer>();
    46. packets.add(0x67);
    47. packets.add(0x68);
    48. packets.add(0x6B);
    49. packets.add(0xFA);
    50. ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, packets) {
    51. @SuppressWarnings("unchecked")
    52. @Override
    53. public void onPacketSending(PacketEvent event) {
    54. PacketContainer packet = event.getPacket();
    55. switch(packet.getID()){
    56. case 0x68:
    57. try {
    58. ItemStack[] read = packet.getItemArrayModifier().read(0);
    59. for(int i=0; i<read.length; i++) {
    60. read[i] = removeAttributes(read[i]);
    61. }
    62. packet.getItemArrayModifier().write(0, read);
    63. }
    64. catch(Exception e) {
    65. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
    66. }
    67. break;
    68. case 0xFA:
    69. if(!packet.getStrings().read(0).equalsIgnoreCase("MC|TrList")) {
    70. break;
    71. }
    72. try {
    73. EntityPlayer p = ((CraftPlayer)event.getPlayer()).getHandle();
    74. ContainerMerchant cM = ((ContainerMerchant) p.activeContainer);
    75. Field fieldMerchant = cM.getClass().getDeclaredField("merchant");
    76. fieldMerchant.setAccessible(true);
    77. IMerchant imerchant = (IMerchant)fieldMerchant.get(cM);
    78.  
    79. MerchantRecipeList merchantrecipelist = imerchant.getOffers(p);
    80. MerchantRecipeList nlist = new MerchantRecipeList();
    81. for(Object orecipe : merchantrecipelist) {
    82. MerchantRecipe recipe = (MerchantRecipe)orecipe;
    83. int uses = recipe.i().getInt("uses");
    84. int maxUses = recipe.i().getInt("maxUses");
    85. MerchantRecipe nrecipe = new MerchantRecipe(removeAttributes(recipe.getBuyItem1()), removeAttributes(recipe.getBuyItem2()), removeAttributes(recipe.getBuyItem3()));
    86. nrecipe.a(maxUses-7);
    87. for(int i=0; i < uses; i++) {
    88. nrecipe.f();
    89. }
    90. nlist.add(nrecipe);
    91. }
    92.  
    93. ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    94. DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
    95. dataoutputstream.writeInt(cC.getInt(p));
    96. nlist.a(dataoutputstream);
    97. byte[] b = bytearrayoutputstream.toByteArray();
    98. packet.getByteArrays().write(0, b);
    99. packet.getIntegers().write(0, b.length);
    100. }
    101. catch(Exception e){
    102. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
    103. }
    104. break;
    105. default:
    106. try {
    107. packet.getItemModifier().write(0, removeAttributes(packet.getItemModifier().read(0)));
    108. }
    109. catch(Exception e) {
    110. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
    111. }
    112.  
    113. }
    114. }
    115.  
    116. });
    117. }
    118.  
    119. public static ItemStack removeAttributes(ItemStack item){
    120. if(item == null) {
    121. return item;
    122. }
    123. net.minecraft.server.v1_6_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
    124. NBTTagCompound tag;
    125. if (!nmsStack.hasTag()){
    126. tag = new NBTTagCompound();
    127. nmsStack.setTag(tag);
    128. }
    129. else {
    130. tag = nmsStack.getTag();
    131. }
    132. NBTTagList am = new NBTTagList();
    133. tag.set("AttributeModifiers", am);
    134. nmsStack.setTag(tag);
    135. return CraftItemStack.asCraftMirror(nmsStack);
    136. }
    137.  
    138. public static net.minecraft.server.v1_6_R3.ItemStack removeAttributes(net.minecraft.server.v1_6_R3.ItemStack item){
    139. if(item == null) {
    140. return item;
    141. }
    142. NBTTagCompound tag;
    143. if (!item.hasTag()){
    144. tag = new NBTTagCompound();
    145. item.setTag(tag);
    146. }
    147. else {
    148. tag = item.getTag();
    149. }
    150. NBTTagList am = new NBTTagList();
    151. tag.set("AttributeModifiers", am);
    152. item.setTag(tag);
    153. return item;
    154. }
    155.  
    156. }[/i][/i]
     
  2. Whatever is on line 38 is null
     
  3. Offline

    Quaro

  4. Am i see it good, that you have a void inside of onEnable?
     
  5. Offline

    Goblom

    its supposed to be
    Code:java
    1. public void onEnable() { }


    Can you post the entire stack trace of the Exception

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  6. Offline

    Quaro

    1. @Goblom
    2. java.lang.NullPointerException
    3. at test.Main.onEnable(Main.java:38)
    4. at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    5. at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    6. .java:457)
    7. at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    8. r.java:381)
    9. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav
    10. a:282)
    11. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.
    12. java:264)
    13. at net.minecraft.server.v1_6_R3.MinecraftServer.l(MinecraftServer.java:3
    14. 15)
    15. at net.minecraft.server.v1_6_R3.MinecraftServer.f(MinecraftServer.java:2
    16. 92)
    17. at net.minecraft.server.v1_6_R3.MinecraftServer.a(MinecraftServer.java:2
    18. 52)
    19. at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.jav
    20. a:152)
    21. at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    22. :393)
    23. at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    24. 83)
     
  7. Offline

    xTrollxDudex

    How do you even get a NullPointerException on a try statement? :p

    Oh and btw, you didn't close the parentheses on line 50
     
  8. Offline

    Goblom

    Dis why i don't like Reflection -.-

    It is my understanding that you have to invoke to Field after making it accessible. Don't ask me how because i have no idea.

    Supposed to be like that. He is creating a packet listener..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  9. Offline

    xTrollxDudex

    Goblom
    I did not see that. I guess it's the minimal amount to of time I put to studying ProtocolLib
     
  10. Offline

    Goblom

  11. Offline

    Skyost

    Micius Please provide more informations, we are not your dog !
     
  12. Offline

    Quaro

    Skyost What information you need?
     
  13. Offline

    Skyost

    Micius Just explain your problem on your first thread like this :
    Code:
    What steps will reproduce the problem ?
    1.
    2.
    3.
     
    What is the expected output ? What do you see instead ?
     
    Do you have an error log of what happened ?
     
    Please provide any additional information below :
    
    (Thanks BukkitDev)
     
  14. Offline

    Quaro

    Error appears when i try to start server, with plugin installed, that's it.
     
  15. Offline

    Goblom

     
  16. I don't get why you take the source of my plugin and recompile it without making any actual changes other than renames of classes. Please use the jars provided on the BukkitDev page. If you happen to encounter a problem with one of the provided jars please create a ticket on the BukkitDev page, but not here.
     
  17. Offline

    Quaro

    mncat77
    I took it, because if you would stop developing it, and after update it would crash, i could update it myself.
     
  18. It's updated to 1.6.4, download here (there are no Bukkit builds for snapshots). I don't think I'll drop support anytime soon; also the source provided in the jar is the one I used to compile, so the error was caused by you somewhere. There also is no need for you to add the code to your *whatever* plugin, you can just use the plugin itself.
     
Thread Status:
Not open for further replies.

Share This Page