Java Map.put()抛出空指针

时间:2016-04-13 15:30:47

标签: java dictionary nullpointerexception

这不是重复的。从表面上看,它看起来很像,但其他问题的解决方案并不适用。

当我在Java 1.7下运行以下方法时,我在

处得到一个空指针异常

normalOutCurrVals.put(new Double(x), new Byte(i));

我已经阅读了相关问题的文档和答案,但到目前为止,我和我的团队都很难过。是否有一些我们遗失的明显事物?

我们只希望将双值(显示在我们的程序UI上)的映射放到它们的字节对应物上,以存储在模拟设备的内存中。

以下是重要声明:

public final Map<Double, Byte>   normalOutCurrVals =   mapNormalOutCurrVals();
public final Map<Double, Byte>   magnetOutCurrVals = mapMagnetOutCurrVals();
public final Map<Integer, Byte>  freqVals          = mapFreqVals();
public final Map<String, Byte>   onTimeVals        = mapOnTimeVals();
public final Map<String, Short>  offTimeVals       = mapOffTimeVals();
public final Map<String , Short> pulseWidthVals    = mapPulseWidthVals();

非常感谢任何帮助。

private Map<Double, Byte> mapNormalOutCurrVals(){

    Map<Double, Byte> map = new HashMap<Double, Byte>();
    double x = 0.000;
    byte i = 0;

    //first set of normal output currents
    while (x <= 2.000){
        normalOutCurrVals.put(new Double(x), new Byte(i));
        x += 0.125;
        i++;
    }

    while (x <= 4.000){
        normalOutCurrVals.put(new Double(x), new Byte(i));
        x += 0.250;
        i++;
    }

    while (x <= 8.000){
        normalOutCurrVals.put(new Double(x), new Byte(i));
        x += 0.500;
        i++;
    }

    return map;
}

1 个答案:

答案 0 :(得分:1)

我想你想填满你的地图!

private Map<Double, Byte> mapNormalOutCurrVals(){

Map map = new HashMap();
double x = 0.000;
byte i = 0;

//first set of normal output currents
while (x <= 2.000){
    map.put(new Double(x), new Byte(i));
    x += 0.125;
    i++;
}

while (x <= 4.000){
    map.put(new Double(x), new Byte(i));
    x += 0.250;
    i++;
}

while (x <= 8.000){
    map.put(new Double(x), new Byte(i));
    x += 0.500;
    i++;
}

return map;
}