-1

I'm getting an exception when I run my program. It compiles ok and it was working earlier so I'm not sure what's wrong. The error: http://gyazo.com/091235db6cc37154e8f7304e4c1482a8

It's supposed to print out a sequence given in a sideways tree.

import java.io.*;
import java.util.Scanner;

public class PrintSideWays{

    public static void main (String[] args){
        PrintSideWays p = new PrintSideWays();
        p.start(args[0]);

    }

    public void start (String args){
        buildTree(args);

    }

    public void buildTree(String preorder){

        BinarySearchTree<StringItem, String> bst = new BinarySearchTree<StringItem, String>();
        StringItem stringItem;



        System.out.println("Sideways printing tree by wcor690:");



        for ( int i=0; i < preorder.length() ; i++){
            String letter = preorder.substring(i,i+1);
            stringItem = new StringItem(letter);
            bst.insert(stringItem);



        }
         TreeNode root = bst.getRoot();
         print(root, " ");

        System.out.print("Inorder traversal:");
         inorderTraversal(root);
         System.out.println();

        System.out.println("Please enter the remove sequence: ");
        Scanner in = new Scanner(System.in);
        String userRemove = in.next();

        for(int i = 0; i < userRemove.length(); i++){
            String letter =  userRemove.substring(i,i+1);
            StringItem removeString = new StringItem(letter);
            try{ bst.delete(removeString);
                }
            catch(TreeException e){
                System.out.println(letter + " not found in tree.");
            }
        }

        print(root, " ");

        System.out.print("Inorder traversal:");
         inorderTraversal(root);
          System.out.println();
    }

    public void inorderTraversal(TreeNode node){

     if(node == null){
      return;
     }

      inorderTraversal(node.getLeft());
      System.out.print(node.getItem());
      inorderTraversal(node.getRight());

    }
    public void print(TreeNode treeNode, String indent ) {  
        if (treeNode == null){ 
            return; 
        }

        print( treeNode.getRight(), indent + "   " );  
        System.out.println(indent + treeNode.getItem());  
        print(treeNode.getLeft(), indent + "   ");
    }
}  
  • 1
    Start by looking at the Exception message and stack trace. What *file* and *line* does it say to look at? Then work backwards and reason why said (already identified line) may have caused such an Exception. Alternatively, attach a debugger. – user2864740 Oct 25 '13 at 02:58
  • *Always* study the exception message for useful information. It may seem like gibberish at first, but it's exceedingly valuable stuff. – Hot Licks Oct 25 '13 at 03:34

2 Answers2

0

p.start(args[0]); is your issue based on the error. You're not passing it any arguments.

NG.
  • 22,560
  • 5
  • 55
  • 61
0

It says "at PrintSideWays.main(PrintSideWays.java:8)" Which means the error is in the main method of the program. And the error is located at line 8. This means their is an error in this line of code: p.start(args[0]) and so args[0] must not exist if its an array out of bounds Exception.

Frostsoft
  • 46
  • 8