求助一个函数调用的问题

Xu_Moriarty 2012-06-30 11:38:13
先给一些代码片段

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>();
出错,不知道为什么
小弟新手,恳请大家指教指教
...全文
111 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
周靖峰 2012-06-30
  • 打赏
  • 举报
回复
这个明显是LinkedStack写的有问题,楼主把这个类的代码也贴一下吧
Xu_Moriarty 2012-06-30
  • 打赏
  • 举报
回复
其中LinkedStack<E>是一个泛型栈。

我把splitStr函数单独拿出来运行是没有问题的,但是不知道为什么,用另一个函数调用就出错了
Xu_Moriarty 2012-06-30
  • 打赏
  • 举报
回复
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);
}
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧