HashMap作为java中的静态成员

发布于 2024-10-14 07:59:56 字数 468 浏览 5 评论 0原文

我想为新类的每个实例保留一个 HashMap 作为静态成员。然而,每次我尝试 .get 或 .put 到我的 HashMap 中时,我都会收到 NullPointerException。帮助!?

我正在做:public class EmailAccount { 私有静态 HashMapname_list; 然后 name_list.put(last_name,occurrences); 甚至 name_list.containsKey(last_name); 返回 NullPointer。

这来自于之前的一个问题:计数 Java 中字符串的出现次数

I want to carry a HashMap over as a static member for each instance of a new class. Every time I try to .get or.put into my HashMap, however, I get a NullPointerException. Help!?

I'm doing: public class EmailAccount {
private static HashMap<String,Integer> name_list;
and then name_list.put(last_name, occurences); Even name_list.containsKey(last_name); returns NullPointer.

This comes from an earlier question: Count occurrences of strings in Java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

向日葵 2024-10-21 07:59:56

您需要实例化它。

private static Map<String, Integer> name_list = new HashMap<String, Integer>();

另请参阅:


请注意,在 a 的变量名中使用“list”地图很混乱。您不希望它是 name_mapname_occurrences 吗?顺便说一下,这个下划线也不太符合 Java 命名约定,但除此之外。

You need to instantiate it.

private static Map<String, Integer> name_list = new HashMap<String, Integer>();

See also:


Note that using "list" in variable name of a map is confusing. Don't you want it to be a name_map or name_occurences? That underscore does by the way also not really fit in Java naming conventions, but that aside.

苍景流年 2024-10-21 07:59:56

您仍然需要初始化它,就像

private static HashMap<String, Integer> name_list = new HashMap<String, Integer>();

当您离开没有初始化的类级对象字段或任何对象引用时,它默认为 null。

虽然对您来说您想要一个 HashMap 似乎是显而易见的,所以它应该只是隐式初始化它,但 Java 不知道您实际上是否想要一个 HashMap,或者可能是一个 HashMap 子类,例如 LinkedHashMap

类级基元,如 int 可以像 private static int someNumber; 一样保留,并且不会通过访问它抛出 NullPointerException - 但那是因为基元不能为 null。 Java 将为它分配一些默认值(在 int 的情况下为 0)。

You still need to initialize it, like

private static HashMap<String, Integer> name_list = new HashMap<String, Integer>();

When you leave a class-level object field with no initialization -- or any object reference, for that matter, it defaults to null.

While it may seem obvious to you that you want a HashMap, so it should just implicitly initialize it, Java doesn't know if you want in fact a HashMap, or maybe a HashMap subclass, like LinkedHashMap

Class-level primitives, like int can be left just like private static int someNumber; and won't throw a NullPointerException by accessing it--but that's because primitives can't be null. Java will assign it some default value (in int's case, 0).

数理化全能战士 2024-10-21 07:59:56

您没有实例化该列表。您声明了它,但没有实例化。

You didn't instantiate the list. You declared it, but didn't instantiate.

最单纯的乌龟 2024-10-21 07:59:56

您创建了一个可以容纳 HashMap 的字段,但没有在其中放入任何内容。

您需要将一个 new HashMap() 放入您的字段中。

You created a field that can hold a HashMap, but you didn't put anything inside of it

You need to put a new HashMap<String, Integer>() into your field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文