Null pointer on building a binary tree

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

Join Date: Apr 2007
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Null pointer on building a binary tree

 
0
  #1
Dec 5th, 2007
I'm getting a whole bunch of null pointer exceptions when I run my program. Here's the info:
My program takes a text file (myIn.txt), will keep individual totals of all the characters, then put them into an ordered linked list. Now I need to build a pseudo-Huffman binary tree, with a dummy root, and then the most occurring character as first left child, 2nd most occurring as first right, then continue this pattern. I believe I have everything working except for this last linking part. When I run it, bam, null pointers. Not sure why I'm getting them. Any info would be much appreciated. I know that the character counting, the linked list creation and ordered insertion works just fine. Also, i can build the dummy root and link the first 2 nodes in the list to it, my problem is continuing the linking from there. Thanks in advance,
-jmasta
  1.  
  2.  
  3. //Jed Stark
  4. //IT 214
  5. import java.io.*;
  6. //http://en.allexperts.com/q/Java-1046/counting-characters-file.htm
  7. /**
  8. * CountLetters - read unicode characters
  9. * from an input file, count the
  10. * occurrences of each character, then
  11. * print the counts only for letters. To
  12. * handle just ASCII, change MAX_CHAR to 255.
  13. */
  14.  
  15. class Node
  16. {
  17. //Our instance variables
  18. private char value;
  19. private int count;
  20. private Node left;
  21. private Node right;
  22. private Node link;
  23.  
  24.  
  25. //The constructor method
  26. public Node() {
  27. this.left = null;
  28. this.right = null;
  29. this.link = null;
  30. }
  31.  
  32. public Node(char value, int count)
  33. {
  34. this.value = value;
  35. this.count = count;
  36. this.right= null;
  37. this.left = null;
  38. this.link = null;
  39. }
  40. // The set methods
  41. public void setLink(Node p)
  42. {
  43. link = p;
  44. }
  45. public void setLeft(Node p)
  46. {
  47. left = p;
  48. }
  49. public void setRight(Node p)
  50. {
  51. right = p;
  52. }
  53. public void setValue(char p)
  54. {
  55. value = p;
  56. }
  57. public void setCount(int r)
  58. {
  59. count = r;
  60. }
  61.  
  62. //The get methods
  63.  
  64. public int getCount()
  65. {
  66. return count;
  67. }
  68. public char getValue()
  69. {
  70. return value;
  71. }
  72. public Node getLeft()
  73. {
  74. return left;
  75. }
  76. public Node getRight()
  77. {
  78. return right;
  79. }
  80. public Node getLink()
  81. {
  82. return link;
  83. }
  84. public String printer()
  85. {
  86. return "Character is "+value+" and there were "+count+".";
  87. }
  88.  
  89. }
  90. class OrderedLinkedList
  91. {
  92. private Node head;
  93. public OrderedLinkedList()
  94. {
  95. this.head = null;
  96. }
  97.  
  98. public Node buildRoot() {
  99. Node root = new Node();
  100. root.setLeft(head);
  101. root.setRight(head.getLink());
  102. root.setLink(head);
  103. return root;
  104. }
  105.  
  106. public Node buildTree(Node current, int X, Node leftspot, Node rightspot, Node root) {
  107. while (current != null) {
  108. if (X == 1) {
  109. leftspot = head;
  110. rightspot = head.getLink();
  111. X++;
  112. }
  113. current.setLeft(leftspot);
  114. leftspot = moveLeftSpot(leftspot);
  115. current.setRight(rightspot);
  116. rightspot = moveRightSpot(rightspot);
  117. buildTree(current.getLink(), X, leftspot, rightspot, root);
  118. }
  119. return root;
  120. }
  121.  
  122.  
  123. public Node moveLeftSpot(Node leftspot) {
  124. if (leftspot == null)
  125. leftspot = null;
  126. else {
  127. leftspot = leftspot.getLink();
  128. leftspot = leftspot.getLink();
  129. }
  130. return leftspot;
  131. }
  132.  
  133. public Node moveRightSpot(Node rightspot) {
  134. if (rightspot == null)
  135. rightspot = null;
  136. else {
  137. rightspot = rightspot.getLink();
  138. rightspot = rightspot.getLink();
  139. }
  140. return rightspot;
  141. }
  142.  
  143. public void insert(Node p)
  144. {
  145. Node front = head;
  146. Node back = null;
  147. while ((front != null) && (p.getCount() <= front.getCount()))
  148. {
  149. back = front;
  150. front = front.getLink();
  151. }
  152. if (head == null)
  153. {
  154. head = p;
  155. }
  156. else if (p.getCount() > head.getCount())
  157. {
  158. p.setLink(head);
  159. head = p;
  160. }
  161. else
  162. {
  163. p.setLink(front);
  164. back.setLink(p);
  165. }
  166. }
  167. //The traverse method for moving through the list
  168. public void traverse()
  169. {
  170. Node p = head;
  171. while(p != null)
  172. {
  173. System.out.println(p.printer());
  174. p = p.getLink();
  175. }
  176. }
  177. //The delete method. Gives confirmation when someone is deleted, or returns an error if they are not.
  178. public void delete(char target)
  179. {
  180. Node front = head;
  181. Node back = null;
  182. while ((front != null) && ((target !=front.getValue())))
  183. {
  184. back = front;
  185. front = front.getLink();
  186. }
  187. if (front == null)
  188. System.out.println("Target not in list");
  189. else if (target == head.getValue())
  190. {
  191. head = head.getLink();
  192. System.out.println("ID "+target+" has been deleted.");
  193. }
  194.  
  195. else
  196. {
  197. back.setLink(front.getLink());
  198. front.setLink(null);
  199. System.out.println("ID "+target+" has been deleted.");
  200. }
  201. }
  202.  
  203. }
  204. public class HuffmanTest {
  205. public static final int MAX_CHAR = 65535;
  206. public static void printTree(Node node, int depthI, String spaceS){
  207. if (node == null) return;
  208. printTree(node.getRight(), depthI+1, spaceS + " ");
  209. printTreeResult(node, spaceS);
  210. printTree(node.getLeft(), depthI+1, spaceS + " ");
  211. }
  212.  
  213.  
  214. public static void printTreeResult(Node node, String spaceS){
  215. System.out.println(spaceS + node.getValue());
  216. }
  217. public static void main(String[] args) {
  218. OrderedLinkedList myList = new OrderedLinkedList();
  219. Node newNode;
  220. Node root;
  221. Node finalTree;
  222. Node leftspot = null;
  223. Node rightspot = null;
  224. char newChar;
  225. int charCount;
  226. int c;
  227. long total;
  228. File inputFile = new File("myIn.txt");
  229. File outputFile = new File("myOut.txt");
  230. int counts[];
  231. counts = new int[MAX_CHAR+1];
  232. total = 0;
  233. try {
  234. FileReader in = new FileReader(inputFile);
  235. for(total = 0;(c = in.read()) != -1; total++) {
  236. if (c <= MAX_CHAR) counts[c] += 1;
  237. }
  238. in.close();
  239. }
  240. catch (IOException e) {
  241. System.err.println("I/O Error: " + e);
  242. System.exit(1);
  243. }
  244. // print the counts for all letters
  245. try {
  246. FileWriter out = new FileWriter(outputFile);
  247. for(c = 0; c <= MAX_CHAR; c++) {
  248. if (counts[c] > 0) {
  249. newChar = (char)c;
  250. charCount = counts[c];
  251. newNode = new Node(newChar, charCount);
  252. myList.insert(newNode);
  253. System.out.println(" " + (char)c + " " +
  254. counts[c]);
  255. out.write(" " + (char)c + " " + counts[c]+"\r\n");
  256. }
  257. }
  258. out.close();
  259. }
  260. catch (IOException e) {
  261. System.err.println("I/O Error: " + e);
  262. System.exit(1);
  263. }
  264. root = myList.buildRoot();
  265. myList.buildTree(root, 1, leftspot, rightspot, root);
  266. printTree(root, 0, " ");
  267. System.out.println("Total chars read: " + total);
  268. myList.traverse();
  269. }
  270. }
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
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