简体   繁体   English

我如何检查空指针异常

[英]how do i check for null pointer exception

Here is the code 这是代码

private static HashMap naturalNumbers = new HashMap();

static
{
    naturalNumbers.put("zero", new Integer( 0 ) );
    naturalNumbers.put("one", new Integer( 1 ) );
    naturalNumbers.put("two", new Integer( 2 ) );
    naturalNumbers.put("three", new Integer( 3 ) );
}

private static int findANumber( String partOfaNumber ) throws Exception
{
int multiplicand = 0;  
multiplicand += (Integer)naturalNumbers.get( partOfaNumber );

If the "get" returns null, how do I check for this? 如果“ get”返回null,该如何检查?

I have tried: 我努力了:

if ( (Integer)naturalNumbers == null )
    {
        throw new Exception( "Number not found" );
    }
 return multiplicand;
}

but the IDE does not even accept it: cannot convert from HashMap to Integer. 但是IDE甚至不接受它:无法从HashMap转换为Integer。

Here's a slightly different version: 这是一个略有不同的版本:

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

static
{
    NUMS.put("zero", 0);
    NUMS.put("one", 1);
    NUMS.put("two", 2);
    NUMS.put("three", 3);
}

private static int findANumber(final String partOfaNumber) throws IllegalArgumentException
{
    int multiplicand = 0;  
    final Integer theNum = NUM.get(partOfaNumber);
    if (theNum != null) {
        multiplicand += theNum;
    } else {
        throw new IllegalArgumentException("Number not found (" + partOfNumber + ")");
    }

    return multiplicand;
}

This is simple. 这很简单。

Integer number = naturalNumbers.get( partOfaNumber )
if(number ==null) {
    throw new Exception("Number not found");
} else {
    multiplicand += number;
}
return multiplicand;

You can use shorthand as follows - 您可以按以下方式使用速记-

multiplicand += naturalNumbers.get(partOfaNumber ) == null ? 被乘数+ = naturalNumbers.get(partOfaNumber)== null吗? 0 : (Integer) naturalNumbers.get(partOfaNumber ); 0:(整数)naturalNumbers.get(partOfaNumber);

private static int findANumber( String partOfaNumber ) throws Exception
{
  int multiplicand = 0;  
  if (naturalNumbers.containsKey( partOfaNumber ); {
    multiplicand += (Integer)naturalNumbers.get( partOfaNumber );
  } else {
    throw new Exception("Number not found");
  }
  return multiplicand;
}

Just check with the key if it is exist inside Hashmap. 只需检查密钥是否存在于Hashmap中。 You dont have to worry about Null-Pointer Exception. 您不必担心Null-Pointer异常。

if(naturalNumbers.containsKey( partOfaNumber )){
  //Do your stuff
}else{
  //Do some stuff what you will when exception throw.
}

You can also search by Value by using 您也可以使用

naturalNumbers.containsValue( value)

You will always have to add check for null values from collections for primitive types because of auto-boxing mechanism on java 由于Java的自动装箱机制,您将始终必须为原始类型的集合添加对空值的检查。

A simple null pointer can be generated like below 可以生成一个简单的空指针,如下所示

Integer i = null;
i=i+10;

So always add default check for null and in your case you can add it like below 因此,请始终添加默认检查是否为null的情况,您可以按照以下方式添加它

Integer multiplicand = 0;
Integer value = get(key);

if(value!= null)
{
//add value to multiplicand
}

First, it would be better to use a specific HashMap 首先,最好使用特定的HashMap

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

which will get rid of the nasty casting. 这将摆脱讨厌的演员。

As for your problem, you try to cast the hash map to integer, which is not what you want. 对于您的问题,您尝试将哈希映射转换为整数,这不是您想要的。 You want to check if a particular value exist. 您要检查是否存在特定值。 The better way is to extract the value and check for null: 更好的方法是提取值并检查null:

int multiplicand = 0;
Integer part = naturalNumbers.get(partOfNumber)

if(part == null) {
   // Handle the null case
}

multiplicand += part;

Now, the question is, what are you going to do in the case the value doesn't exist. 现在的问题是,如果该值不存在,您将如何处理。 If you define the not existing value a zero, you can do do something like this 如果将不存在的值定义为零,则可以执行以下操作

multiplicand += (naturalNumbers.contains(partOfNumber)?naturalNumbers.get(partOfNumber):0;

This makes it more concise. 这使其更加简洁。 However, that depends on what you want to do if the value is not contained in the HashMap. 但是,这取决于您要在HashMap中不包含该值的情况下要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM