Java:Null Pointer Deque Iterator
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java:Null Pointer Deque Iterator,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4394字,纯文字阅读大概需要7分钟。
内容图文
![Java:Null Pointer Deque Iterator](/upload/InfoBanner/zyjiaocheng/697/5d943c933c81409ab8f30e40466a3ae9.jpg)
我被指派编写一个以中缀表示法采用数学表达式的类,并将该表达式转换为后缀表示法中的等效表达式.那部分我已经完成了.
我还被分配编写一个迭代器,让客户端代码迭代后缀表达式的标记.
所以,到目前为止我的迭代器代码是:
class PostfixIterator implements Iterator<String> { //this is line 117
private Deque<String> postfix;
public PostfixIterator(Deque<String> postfix) {
this.postfix = postfix;
}
public boolean hasNext() {
return postfix.isEmpty();
}
public String next() {
return postfix.pop(); //this is line 130
}
}
当我尝试创建迭代器的一个实例并调用其中一个方法时,我得到一个空指针异常,我无法弄清楚为什么.
这就是我的主要看法:
public static void main(String[] args){
InfixToPostfix a = new InfixToPostfix("(123)^45+6*7/89");
Iterator itr = a.iterator();
System.out.println(itr.next());
}
根据我的编译器,返回postfix.pop()正在评估为null.我不知道为什么会这样.
那么,有人可以帮我解决这个问题并解释为什么我现在所做的不起作用吗?
谢谢
这是我的整个InfixToPost修复类:
import java.util.*;
public class InfixToPostfix{
private Deque<String> postfix;
public InfixToPostfix(String infix){
Deque<String> postfix = new LinkedList<String>();
Deque<String> infixQ = new LinkedList<String>();
//tokenize the user input
int i = 0;
char ch;
infix = infix.replaceAll("\\s","");//make sure there is no whitespace
while(i < infix.length()){
ch = infix.charAt(i);
if(ch == '(' || ch == ')'|| ch == '+'|| ch == '-'
|| ch == '/' || ch == '%'|| ch == '*'
|| ch == '^'){
String s =ch+"";
infixQ.add(s);
i++;
}
else if (Character.isDigit(ch)){
String s ="";
int j = i;
char c = infix.charAt(j);
while(j <= infix.length()-1 && //accumulate the digits in that number
Character.isDigit(c = infix.charAt(j))){
s = s + c;
j++;
}
infixQ.add(s);
i=j;
}
else if (Character.isLetter(ch)){
String s ="";
int j = i;
char c = infix.charAt(j);
while(j <= infix.length()-1 && //accumulate the lettes in that variable
Character.isLetter(c = infix.charAt(j))){
s = s + c;
j++;
}
infixQ.add(s);
i=j;
}
}
System.out.println(infixQ);
//start shunting-yard
Deque<String> stack = new ArrayDeque<String>();
Iterator<String> itr = infixQ.iterator();
while(itr.hasNext()){
String s = itr.next();
//if token is number or a variable, put it on the output queue
if(Character.isDigit(s.charAt(0))
|| Character.isLetter(s.charAt(0))){
postfix.add(s);
}
if(s.equals("(")){
stack.push(s);
}
if(s.equals(")")){
while((!stack.isEmpty())&&(!stack.peek().equals("("))){
postfix.add(stack.pop());
}
stack.pop();
}
if(s.equals("+") || s.equals("-")){
while((!stack.isEmpty()) && (stack.peek().equals("+")
|| stack.peek().equals("-")
|| stack.peek().equals("*") || stack.peek().equals("/")
|| stack.peek().equals("^"))){
postfix.add(stack.pop());
}
stack.push(s);
}
if(s.equals("*") || s.equals("/") || s.equals("%")){
if(!stack.isEmpty()){
while((!stack.isEmpty())&&(stack.peek().equals("*")
|| stack.peek().equals("/")
|| stack.peek().equals("%")
|| stack.peek().equals("^"))){
postfix.add(stack.pop());
}
}
stack.push(s);
}
if(s.equals("^")){
stack.push(s);
}
}
while(!stack.isEmpty()){
postfix.add(stack.pop());
}
System.out.println(stack.isEmpty());
System.out.println(postfix);
}
public Iterator<String> iterator(){
return new PostfixIterator(postfix);
}
public static void main(String[] args){
InfixToPostfix a = new InfixToPostfix("(123)^45+6*7/89");
Iterator itr = a.iterator();
System.out.println(itr.next()); // this is line 112
}
}
我很肯定它的写得不好.我只是需要它才能工作,所以我可以把它打开.
这是我的堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at PostfixIterator.next(InfixToPostfix.java:130)
at PostfixIterator.next(InfixToPostfix.java:117)
at InfixToPostfix.main(InfixToPostfix.java:112)
解决方法:
您的问题是关于数据字段后缀的范围
这就是你拥有的:
public class InfixToPostfix{
private Deque<String> postfix; <-- this is data field
public InfixToPostfix(String infix){
Deque<String> postfix = new LinkedList<String>();
^
|
you declared that reference here again which shadows the data field.
postfix is just visible in constructor and out of here your data field
is still pointing to null value.
改为
postfix = new LinkedList<String>();
因此,您将实例化后缀,当您想要访问它时,它将永远不会为null,因为您实例化了数据字段后缀.
一些建议:
>从Java 7开始,您可以使用钻石推理
例如:
List<String> myList = new ArrayList<String>();
可以写
List<String> myList = new ArrayList< >();
^
|
如果您可以在下面的代码中为迭代器函数选择不同的名称,那么更好,因为您可能会混淆读取代码的人
public Iterator<String> iterator(){
return new PostfixIterator(postfix);
}
内容总结
以上是互联网集市为您收集整理的Java:Null Pointer Deque Iterator全部内容,希望文章能够帮你解决Java:Null Pointer Deque Iterator所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。