943,559 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 639
  • Java RSS
Apr 14th, 2008
0

Don't understand why I'm getting this error

Expand Post »
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:

java Syntax (Toggle Plain Text)
  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:

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Noliving is offline Offline
62 posts
since Sep 2007
Apr 14th, 2008
0

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

Did you first compile the first file then put it into your classpath ?

or
javac classfile.java testfiile.java
?
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Apr 14th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Noliving is offline Offline
62 posts
since Sep 2007
Apr 15th, 2008
0

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

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

java Syntax (Toggle Plain Text)
  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:

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Noliving is offline Offline
62 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Passing Info Between Forms
Next Thread in Java Forum Timeline: I need help with this reducing method





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC