先给一些代码片段public void delete(String input)
{
LinkedStack<String> addrStack = splitStr(input);
               
                ......
        }
                
private static LinkedStack<String> splitStr(String input)
{
LinkedStack<String> addrStack = new LinkedStack<String>(); String[] s = input.split("\\.");
for (int i = 0; i < s.length; i++)
{
if (i != s.length - 1)
addrStack.push(s[i] + ".");
else
addrStack.push(s[i]);
} return addrStack;
}
编译都通过了,但是运行的时候有NullPointerException,调试的时候在splitStr函数的
LinkedStack<String> addrStack = new LinkedStack<String>();
出错,不知道为什么
小弟新手,恳请大家指教指教

解决方案 »

  1.   

    其中LinkedStack<E>是一个泛型栈。我把splitStr函数单独拿出来运行是没有问题的,但是不知道为什么,用另一个函数调用就出错了
      

  2.   

    这个明显是LinkedStack写的有问题,楼主把这个类的代码也贴一下吧
      

  3.   

    LinkedStack<E>应该没有问题啊,因为我把我把splitStr函数单独拿出来运行是没有问题的
    下面是LinkedStack<E>的定义public class LinkedStack<E> implements Cloneable
    {
    private Node<E> top; public LinkedStack()
    {
    top = null;
    } public LinkedStack<E> clone()
    {
    LinkedStack<E> answer; try
    {
    answer = (LinkedStack<E>) super.clone();
    } catch (CloneNotSupportedException e)
    {
    throw new RuntimeException(
    "This class does not implements Cloneable");
    } answer.top = Node.listCopy(top);
    return answer;
    } public boolean isEmpty()
    {
    return (top == null);
    } public E peek()
    {
    if (top == null)
    throw new EmptyStackException();
    return top.getData();
    } public E pop()
    {
    E answer; if (top == null)
    throw new EmptyStackException(); answer = top.getData();
    top = top.getLink();
    return answer;
    } public void push(E item)
    {
    top = new Node<E>(item, top);
    } public int size()
    {
    return Node.listLength(top);
    }
    }