Don't understand why I'm getting this error

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2007
Posts: 58
Reputation: Noliving is an unknown quantity at this point 
Solved Threads: 0
Noliving Noliving is offline Offline
Junior Poster in Training

Don't understand why I'm getting this error

 
0
  #1
Apr 14th, 2008
Well this is a lab assignment, the problem I'm having is that the sample code he gave us is suppose to run as is, but it doesn't. I email him and he thought that most likely there might be a problem with the CLASSPATH for java or that I might have messed with the orignal files for the lab due to the fact that no one else has gotten the same issue or complained of the same issue.

Here is the orignal sample code as is:

  1. import java.io.*;
  2.  
  3. /** Class for a binary tree that stores type E objects.
  4. * @author Koffman and Wolfgang
  5. * */
  6.  
  7. public class BinaryTree < E >
  8. implements Serializable {
  9.  
  10. /** Class to encapsulate a tree node. */
  11. protected static class Node < E >
  12. implements Serializable {
  13. // Data Fields
  14. /** The information stored in this node. */
  15. protected E data;
  16.  
  17. /** Reference to the left child. */
  18. protected Node < E > left;
  19.  
  20. /** Reference to the right child. */
  21. protected Node < E > right;
  22.  
  23. // Constructors
  24. /** Construct a node with given data and no children.
  25.   @param data The data to store in this node
  26.   */
  27. public Node(E data) {
  28. this.data = data;
  29. left = null;
  30. right = null;
  31. }
  32.  
  33. // Methods
  34. /** Return a string representation of the node.
  35.   @return A string representation of the data fields
  36.   */
  37. public String toString() {
  38. return data.toString();
  39. }
  40. }
  41.  
  42. // Data Field
  43. /** The root of the binary tree */
  44. protected Node < E > root;
  45.  
  46. public BinaryTree() {
  47. root = null;
  48. }
  49.  
  50. protected BinaryTree(Node < E > root) {
  51. this.root = root;
  52. }
  53.  
  54. /** Constructs a new binary tree with data in its root,leftTree
  55.   as its left subtree and rightTree as its right subtree.
  56.   */
  57. public BinaryTree(E data, BinaryTree < E > leftTree,
  58. BinaryTree < E > rightTree) {
  59. root = new Node < E > (data);
  60. if (leftTree != null) {
  61. root.left = leftTree.root;
  62. }
  63. else {
  64. root.left = null;
  65. }
  66. if (rightTree != null) {
  67. root.right = rightTree.root;
  68. }
  69. else {
  70. root.right = null;
  71. }
  72. }
  73.  
  74. /** Return the left subtree.
  75.   @return The left subtree or null if either the root or
  76.   the left subtree is null
  77.   */
  78. public BinaryTree < E > getLeftSubtree() {
  79. if (root != null && root.left != null) {
  80. return new BinaryTree < E > (root.left);
  81. }
  82. else {
  83. return null;
  84. }
  85. }
  86.  
  87. /** Return the right sub-tree
  88.   @return the right sub-tree or
  89.   null if either the root or the
  90.   right subtree is null.
  91.   */
  92. public BinaryTree<E> getRightSubtree() {
  93. if (root != null && root.right != null) {
  94. return new BinaryTree<E>(root.right);
  95. } else {
  96. return null;
  97. }
  98. }
  99.  
  100.  
  101. /**** BEGIN EXERCISE ****/
  102. /** Return the data field of the root
  103.   @return the data field of the root
  104.   or null if the root is null
  105.   */
  106. public E getData() {
  107. if (root != null) {
  108. return root.data;
  109. } else {
  110. return null;
  111. }
  112. }
  113. /**** END EXERCISE ****/
  114.  
  115. /** Determine whether this tree is a leaf.
  116.   @return true if the root has no children
  117.   */
  118. public boolean isLeaf() {
  119. return (root.left == null && root.right == null);
  120. }
  121.  
  122. public String toString() {
  123. StringBuilder sb = new StringBuilder();
  124. preOrderTraverse(root, 1, sb);
  125. return sb.toString();
  126. }
  127.  
  128. /** Perform a preorder traversal.
  129.   @param node The local root
  130.   @param depth The depth
  131.   @param sb The string buffer to save the output
  132.   */
  133. private void preOrderTraverse(Node < E > node, int depth,
  134. StringBuilder sb) {
  135. for (int i = 1; i < depth; i++) {
  136. sb.append(" ");
  137. }
  138. if (node == null) {
  139. sb.append("null\n");
  140. }
  141. else {
  142. sb.append(node.toString());
  143. sb.append("\n");
  144. preOrderTraverse(node.left, depth + 1, sb);
  145. preOrderTraverse(node.right, depth + 1, sb);
  146. }
  147. }
  148.  
  149. /** Method to read a binary tree.
  150.   pre: The input consists of a preorder traversal
  151.   of the binary tree. The line "null" indicates a null tree.
  152.   @param bR The input file
  153.   @return The binary tree
  154.   @throws IOException If there is an input error
  155.   */
  156. public static BinaryTree < String >
  157. readBinaryTree(BufferedReader bR) throws IOException {
  158. // Read a line and trim leading and trailing spaces.
  159. String data = bR.readLine().trim();
  160. if (data.equals("null")) {
  161. return null;
  162. }
  163. else {
  164. BinaryTree < String > leftTree = readBinaryTree(bR);
  165. BinaryTree < String > rightTree = readBinaryTree(bR);
  166. return new BinaryTree < String > (data, leftTree, rightTree);
  167. }
  168. }
  169.  
  170. }

Here is the testfile for it:

  1. import java.io.*;
  2. import javax.swing.JOptionPane;
  3.  
  4.  
  5. /** Program to test the modifications to the BinaryTree class
  6. * @author Leon Tietz
  7. * */
  8.  
  9. public class TestBinaryTree {
  10.  
  11. /** Exercise the changes to the BinaryTree class
  12.   * @param args - Unused*/
  13. public static void main(String[] args) {
  14. String outStr;
  15. String fileName =
  16. JOptionPane.showInputDialog("File name for tree data");
  17. try{
  18. BufferedReader bR = new BufferedReader(new FileReader(fileName));
  19. BinaryTree < String > myTree = BinaryTree.readBinaryTree(bR);
  20. outStr = myTree.toString();
  21. System.out.println(outStr);
  22. JOptionPane.showMessageDialog(null, "Preorder Traversal:\n" + outStr);
  23. /* // uncomment this section when you are ready to test your code
  24.   outStr = myTree.toStringPost();
  25.   System.out.println(outStr);
  26.   JOptionPane.showMessageDialog(null, "Postorder Traversal:\n" + outStr);
  27.   */
  28. }
  29. catch (FileNotFoundException ex){
  30. System.out.println("Bad file name!!");
  31. System.exit(1);
  32. }
  33. catch (java.io.IOException ex) {
  34. System.out.println("Error while reading file!!");
  35. System.exit(1);
  36. }
  37. }
  38.  
  39. }

Here is the error I'm getting:
java.lang.NoClassDefFoundError: TestBinaryTree
Caused by: java.lang.ClassNotFoundException: TestBinaryTree
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread "main" >Exit code: 1 Time: 0.317

I understand what the error means but I don't understand why if this is the orignal code and it is suppose to run as and I'm so far the only one that is getting this error.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Don't understand why I'm getting this error

 
0
  #2
Apr 14th, 2008
Did you first compile the first file then put it into your classpath ?

or
javac classfile.java testfiile.java
?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 58
Reputation: Noliving is an unknown quantity at this point 
Solved Threads: 0
Noliving Noliving is offline Offline
Junior Poster in Training

Re: Don't understand why I'm getting this error

 
0
  #3
Apr 14th, 2008
Well the problem is solved now, my professor sent me the files so they work now.

It wasn't a class path or any other type of problem.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 58
Reputation: Noliving is an unknown quantity at this point 
Solved Threads: 0
Noliving Noliving is offline Offline
Junior Poster in Training

Re: Don't understand why I'm getting this error

 
0
  #4
Apr 15th, 2008
Alright, well I'm new too tree's and I need help creating a postOrderTraverse and toStringPost() methods.

My understanding with the postorder traverse is that it is basically the same as a pre but that the root is the last instead of the first to be visited or done if you will.

This is what I had come up with for a postordertraverse for the preordertraverse that is in the code in the first post

  1. public String toStringPost() {
  2. StringBuilder sb = new StringBuilder();
  3. postOrderTraverse(root, 1, sb);
  4. return sb.toString();
  5. }
  6.  
  7.  
  8. private void postOrderTraverse(Node < E > node, int depth,
  9. StringBuilder sb) {
  10. for (int i = 1; i < depth; i++) {
  11. sb.append(" ");
  12. }
  13. if (node == null) {
  14. sb.append("null\n");
  15. }
  16. else {
  17. postOrderTraverse(node.left, depth + 1, sb);
  18. postOrderTraverse(node.right, depth + 1, sb);
  19. sb.append(node.toString());
  20. sb.append("\n");
  21.  
  22. }
  23. }


When I run the test code:

  1. mport java.io.*;
  2. import javax.swing.JOptionPane;
  3.  
  4.  
  5. /** Program to test the modifications to the BinaryTree class
  6. * @author Leon Tietz
  7. * */
  8.  
  9. public class TestBinaryTree {
  10.  
  11. /** Exercise the changes to the BinaryTree class
  12.   * @param args - Unused*/
  13. public static void main(String[] args) {
  14. String outStr;
  15. String fileName =
  16. JOptionPane.showInputDialog("File name for tree data");
  17. try{
  18. BufferedReader bR = new BufferedReader(new FileReader(fileName));
  19. BinaryTree < String > myTree = BinaryTree.readBinaryTree(bR);
  20. outStr = myTree.toStringPost();
  21. System.out.println(outStr);
  22. JOptionPane.showMessageDialog(null, "Postorder Traversal:\n" + outStr);
  23.  
  24.  
  25. }
  26. catch (FileNotFoundException ex){
  27. System.out.println("Bad file name!!");
  28. System.exit(1);
  29. }
  30. catch (java.io.IOException ex) {
  31. System.out.println("Error while reading file!!");
  32. System.exit(1);
  33. }
  34. }
  35.  
  36. }


The output still comes out in preordertraverse.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC