Jump to content

Crash + unable to add names for a metadata block


SamTebbs33

Recommended Posts

I've made a block which has many versions of itself using metadata.

 

I used a for loop in my main mod class to add metadata-specific names to each block version, but I'm getting a null pointer error, for no apparent reason (There's obviously a reason :P)

 

Adding names (the first for loop works but isn't for the relevant block, the second for loop is the one that doesn't work):

 

for (int k = 0; k < Variables.getLastInt(ItemAnt.antMeta) + Variables.getLastInt(ItemAnt.typeMeta); k++){

		ItemStack ant = new ItemStack(itemAnt, 1, k);

		String name = ItemAnt.names[k];

		LanguageRegistry.addName(ant, name);

	}

	for (int i = 0; i < Variables.getLastInt(BlockAntHill.hillMeta); i++){

		ItemStack hill = new ItemStack(blockAntHill, 1, i);

		String name2 = BlockAntHill.hillNames[i];

		/* Line 168 --->*/ LanguageRegistry.addName(hill, BlockAntHill.hillNames[i]);

	}

 

The getLastInt method:

public static int getLastInt(int[] array){

	int a;

	a = array[array.length - 1];

	return a;

}

 

The hillMeta variable in BlockAntHill:

 

public static final int[] hillMeta = {0, 1, 2, 3, 4, 5, 6};

 

The hillNames variable in BlockAntHill:

private static final String NAME = "Ant Hill";

public static final String[] hillNames = {"Forest "+NAME, "Hillside "+NAME, "Desert "+NAME, "Swamp "+NAME, 
	"Plains "+NAME, "Jungle "+NAME, "Snowy "+NAME};

 

The blockAntHill object:

public static Block blockAntHill;
blockAntHill = new BlockAntHill(blockAntHillID);

 

The error:

---- Minecraft Crash Report ----
// Daisy, daisy...

Time: 6/10/13 10:09 PM
Description: Failed to start game

java.lang.NullPointerException
at cpw.mods.fml.common.registry.LanguageRegistry.addNameForObject(LanguageRegistry.java:110)
at cpw.mods.fml.common.registry.LanguageRegistry.addName(LanguageRegistry.java:120)
at vivadaylight3.myrmecology.common.Myrmecology.mainInit(Myrmecology.java:168)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:494)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:74)
at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:45)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:314)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:296)
at com.google.common.eventbus.EventBus.post(EventBus.java:267)
at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:165)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:74)
at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:45)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:314)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:296)
at com.google.common.eventbus.EventBus.post(EventBus.java:267)
at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:98)
at cpw.mods.fml.common.Loader.initializeMods(Loader.java:696)
at cpw.mods.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:213)
at net.minecraft.client.Minecraft.startGame(Minecraft.java:448)
at net.minecraft.client.MinecraftAppletImpl.startGame(MinecraftAppletImpl.java:44)
at net.minecraft.client.Minecraft.run(Minecraft.java:733)
at java.lang.Thread.run(Thread.java:680)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- System Details --
Details:
Minecraft Version: 1.5.2
Operating System: Mac OS X (x86_64) version 10.8.3
Java Version: 1.6.0_45, Apple Inc.
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Apple Inc.
Memory: 36162168 bytes (34 MB) / 85000192 bytes (81 MB) up to 2138767360 bytes (2039 MB)
JVM Flags: 1 total; -Xmx2g
AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
Suspicious classes: FML and Forge are installed
IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
FML: MCP v7.51 FML v5.2.17.716 Minecraft Forge 7.8.0.716 4 mods loaded, 4 mods active
mcp{7.44} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized
FML{5.2.17.716} [Forge Mod Loader] (coremods) Unloaded->Constructed->Pre-initialized->Initialized
Forge{7.8.0.716} [Minecraft Forge] (coremods) Unloaded->Constructed->Pre-initialized->Initialized
Myrmecology{0.0.1} [Myrmecology] (bin) Unloaded->Constructed->Pre-initialized->Errored
LWJGL: 2.4.2
OpenGL: Intel HD Graphics 3000 OpenGL Engine GL version 2.1 INTEL-8.10.44, Intel Inc.
Is Modded: Definitely; Client brand changed to 'fml,forge'
Type: Client (map_client.txt)
Texture Pack: Default
Profiler Position: N/A (disabled)
Vec3 Pool Size: ~~ERROR~~ NullPointerException: null

 

I appreciate any help, thanks in advance!

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

Why... do you... never mind, I don't even want to ask.

 

You probably want to use i/k < myList.length if you want to register the name for all of the items. As it is you're leaving out the last one.

Your problem could be caused by a failure to register your block. Are you registering your block?

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

What is it you don't want to ask me about? I would like to optimise my code if that's the thing.

 

I solved it by registering the block prior to adding then name, that was the very obvious problem :P (noob moment)

 

Thanks for the help!

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

The reason why I have the getLastInt() method is because I often forget how to do several things in Java, so I make methods so that I don't forget :)

 

If you mean native 'speaking' language, I was born in England and it's English.

 

If you mean native programming language, I come from a PHP, Css and HTML background.

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

The reason why I have the getLastInt() method is because I often forget how to do several things in Java, so I make methods so that I don't forget :)

 

If you mean native 'speaking' language, I was born in England and it's English.

 

If you mean native programming language, I come from a PHP, Css and HTML background.

I did mean programming... I'm a Python person myself, and I hate Java with all my heart. But I mod MC anyway, because I like modding and there's little I can do about the language.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

I like Java, but prefer PHP for its simplicity.

 

For some reason I can't register names for each metadata variant for my antHill block.

 

Each metadata variant appears in the creative menu but has the name "Snow Ant Hill" which is the last value of the hillNames array. I haven't been able to figure out why  :-\  ???

 

I event tried adding the names manually with several LanguageRegistry.addName(block variable, metadata name) but I got the same result.

 

Adding names in main class:

blockAntHill = new BlockAntHill(blockAntHillID);

for (int k = 0; k < Variables.getLastInt(BlockAntHill.hillMeta); k++){

		ItemStack ant = new ItemStack(blockAntHill, 1, k);

		LanguageRegistry.addName(ant, BlockAntHill.hillNames[k]);

	}

 

The BlockAntHill class:

package vivadaylight3.myrmecology.common.block;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import vivadaylight3.myrmecology.common.Myrmecology;
import vivadaylight3.myrmecology.common.item.ItemAnt;
import vivadaylight3.myrmecology.common.item.ItemExtractor;
import vivadaylight3.myrmecology.common.lib.Variables;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import net.minecraft.world.World;

public class BlockAntHill extends Block
{

public static final int[] hillMeta = {0, 1, 2, 3, 4, 5, 6};

private static final String NAME = "Ant Hill";

public static final String[] hillNames = {"Forest "+NAME, "Hillside "+NAME, "Desert "+NAME, "Swamp "+NAME, 
	"Plains "+NAME, "Jungle "+NAME, "Snowy "+NAME};

private static Icon[] icons;

private final String[] iconNames = {Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME, 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Forest", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Hills", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Desert",
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Swamp", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Plains", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Jungle", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Snow"};

public BlockAntHill(int par1)
{
	super(par1, Material.ground);
	setUnlocalizedName("antHill");
	setCreativeTab(Myrmecology.tabMyrmecology);
	setStepSound(Block.soundGrassFootstep);
	setHardness(1.0F);
	setResistance(1.0F);
}

@Override
public void registerIcons(IconRegister iconRegister){

	icons = Variables.iconsToArray(iconRegister, iconNames);

}

@Override
public Icon getIcon(int side, int metadata){

	return icons[metadata];

}

public String getUnlocalizedName(ItemStack itemStack)
    {

    	return this.getUnlocalizedName() + hillNames[itemStack.getItemDamage()];
    
    }

@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    {

	for (int k = 0; k < hillMeta.length + 1; k++){

		par3List.add(new ItemStack(this, 1, k));

	}
    }

@Override
public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9){

	return true;

}

public void harvestBlock(World world, EntityPlayer entity, int x, int y, int z){

	if(!world.isRemote){

		ItemStack tool = entity.getCurrentEquippedItem();
		ItemStack extractor = new ItemStack(Myrmecology.itemExtractor);

		if(tool != null && tool == extractor){

			int meta = world.getBlockMetadata(x, y, z);

			for(int k = 0; k < hillMeta.length * 5 + 1; k++){

				if(meta == k){

					ItemStack drone = new ItemStack(Myrmecology.itemAnt, k + ItemAnt.typeMeta[1], 1);
					ItemStack queen = new ItemStack(Myrmecology.itemAnt, k + ItemAnt.typeMeta[0], 1);
					entity.dropPlayerItem(drone);
					entity.dropPlayerItem(queen);

				}

			}

		}
	}

}

}

 

Does anyone have any idea why? :/

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.