-1

I'm learning how to java and trying to build a minecraft mod.

The problem is I'm having trouble with the 1.8 item texture adding system and even though Google is my best friend I still haven't found what I should do to fix this NPE.

Here is the Base 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)
    {
    }
}

Here is the item 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;
    }
}

And here is the error report:

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)

line 41 is marked in the Base Mod class wiht "line 41->" right before the code. I'll also transcribe it here:

renderItem.getItemModelMesher().register(testIngot, 0, new ModelResourceLocation("sekai:" + ((testItem) testIngot).getName(), "inventory"));
rosaqq
  • 426
  • 4
  • 16
  • You may get better help going to a minecraft modding forum. In `sekai`, which line is line 41? – Captain Man May 06 '15 at 19:30
  • From your stack trace it looks like the null pointer exception at line 41 of sekai.java. From the included code snippets it's hard to tell what line 41 is. But, you can start by checking that any objects used are instantiated. For instance is the parameter "event" null? Or, is "renderItem" null? – danplubell May 06 '15 at 19:40
  • @danplubell marked line 41 sorry for forgetting to do that – rosaqq May 06 '15 at 19:49
  • Does this line return null: Minecraft.getMinecraft().getRenderItem()? If it doesn't then does this line return null: renderItem.getItemModelMesher() – danplubell May 06 '15 at 19:56
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Raedwald May 06 '15 at 20:35

2 Answers2

2

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

This guy explains you have to register the item renders in the init method. I did it and it worked.

All I had to do was move the render part to the init method. It turned out like this in the 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"));
        }
    }

Thank you guys anyway.

rosaqq
  • 426
  • 4
  • 16
0

I believe your issue is a difference between the MODNAME and MODID in the instantiation of the ModelResourceLocation. The first parameter requires the MODID before the ":", you're using the MODNAME. Try changing this line in your base mod class:

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

To this:

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

Or even better:

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

In addition to that fix, I think your item class may need some alterations. Change your constructor to this:

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