【发布时间】:2014-11-14 23:36:24
【问题描述】:
发生 NullPointerException,我不知道为什么。
import java.util.*;
public class One {
//first class with me handling stacks
Stack<String> s = new Stack<String>();
String[] stacks = null;;
public One(){
s.push("one");
s.push("two");
s.push("three");
s.push("four");
s.push("five");
s.push("six");
add();
}
public void add(){
for (int i=0;i<s.capacity();i++){
String temp = (String) s.pop(); //this is the part that gives me a NullPointerException
System.out.println(temp);
}
}
public static void main(String[] args){
One obj1 = new One();
}
}
【问题讨论】:
-
使用 s.size() 代替 s.capacity()
-
使用 s.size() 并让我知道您是否遇到相同的异常
-
容量用于知道运行时动态分配给堆栈集合的内存