Jump to content

NullPointerException in HashMap for data that should already be added


Cephrus

Recommended Posts

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.

Link to comment
Share on other sites

Hi

 

The root cause of your problem is that you defined your HashMap with Object, so the compiler didn't warn you that you are mixing up Items with ItemStacks

Map<Object, Integer[]>;

should be better be

Map<Item, Integer []>

 

Do you know about the difference between Items and ItemStacks?

 

If not, this might help

http://greyminecraftcoder.blogspot.com.au/2013/12/items.html

 

-TGG

Link to comment
Share on other sites

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

 

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Just a few months ago I was creating my own plugin for Minecraft 1.20.2 spigot that did the same thing, but the skins were not saved, if you can understand this code that I made a long time ago it may help you.   //This is a class method private static String APIRequest(String value, String url, String toSearch) { try { URL api = new URL(url + value); HttpURLConnection connection = (HttpURLConnection) api.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); for (String responseChar; (responseChar = reader.readLine()) != null; ) response.append(responseChar); reader.close(); JSONObject responseObject = new JSONObject(response.toString()); if (!toSearch.equals("id")) return responseObject .getJSONArray("properties") .getJSONObject(0) .getString("value"); else return responseObject.getString("id"); } else { AntiGianka.ConsoleMessage(ChatColor.RED, String.format( "Could not get %s. Response code: %s", ((toSearch.equals("id")) ? "UUID" : "texture"), responseCode )); } } catch (MalformedURLException error) { AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while trying to access the URL. Error: " + error); } catch (IOException error) { AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while attempting to connect to the URL. Error: " + error); } return ""; } //other class method private void SkinGetter() { String uuid; String textureCoded; if ((uuid = APIRequest(args[0], "https://api.mojang.com/users/profiles/minecraft/", "id")).isEmpty() || (textureCoded = APIRequest(uuid, "https://sessionserver.mojang.com/session/minecraft/profile/", "value")).isEmpty() ) sender.sendMessage(ChatColor.RED + String.format( "An error has occurred while trying to obtain the %s player skin, please check if the name %s is spelled correctly or try again later.", args[0], args[0] ) ); else SkinSetter(textureCoded); } //other more private void SkinSetter(String textureCoded) { JSONObject profile = new JSONObject(new String(Base64.getDecoder().decode(textureCoded))); try { URL textureUrl = new URL(profile.getJSONObject("textures"). getJSONObject("SKIN"). getString("url")); if (sender instanceof Player && args.length == 1) { PlayerTextures playerTextures = ((Player) sender).getPlayerProfile().getTextures(); playerTextures.setSkin(textureUrl); ((Player) sender).getPlayerProfile().setTextures(playerTextures); if (((Player) sender).getPlayerProfile().getTextures().getSkin() != null) sender.sendMessage(((Player) sender).getPlayerProfile().getTextures().getSkin().toString()); else sender.sendMessage("Null"); sender.sendMessage("Skin changed successfully.a"); } else { } AntiGianka.ConsoleMessage(ChatColor.GREEN, "Skin command executed successfully."); } catch (MalformedURLException error) { sender.sendMessage(ChatColor.RED + String.format( "An error has occurred while trying to obtain the %s player skin, please check if the name %s is spelled correctly or try again later.", args[0], args[0] ) ); AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while trying to access the URL. Error: " + error); } }  
    • Use /locate structure The chat should show all available structures as suggestion For the Ancient City it is /locate structure ancient_city
    • So does it work without this mod? Did you test it with other builds?
    • Make a test without Sophisticated Backpacks or try other builds
  • Topics

×
×
  • Create New...

Important Information

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