我正在学习如何使用 java 并尝试构建一个 minecraft mod。
问题是我在使用 1.8 项目纹理添加系统时遇到了问题,即使 Google 是我最好的朋友,我仍然没有找到我应该做些什么来修复这个 NPE。
这是基本 Mod 类:
package rocha.sekai;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraft.item.Item;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;
@Mod(modid=sekai.MODID, name=sekai.MODNAME, version=sekai.MODVER)
public class sekai{
//items variable
public static Item testIngot;
//Set the ID of the mod (Should be lower case).
public static final String MODID = "sekairocha";
//Set the "Name" of the mod.
public static final String MODNAME = "sekai";
//Set the version of the mod.
public static final String MODVER = "0.0.1";
@Instance(value = sekai.MODID) //Tell Forge what instance to use.
public static sekai instance;
@EventHandler
public void preInit(FMLPreInitializationEvent event){
testIngot = new testItem();
if(event.getSide() == Side.CLIENT){
RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
line 41-> renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekai:" + ((testItem) testIngot).getName(), "inventory"));
}
}
@EventHandler
public void load(FMLInitializationEvent event)
{
}
@EventHandler
public void postInit(FMLPostInitializationEvent event)
{
}
}
这是项目类:
package rocha.sekai;
import net.minecraft.item.Item;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraftforge.fml.common.registry.GameRegistry;
public class testItem extends Item{
private final String name = "testIngot";
public testItem(){
GameRegistry.registerItem(this, name);
maxStackSize = 64;
setCreativeTab(CreativeTabs.tabMisc);
setUnlocalizedName("testIngot");
}
public String getName(){
return name;
}
}
这是错误报告:
java.lang.NullPointerException: Initializing game
at rocha.sekai.sekai.preInit(sekai.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:536)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)
第 41 行在 Base Mod 类中用“第 41-> 行”标记在代码之前。我也会在这里转录:
renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekai:" + ((testItem) testIngot).getName(), "inventory"));