尝试设置项目纹理时出现 NullPointerException - 我的世界 1.8 改装

NullPointerException when trying to set item texture - minecraft 1.8 modding

我正在学习如何 java 并尝试构建我的世界 mod。

问题是我在使用 1.8 项目纹理添加系统时遇到了问题,尽管 Google 是我最好的朋友,但我仍然没有找到解决此 NPE 的方法。

这是基地 Mod class:

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)
    {
    }
}

这是项目 class:

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 行在代码 Mod class 中用 "line 41->" 标记在代码之前。我也抄录在这里:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekai:" + ((testItem) testIngot).getName(), "inventory"));

我认为您的问题是 ModelResourceLocation 实例化中的 MODNAME 和 MODID 之间的差异。第一个参数在“:”之前需要 MODID,您使用的是 MODNAME。尝试在您的基础中更改此行 mod class:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekai:" + ((testItem) testIngot).getName(), "inventory"));

为此:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekairocha:" + ((testItem) testIngot).getName(), "inventory"));

甚至更好:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation(sekai.MODID + ":" + ((testItem) testIngot).getName(), "inventory"));

除了该修复之外,我认为您的项目 class 可能需要一些改动。将您的构造函数更改为:

public testItem(){
    super(); //missing initializations from Item class
    maxStackSize = 64;
    this.setCreativeTab(CreativeTabs.tabMisc);
    this.setUnlocalizedName("testIngot");
    GameRegistry.registerItem(this, name); //do this last
}

http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/modification-development/2272349-lessons-from-my-first-mc-1-8-mod

这家伙解释说你必须在 init 方法中注册项目渲染。我做到了并且成功了。

我所要做的就是将渲染部分移至 init 方法。结果在base里是这样的 Mod class:

public void load(FMLInitializationEvent event){
        if(event.getSide() == Side.CLIENT)
        {
            RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
            renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation(sekai.MODID + ":" + ((testItem) testIngot).getName(), "inventory"));
        }
    }

谢谢你们。