0

我得到一个NullPointerException并且无法弄清楚为什么。我在这里阅读了很多问题,似乎无法很好地剖析任何内容以获得我需要的答案。我正在使用 aHashMap并且我正在尝试从不同的模块中引用它。

这是代码:

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

4 回答 4

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);
}
于 2012-07-09T20:34:21.757 回答
2

这个块是一个实例初始化器:

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

(双重{{放弃它)。您正在尝试hm在其自身初始化期间访问静态字段。

如果您删除该hm.位,它应该可以工作,然后做put("ONE", 1);等等。

于 2012-07-09T20:34:46.250 回答
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);
}
于 2012-07-09T20:39:29.920 回答
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
);
于 2012-07-09T20:52:20.663 回答