i am working on an infix to postfix project and have reached a troubling point.

one by one i must extract a string token from the string being inputted to the method infixtopostfix and one by one input them to the front of the deque named postfix.

i dont understand what is wrong with what i am doing as i am moving through my while loop pulling one token at a time from the string using

while(infixString.hasMoreTokens() )
{
String temp = infixString.nextToken();

and inserting one by one into my deque string named postfix using:

postfix.addFirst(temp);

i suppose i am dealing with an obvious syntax error or small logical error. i am receiving the following error:

Exception in thread "main" java.lang.NullPointerException
at InfixToPostfix.<init>(InfixToPostfix.java:22)
at InfixToPostfix.main(InfixToPostfix.java:58)

please if someone could shed some light in the right direction i would very much appreciate it and this is my code thus far:

import java.util.*;
import java.util.Deque;
import java.util.StringTokenizer; //imports class to tokenize a string

/** Class documentation goes here. Write more than you did last time.
 */

public class InfixToPostfix {

    private Deque<String> postfix; // Used as a queue of String

    /** Method documentation goes here
     */

    public InfixToPostfix(String infix) {//parse through string and turn into tokens.

    StringTokenizer infixString = new StringTokenizer(infix, "+-/*()", true);

    while(infixString.hasMoreTokens() )
    {
      String temp = infixString.nextToken();

      postfix.addFirst(temp);

      System.out.println(infixString.nextToken());
      /*
      if(# or variable){
      move to outputqueue
      }
      if()

      if()*/
    }



        //output oken when parsing..........we get:
        //on left                      prefix notation
        //above                        infix notation
        //on right                     postfix notation

        // Build postfix as a queue representing a postfix expression

        postfix = new LinkedList<String>();
    }

    /** Mehtod documentation goes here
     */

    public Iterator<String> iterator() {

        return new PostfixIterator(postfix) ;
    }

    public static void main(String[] args) {

        // Simple embedded unit-test for InfixToPostfix

        InfixToPostfix test = new InfixToPostfix("2+3*5");
    }

}

// PostfixIterator implements Iterator<String>, so is an acceptable type

// whenever Iterator<String> is specified as the type (eg, iterator() above).

class PostfixIterator implements Iterator<String> {

    private Deque<String> postfix;

    public PostfixIterator(Deque<String> postfix) {

        this.postfix = postfix;
    }

    public boolean hasNext() {
        return true;
    }

    public String next() {
        return "";
    }

}

Recommended Answers

All 8 Replies

I believe it should be fairly easy to figure this one out. When is the NullPointerException thrown? From the stack trace can you locate the line which throws this exception? (InfixToPostfix.<init>(InfixToPostfix.java:22)). What piece of code on that line could possibly be null?

Hint: Java initializes class members to their default value when the class object is created. All references are by default initialized to null.

So i need to create an if statement that checks if the next token is null before actually setting temp equal to nextToken to avoid the nullpointerexception does that sound right? from happening

No, it's not the token that's null.
What is the value of the reference variable postfix on line 23 in the code you posted?

It is temp which should contain ("2+3*5") one at a time

You misread the question.
I didn't ask what was in the DeQueue, I asked about the variable called postFix.
If youy are still confused, try printing postFix at line 33

When i print postfix the output is:
Null
Null
Null

I now understand why i am receiving a null pointer exception. So my problem is i am adding one string token to the deque wrong right? I am trying to assign something that is pointing to null. Which is causing the error. So i need to initialize deque with a blank value so when i add to it, the token will be pointing to a value and not null right?

The problem is not with the tokens.
~s.o.s~ gave you the hint 21 hours ago, but you ignored it.
You create a variable called postFix, but you don't give it a value until line 44, so on line 23 it still has its default value of nulland when you try to use it, you get an NPE.

I obviously did not understand his post. I wouldn't ignore an effort to help!
But i understand and i line 44 was supposed to be the first line in the method but i had written code on top and it was lost at the bottom. Thank you very much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.