Jump to content

Recommended Posts

Posted

That's rather convenient,

 

int length = stack.getTagCompound().getInteger("Length");

 

However, yields a null pointer exception.

 

I assume I would need to set the integer first, but where would I do that?

Posted

ItemStack can't be extended, it is final. However, getting an integer shouldn't throw NPE as it will always either return a value if it has one or 0 if it does not. Look at the getInteger(String) method in NBTTagCompound and you'll see that it always returns something. The NPE is coming from one of two things (most likely the second) - The stack is null or it's tag compound is null. ItemStack tag compounds are protected against NPE, so first do this:

 

NBTTagCompound nbt = stack.getTagCompound();
	if(nbt == null)
	{
		nbt = new NBTTagCompound();
		stack.setTagCompound(nbt);
	}

 

A specific example of what you're trying to do with the NBT would be helpful, that way I can tell you where to set the int value for retrieval later.

Posted
A specific example of what you're trying to do with the NBT would be helpful, that way I can tell you where to set the int value for retrieval later.

 

Not needed, your example works perfectly, thank you.

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.