Hashmap问题,得到异常我不明白

时间:2012-07-09 20:19:42

标签: java hashmap

我得到NullPointerException并且无法找出原因。我在这里已经阅读了很多问题,似乎无法解剖任何问题以获得我需要的答案。我正在使用HashMap,我试图从不同的模块中引用它。

以下是代码:

import java.util.*; 
@SuppressWarnings("unused")
public class Decoder 
{
    public static void main(String[] args)
    {
        decrypt();
        keywork(null);
    }

    static void decrypt()
    {
        String codedline = "ilyh wkrxvdqg, wzr kxqguhg dqg qlqhwb-wkuhh".toLowerCase();
        char[] cwarr = codedline.toCharArray();
        String dcline  = "";
        for(char x : cwarr)
        {
            if(Character.isLetter(x))
            {
                int c = (int)x;
                c = c - 3;
                if(c > 90 && c < 97)
                {
                    c += 26;
                }
                x = (char)c;
                dcline += x;
            } else
            {
                dcline += x;
            }
        }
        System.out.println(dcline);
    }

    static void keywork(String dcline)
    {
        int x;
        for(int i = 1, sw = 0; i == dcline.length(); i++)
        {
            String strchk = dcline.substring(sw, i);
            Object n = hm.get(strchk);
        };
    }


    static final HashMap<String, Integer> hm = new HashMap <String, Integer>()
    {
        /**
         * 
         */
        private static final long serialVersionUID = 2688387173090905196L;
        {
            hm.put("ONE", 1);
            hm.put("TWO", 2);
            hm.put("THREE", 3);
            hm.put("FOUR", 4);
            hm.put("FIVE", 5);
            hm.put("SIX", 6);
            hm.put("SEVEN", 7);
            hm.put("EIGHT", 8);
            hm.put("NINE", 9);
            hm.put("TEN", 10);
            hm.put("ELEVEN", 11);
            hm.put("TWELVE", 12);
            hm.put("THIRTEEN", 13);
            hm.put("FOURTEEN", 14);
            hm.put("FIFTEEN", 15);
            hm.put("SIXTEEN", 16);
            hm.put("SEVENTEEN", 17);
            hm.put("EIGHTEEN", 18);
            hm.put("NINETEEN", 19);
            hm.put("TWENTY", 20);
            hm.put("THIRTY", 30);
            hm.put("FOURTY", 40);
            hm.put("FIFTY", 50);
            hm.put("SIXTY", 60);
            hm.put("SEVENTY", 70);
            hm.put("EIGHTY", 80);
            hm.put("NINETY", 90);
            hm.put("HUNDRED", 100);
            hm.put("THOUSAND", 1000);
        }
    };
} 

这就是我得到的:

java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at Decoder$1.<init>(Decoder.java:56)
at Decoder.<clinit>(Decoder.java:49)
Exception in thread "main" 

4 个答案:

答案 0 :(得分:3)

只需删除静态初始化程序中的“hm”,hm对象尚不存在!它看起来像这样:

{
put("ONE", 1);
put("TWO", 2);
put("THREE", 3);
put("FOUR", 4);
put("FIVE", 5);
put("SIX", 6);
put("SEVEN", 7);
put("EIGHT", 8);
put("NINE", 9);
put("TEN", 10);
put("ELEVEN", 11);
put("TWELVE", 12);
put("THIRTEEN", 13);
put("FOURTEEN", 14);
put("FIFTEEN", 15);
put("SIXTEEN", 16);
put("SEVENTEEN", 17);
put("EIGHTEEN", 18);
put("NINETEEN", 19);
put("TWENTY", 20);
put("THIRTY", 30);
put("FOURTY", 40);
put("FIFTY", 50);
put("SIXTY", 60);
put("SEVENTY", 70);
put("EIGHTY", 80);
put("NINETY", 90);
put("HUNDRED", 100);
put("THOUSAND", 1000);
}

答案 1 :(得分:2)

此块是实例初始值设定项:

{
/**
     * 
     */
    private static final long serialVersionUID = 2688387173090905196L;
{
    hm.put("ONE", 1);

(双{{给它了)。您正在尝试在自己的初始化期间访问静态hm字段。

如果删除hm.位,只执行put("ONE", 1);等,它应该有用。

答案 2 :(得分:1)

我从来没有做过你想要做的事情,所以我不知道它是否会按照我认为的方式运作,但我可以确切地知道出了什么问题。

您在声明中使用了匿名子类语法(可能是偶然的):

public static Something object = new Something()
{
    // anonymous subclass declaration here
}

结果是,在hm的初始化期间,你引用它,因为你当前正在hm的构造函数中运行,它是未初始化的。我坦率地说不是甚至可以确定如何设法编译,但你应该能够使它与以下工作。

static final HashMap<String, Integer> hm;

static
{
    hm = new HashMap <String, Integer>()
    {
        private static final long serialVersionUID = 2688387173090905196L;
    }
    hm.put("ONE", 1);
    // snip
    hm.put("THOUSAND", 1000);
}

答案 3 :(得分:0)

如果你想要一些非常简洁和纯粹的功能,你应该添加以下代码:

public static <K, V> Map<K, V> hashMap(Object... data) {
  return fill(new HashMap<K, V>(), data);
}
public static <K, V> Map<K, V> fill(Map<K, V> map, Object... data) {
  if ((data.length & 1) != 0)
    throw new IllegalArgumentException("Odd number of key-value arguments");
  int i = 0;
  while (i < data.length) map.put((K) data[i++], (V) data[i++]);
  return map;
}

然后像这样进行初始化:

public static final Map<Integer, String> hm = hashMap(
  "ONE", 1,
  "TWO", 2,
  "THREE", 3,
  "FOUR", 4,
  "FIVE", 5,
  "SIX", 6,
  "SEVEN", 7,
  "EIGHT", 8,
  "NINE", 9,
  "TEN", 10,
  "ELEVEN", 11,
  "TWELVE", 12,
  "THIRTEEN", 13,
  "FOURTEEN", 14,
  "FIFTEEN", 15,
  "SIXTEEN", 16,
  "SEVENTEEN", 17,
  "EIGHTEEN", 18,
  "NINETEEN", 19,
  "TWENTY", 20,
  "THIRTY", 30,
  "FOURTY", 40,
  "FIFTY", 50,
  "SIXTY", 60,
  "SEVENTY", 70,
  "EIGHTY", 80,
  "NINETY", 90,
  "HUNDRED", 100,
  "THOUSAND", 1000
);