简体   繁体   English

数组列表 <Integer> 抛出NullPointerException

[英]ArrayList <Integer> throws NullPointerException

I have a method that returns an ArrayList <Integer> . 我有一个返回ArrayList <Integer> When i return it and if its null i get a nullPointerException . 当我返回它,并且如果它为null,我得到一个nullPointerException Now i want to have some sort of validation and prevent the null pointer exception hapenning. 现在,我想进行某种验证,并防止空指针异常发生。

These are what i tried, but i still get the nullpointerexception 这些是我尝试过的,但是我仍然得到nullpointerexception

Map<Integer,ArrayList<Integer>> personMap = new HashMap<Integer,ArrayList<Integer>>();

public ArrayList<Integer> method(int departmentID){
  if (personMap.get(new Integer(departmentID)) !=null)
     return personMap.get(new Integer(departmentID));
  else 
     return null;


}

I still get the nullPointerException . 我仍然得到nullPointerException

approach 2. 方法2。

public ArrayList<Integer> method(int departmentID){
  if (personMap.get(new Integer(departmentID)).isEmpty()) return null;
  else return personMap.get(new Integer(departmentID));


}

I still get the nullpointerexception . 我仍然得到nullpointerexception Can someone help me solve this ? 有人可以帮我解决这个问题吗?

From a Testing class i will call the method as follows; 从测试类,我将按以下方式调用该方法;

Class1 cl = new Class1();

if (c1.method(112121)) // do something

First of all, your Map declaration won't compile: - 首先,您的Map声明将不会编译:-

Map<Integer,ArrayList<Integer>> personMap = new ArrayList<Integer,ArrayList<Integer>>();

You should have new HashMap<Integer,ArrayList<Integer>>(); 您应该具有new HashMap<Integer,ArrayList<Integer>>(); on the RHS. 在RHS上。

Secondly, your if statement, will again not compile: - 其次,您的if语句将再次无法编译:-

if (c1.method(112121))

Your method() returns an ArrayList , whereas in Java , you can only have boolean value as a condition in if . 您的method()返回一个ArrayList ,而在Javaif只能包含boolean值作为条件。


Modification Needed: - 需要修改:-

You should change your if to: - 您应该将if更改为:-

if (c1.method(112121) != null)

and use first method. 并使用第一种方法。

In fact, you don't need an if-else there. 实际上,您在那里不需要if-else You can simply return the value from the map: - 您可以简单地从地图中返回值:-

public ArrayList<Integer> method(int departmentID){ 
     return personMap.get(new Integer(departmentID));
}

Now, this will return null , if the key is not in the HashMap , else it will return the corresponding ArrayList . 现在,如果键不在HashMap ,则此方法将返回null ,否则它将返回相应的ArrayList

If a map doesn't have an entry for a key, get() returns null . 如果映射没有键的条目,则get()返回null
If you then call .isEmpty() on that you get an NPE. 如果然后调用.isEmpty() ,则会得到一个NPE。

Instead test for map.get(key) == null or better, !map.contains(key) 而是测试map.get(key) == null或更好, !map.contains(key)

Your first approach is the same as: 您的第一种方法与:

public ArrayList<Integer> method(int departmentID){
    return personMap.get(new Integer(departmentID));
}

and the second one is clearly able to throw a NPE: 第二个显然可以抛出NPE:

public ArrayList<Integer> method(int departmentID){
  if (personMap.get(new Integer(departmentID)).isEmpty()) return null;
  else return personMap.get(new Integer(departmentID));
}

If personMap.get(new Integer(departmentID)) returns null (which it does, when the map has no entry for this key) then calling a method on that throws an NPE: 如果personMap.get(new Integer(departmentID))返回null(如果地图没有为此键输入条目,则返回null),然后调用会抛出NPE的方法:

if (personMap.get(new Integer(departmentID)).isEmpty())

First of all I would NOT recommend returning null from a method. 首先,我不建议从方法中返回null。 You should return empty ArrayList. 您应该返回空的ArrayList。

Map<Integer,ArrayList<Integer>> personMap = new HashMap<Integer, ArrayList<Integer>> ();

public ArrayList<Integer> someMethod(int departmentID){

    if (personMap.get(departmentID) !=null)
        return personMap.get(departmentID);
    else
        return new ArrayList<Integer>();

}

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

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