Jump to content

NullPointerException in HashMap for data that should already be added


Recommended Posts

Posted

I really don't think HashMap wants to work for me.

 

public static Map<Object, Integer[]> atomicMass = new HashMap<Object, Integer[]>();

public static void registerItem(Item item, int protons, int neutrons, int electrons)
{
	//registerItem(item, new AtomicMass(protons, neutrons, electrons));
	Integer[] i = new Integer[5];
	i[1] = protons;
	i[2] = neutrons;
	i[3] = electrons;
	i[4] = protons + neutrons;

	atomicMass.put(item, i);
}

This is the code I am currently using. I create a HashMap/Map to store the item Object and the proton/neutron/electron/mass Integer[] values. I use the method also there to register items into this HashMap at PreInit. I also use a similar method for Blocks and ItemStacks, the only difference being the Item changes.

 

@SubscribeEvent
public void tooltip(ItemTooltipEvent event)
{
	Integer[] mval = AtomicRegister.getStuffies(event.itemStack);

	if(mval != null)
	{
		event.toolTip.add("Atomic Mass: " + mval[4]);
		event.toolTip.add("Protons: " + mval[1]);
		event.toolTip.add("Neutrons: " + mval[2]);
		event.toolTip.add("Electrons: " + mval[3]);
	}
}

I then use an event to try to post these values to a tooltip. However, the NullPointerException occurs on the first tooltip adding line "event.toolTip.add("Atomic Mass: " + mval[4]);" I added the !=null check to stop this, but now the tooltip doesn't show up even on the Items/Blocks I had registered.

Posted

Hi

 

There are a couple of concepts I think you're missing that might help.

The first is that you're storing information about Items, not about ItemStacks. 

So for example there is only one Item for "Carbon", and it has six protons.

There might be many ItemStacks of LumpOfCarbon lying around, and they are all Carbon, but in different amounts, one lump might be 6 moles, the other lump 12 moles.  They are not the same.

 

So if you want a HashMap to tell you how many protons are in a Carbon, you need a HashMap for Carbon, not for LumpOfCarbon.  You could in principle set up a HashMap for LumpsOfCarbon which ignores the number of moles in the lump (using hashCode and equals, like DieSieben said) but that really doesn't make much sense in this case.

 

A second thing is that, if only your new items have atomic masses, why do you need the HashMap registry at all?  Why not store the information in the (eg) ItemElement class directly and override Item.addInformation(..) instead?

 

-TGG

 

 

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.