-2
我在Minecraft MCP Forge 1.6中製作了一個我的世界MOD(不知道是否將它放入遊戲部分),我在加載遊戲時獲得NullPointerException
。NullPointerException當添加農作物時,Minecraft MCP Forge 1.6?
下面是代碼:
主營:http://pastebin.com/w8i0p46d
package net.minecraft.MFM;
import net.minecraft.MSM.eventmanager;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemSeedFood;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@Mod(modid = morefoodmod.modid, name = "More Food Mod", version = "0.1")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class morefoodmod {
public static final String modid = "morefoodmod";
@Instance("morefoodmod")
public static morefoodmod instance = new morefoodmod();
// public static CommonProxy proxy;
@Init
public void initMain(FMLInitializationEvent e) {
// NetworkRegistry.instance().registerGuiHandler(this, new guiHandler());
}
public static CreativeTabs mfmTab;
public static Block BlockStrawberry;
public static Item ItemHealingApple;
public static Item ItemRegenApple;
public static Item ItemStrawberry;
eventmanager eventmanager = new eventmanager();
@EventHandler
public void load(FMLInitializationEvent e) {
mfmTab = new CreativeTabs("mfmTab") {
@SideOnly(Side.CLIENT)
public Item getTabItemIconIndex() {
return Item.fishCooked;
}
};
LanguageRegistry.instance().addStringLocalization(mfmTab.getTranslatedTabLabel(), "More Food Mod");
ItemHealingApple = new HealingApple(5371, 4, 2F, false).setPotionEffect(Potion.regeneration.id, 1, 255, 1.0F).setUnlocalizedName("itemHealingApple");
ItemRegenApple = new RegenApple(8029, 4, 2F, false).setPotionEffect(Potion.regeneration.id, 300, 1, 1F).setUnlocalizedName("itemRegenApple");
// Exception thrown on the following statement.
ItemStrawberry = new ItemSeedFood(7231, 3, 0.5F, BlockStrawberry.blockID, Block.tilledField.blockID).setUnlocalizedName("itemStrawberry").setTextureName(modid + ":" + "itemStrawberry").setCreativeTab(CreativeTabs.tabFood);
BlockStrawberry = new Strawberry(1018).setUnlocalizedName("blockStrawberry");
GameRegistry.addRecipe(new ItemStack(ItemHealingApple, 1), new Object[]{"GPG", "DAD", "GPG", 'G', Item.ingotGold, 'D', Item.diamond, 'A', Item.appleRed, 'P', new ItemStack(Item.potion, 1, 8261)});
GameRegistry.addRecipe(new ItemStack(ItemRegenApple, 1),new Object[]{"GPG", "DAD", "GPG", 'G', Item.ingotGold, 'D', Item.diamond, 'A', Item.appleRed, 'P', new ItemStack(Item.potion, 1, 8193)});
registerBlock(BlockStrawberry, "StrawberryGrow");
registerItem(ItemStrawberry, "Strawberry");
registerItem(ItemHealingApple, "Healing Apple");
registerItem(ItemRegenApple ,"Regeneration Apple");
}
public void registerItem(Item item, String name){
GameRegistry.registerItem(item, item.getUnlocalizedName());
LanguageRegistry.addName(item, name);
}
public void registerBlock(Block block, String name){
GameRegistry.registerBlock(block, block.getUnlocalizedName());
LanguageRegistry.addName(block, name);
}
}
草莓:http://pastebin.com/7qC4DkNt
package net.minecraft.MFM;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCrops;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.util.Icon;
public class Strawberry extends BlockCrops {
@SideOnly(Side.CLIENT)
private Icon[] iconArray;
protected Strawberry(int par1) {
super(par1);
}
@SideOnly(Side.CLIENT)
public Icon getIcon(int side, int metadata) {
if(metadata > 7) {
if(metadata == 6)
metadata = 5;
return iconArray[metadata >> 1];
}
return iconArray[3];
}
public int getSeedItem() {
return morefoodmod.ItemStrawberry.itemID;
}
public int getCropItem() {
return morefoodmod.ItemStrawberry.itemID;
}
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister iconRegister) {
this.iconArray = new Icon[4];
for(int i = 0; i < this.iconArray.length; i++)
this.iconArray[i] = iconRegister.registerIcon(morefoodmod.modid + ":" + "itemStrawberry_" + (i+1));
}
}
http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – Mat
不要嘗試訪問空對象。您需要找到哪個對象爲null,然後確保它在該點之前的某個位置被初始化。 – csmckelvey