找到你要的答案

Q:Java NullPointerException and ArrayList

Q:java NullPointerException和ArrayList

Given the following:

import java.util.ArrayList;

public class IntegerList {

    public ArrayList<Integer> list;
    private int front;
    private int[] integers = getIntegers();

    public IntegerList (int maxCapacity) {
        list = new ArrayList<Integer>(maxCapacity);
        for (int i = 0; i < maxCapacity; i++) {
            list.add(null);
        }
        front = 0;
    }

    public int getCapacity() {
        if (list.get(front) == null) 
            return 0;
        else 
            return 10;
    }

    public int[] getIntegers() {
        int[] integers = new int[getCapacity()];
        return integers;
    }
}

When I instantiate the class IntegerList by passing 20 to the maxCapacity parameter in the constructor I get the following message at compile time:

Exception in thread "main" java.lang.NullPointerException
    at IntegerList.getCapacity(IntegerList.java:18)
    at IntegerList.getIntegers(IntegerList.java:26)
    at IntegerList.<init>(IntegerList.java:7)
    at Test.main(Test.java:5)

Why do I get the exception? It appears that the problem is in list.get(front) in getCapacity(), but does this not return null, thus returning true for the if statement?

给出以下:

import java.util.ArrayList;

public class IntegerList {

    public ArrayList<Integer> list;
    private int front;
    private int[] 整数= getintegers();

    public IntegerList (int maxCapacity) {
        list = new ArrayList<Integer>(maxCapacity);
        for (int i = 0; i < maxCapacity; i++) {
            list.add(null);
        }
        front = 0;
    }

    public int getCapacity() {
        if (list.get(front) == null) 
            return 0;
        else 
            return 10;
    }

    public int[] getIntegers() {
        int[] integers = new int[getCapacity()];
        return integers;
    }
}

当我实例化类integerlist通过20 MaxCapacity参数的构造函数中我在编译的时候得到以下信息:

Exception in thread "main" java.lang.NullPointerException
    at IntegerList.getCapacity(IntegerList.java:18)
    at IntegerList.getIntegers(IntegerList.java:26)
    at IntegerList.<init>(IntegerList.java:7)
    at Test.main(Test.java:5)

为什么我会有例外?看来,问题是在名单上。得到(前)在getcapacity(),但不会返回null,因此返回TRUE if语句?

answer1: 回答1:

Your initializer integers = getIntegers(); is implicitly inserted at the top of your constructor and runs before list = new ArrayList<Integer>(maxCapacity);.

Using anything other than an immediately evaluable expression in an initializer is frequently asking for trouble of this sort. It's better to do complex initialization like that in the constructor explicitly.

你的初始化整数= getintegers();隐式构造函数在你上方插入和运行在列表=新的ArrayList <;整数>;(MaxCapacity);。

使用任何其他比初始值设定项中立即评估的表达经常问这样的问题。在构造函数中显式地做复杂的初始化是比较好的。

answer2: 回答2:

The reason is the statement: private int[] integers = getIntegers();

When you instantiate this class it leads to invocation of method getIntegers which invokes getCapacity but list is not yet initialized and it is null. So when it invokes list.get(front) it gets NPE.

The reason is the statement: private int[] 整数= getintegers();

当你实例化这个类导致方法调用列表getintegers getcapacity但尚未初始化调用,它是空的。所以当它调用列表。得到了NPE(前)。

answer3: 回答3:

Your code private int[] integers = getIntegers(); gets executed during initialization of IntegerList instance.

Change your code to

public int getCapacity() {
    if (list == null || list.get(front) == null) 
        return 0;
    else 
        return 10;
}

你的代码私有[]整数= getintegers();获取integerlist实例初始化期间执行。

更改您的代码

public int getCapacity() {
    if (list == null || list.get(front) == null) 
        return 0;
    else 
        return 10;
}
answer4: 回答4:

Remove that null thing in the constructor array. By default Nulls are already there. Secondly change private int[] integers = getIntegers(); thing to private int[] integers;

and do this as the last line of constructor code:

integers = getIntegers();

Remove that null thing in the constructor array. By default Nulls are already there. Secondly change private int[] 整数= getintegers(); thing to private int[] integers;

并将其作为构造函数代码的最后一行:

整数= getintegers();

answer5: 回答5:

The point is the instance member initialization takes place secretly before the explicit constructor code you provide hence in your case

private int[] integers = getIntegers();

kicks in as the first line of execution when you call the constructor by initializing the IntegerClass and at that point the ArrayList list is still pointing to null and hence can't be used.

要点是实例成员初始化在您提供的显式构造函数代码之前发生,因此在您的案例中

private int[] 整数= getintegers();

开始为执行第一行,当你调用构造函数初始化integerclass那时ArrayList列表仍然指向零,因此不能用。

answer6: 回答6:

look into this,

public int getCapacity() {
        if (list.get(front) == null) 
            return 0;
        else 
            return 10;
    }

you are getting nullPointerException because you are storing null. and list.get(front) when you try to get value it'll throw exception before comparison.

its batter you store 0 instead of null. or wtite this code within try catch block. like following.

 public int getCapacity() {
  int result =0;
try{
      if (list.get(front) != null) 
             return 10;
   }catch(Exception e) {
     return 0
 }

}

看看这个,

public int getCapacity() {
        if (list.get(front) == null) 
            return 0;
        else 
            return 10;
    }

你是因为你是存放null时抛出。当你试图得到值时,它会在比较之前抛出异常。

它的面糊你存储0而不是空。或列此代码在try-catch块。像下面的。

 public int getCapacity() {
  int result =0;
try{
      if (list.get(front) != null) 
             return 10;
   }catch(Exception e) {
     return 0
 }

}
java  arraylist  nullpointerexception