1

I build an expression into a binary tree each time it rolls through the loop, creating a new tree at each ending of ")" and pushing those operators/operands into a stack to be popped back into one complete binary tree.

My Build Method:

package lab5;

import net.datastructures.*;

public class Expression<T> {

/** Contain Linked Tree and Linked Stack instance variables **/
LinkedBinaryTree<T> tree;
LinkedStack<LinkedBinaryTree<T>> stack;


public Expression () {
    tree = new LinkedBinaryTree<T> ();
    stack = new LinkedStack<LinkedBinaryTree<T>>();

} // end constructor

public LinkedBinaryTree<T> buildExpression (String expression) {// LinkedBinaryTree<T> is a type of LinkedBinaryTree
    // major TODO to implement the algorithm]
    LinkedBinaryTree<T> operand, op1, op2;
    LinkedStack<LinkedBinaryTree<T>> newStack = new LinkedStack<LinkedBinaryTree<T>>();
    String symbol;

    int i = 0;
    int len = expression.length();

    for (i = 0; i < len; i++) {
        symbol = expression.substring(i, i+1);

        if ((!symbol.equals ("(")) && (!symbol.equals (")"))) {
            operand = new LinkedBinaryTree<T> ();
            operand.addRoot((T)symbol);
            newStack.push(operand);
        } else if (symbol.equals ("(")){
            continue;
        } else {
            op2 = newStack.pop();
            operand = newStack.pop();
            op1 = newStack.pop();
        tree.attach(operand.root(), op1, op2);
        newStack.push(tree);
        }
    }
    tree = newStack.pop();
    return tree;

}  // end method buildExpression

}

My Test:

package lab5;
import net.datastructures.*;

public class ExpressionTest {

/**
 * @param args
 * @throws EmptyTreeException
 */

/** Paranthesize is code fragment 8.26 pg. 346 **/

/** evaluateExpression method apart of LinkedBinaryTree class in net.datastructures **/

public static void main(String[] args) {
    // declare local variables/objects
            String s = new String ();
            String exp = "((((3+1)x3)/((9-5)+2))-((3x(7-4))+6))"; //-13
            Expression<String> expression = new Expression<String> ();

            // print the expression string
            System.out.printf ("The original Expression String generated via printf: %s", exp);

            // create the tree using the 'stub' method
            // i.e. it does nothing
            LinkedBinaryTree<String> tree = expression.buildExpression (exp);

            // use Object.toString simply to print its reference
            System.out.printf ("\n\nThe Tree: %s\n", tree);

    }

}

I am getting a NullPointerException and I do not know why. Do I need to 'Try' and then let the error roll through?

8
  • You should use a debugger to check where and why you have the error. Commented Apr 9, 2016 at 22:12
  • It's suspending the run of the app due to the root of the tree. It stops after the t1.root.setParent(node) in a LinkedBinaryTree.class I am using to grab my LinkedBinaryTree object initialization from. This is apart of a net.datastructures that was provided by the author of the book im reading. In the book it states if I use addRoot(element), an error will occur if the tree is not empty. Commented Apr 10, 2016 at 20:18
  • are you setting conditions to catch that error? if you aren't you could say this in the addRoot(element) method if(root == null){ //addRootElement;} else{ //return an error message indicating there is already a root Commented Apr 10, 2016 at 22:29
  • No I am not. So, should I catch the error in my code to just let it roll through or handle it. I have no idea how to handle it haha. Commented Apr 10, 2016 at 22:30
  • an error would ocurr if the root is not empty because addRoot(element) is trying to add a root where there is already an element in the root, you should handle it to make sure your code doesn't crash Commented Apr 10, 2016 at 22:33

1 Answer 1

1

The error probably is related to checking for '(' (single quotes) instead of "(" (double quotes) in symbol.equals ('('). You compare a string to a character, which will never be equal.

Maybe also stack is not initialized? It should be local to buildExpression().

Note that your code snippet won't compile:

  • stack is not defined
  • symbol is not defined

BTW: buildExpression() could be static, that would avoid the empty unused expression allocation in main.

Checking for "(" first would avoid checking for "(" twice.

Sign up to request clarification or add additional context in comments.

12 Comments

Sorry I took so long to respond. Okay so I updated my buildExpression code above. I am still getting the same error NullPointerException. Here is the error: The original Expression String generated via printf: ((((3+1)x3)/((9-5)+2))-((3x(7-4))+6))Exception in thread "main" java.lang.NullPointerException
What is the stack trace printed for the expression?
I don't know what you mean.
Sorry, meant th stack trace fro the null pointer exception (expression -> exception): stackoverflow.com/questions/3988788/…
at net.datastructures.LinkedBinaryTree.attach(LinkedBinaryTree.java:251) at lab5.Expression.buildExpression(Expression.java:40) at lab5.ExpressionTest.main(ExpressionTest.java:47)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.