![]() |
Java code.....null pointer exceptions
Hey ppl, im close to completion in a java project, but im getting some null pointer errors. I have 6 classes NodeDictionary, ListDictionary, DictionaryArray, NodeWordbase, wordbase1, Driver. Basically im trying to read a file(dictionary.txt) into an array of linked lists in the method addToDictionary() in DictionaryArray class, im then trying to make a linked list in wordbase1 from the words in the array of linked list with words less than the size of 4. Ive found where the error is occuring, indicated by the error msg at the very bottom of this post. If anyone knows how i can tackle this ill would appreciate the help. the classes are below.
ListDictionary and NodeDictionary ------------------------------------------- :
import java.util.*; import java.io.*; public class ListDictionary { private NodeDictionary head; public ListDictionary() { head = null; } public NodeDictionary search(String word) { NodeDictionary p = head; String dataAtPosition; while(p != null) { dataAtPosition = p.word; if( dataAtPosition.equals(word)) { return p; } p = p.next; } return null; } public boolean retrieveWord(String word) { return (search(word) != null); } public void showList() { NodeDictionary p = head; while(p !=null) { System.out.println(p.word); p = p.next; } } public void insert(String word) { NodeDictionary p = new NodeDictionary(word); if(head == null) { head = p; } else if( (head.word).compareTo(p.word) >= 0 ) { p.next = head; head = p; } else { NodeDictionary node = head; while( (node.next != null) && ((node.next).word).compareTo(p.word) < 0 ) { node = node.next; } p.next = node.next; node.next = p; } } public NodeDictionary getHead() { return head; } public class NodeDictionary { protected String word; private NodeDictionary next; public NodeDictionary(String word) { this.word = word; next = null; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public NodeDictionary getNext() { return next; } public void setNext(NodeDictionary next) { this.next = next; } } }DictionaryArray ---------------------------- :
import java.io.*; public class DictionaryArray { public static ListDictionary[] array; private ListDictionary insert; public DictionaryArray(ListDictionary insert) { this.insert = insert; array = new ListDictionary[26]; for(int i=0; i<26; i++) { array = new ListDictionary(); } } public ListDictionary[] getArray() { return array; } public void addToDictionary() { try { BufferedReader input = new BufferedReader(new FileReader("dictionary.txt")); String line = input.readLine(); char arrayLine; //is the words in dictionary.txt gonna be upper or lower case while(line != null) { arrayLine = ((char)line.charAt(0)); int letter = (int)arrayLine; if( letter >= 97 && letter <= 122) { array[letter-97].insert(line); } else if(letter >= 65 && letter <= 90) { array[letter-65].insert(line); } line = input.readLine(); } } catch(FileNotFoundException e) { System.out.println("File opening problem"); } catch(IOException e) { System.out.println("File reading problem"); } } public void setArray(ListDictionary[] array, int position) { this.array[position] = array[position]; } public ListDictionary getArray(int j) { return array[j]; } }NodeWordBase and wordbase1 -------------------------------- :
import java.io.*; public class wordbase1 extends ListDictionary { private wordbase head; private DictionaryArray insert; private DictionaryArray[] test; private ListDictionary in; public wordbase1(DictionaryArray insert) { super(); in = null; this.insert = insert; head = null; test = new DictionaryArray[26]; for(int i=0; i<26; i++) { test = new DictionaryArray(in); } } public void playGame1(String playerB) { wordbase1 game1 = new wordbase1(insert); game1.insertDictionary(); boolean gameOn = true; String playerA = head.word; while(gameOn) { if(findWord1(playerB)) { if( playerB.charAt(0) == playerA.charAt(0)) { if(!reusedWord(playerB)) { playerA = AWord(playerB); if(playerA == null) { gameOn = false; System.out.println("I Have run out of words. Game is terminated. The player has won"); } } } } else { gameOn = false; System.out.println("Game is terminated. The computer has won."); } } } public void insertDictionary() { insert.addToDictionary(); wordbase p; for(int i = 0; i < 26; i++) { ListDictionary[] test = insert.getArray(); if(test.getHead() == null) { System.out.println("Head was null"); // return; } p = new wordbase(""); //p = new wordbase(test.getHead().getWord()); //int testBase = (test.getHead().getWord()).length(); //if( testBase <=4) { if(head == null) { head = p; } else if(head.word.compareTo(p.word) >=0) { p.next = head; head = p; } else { wordbase node = head; while((node.next != null) && ((node.next).word).compareTo(p.word) < 0) { node = node.next; } p.next = node.next; node.next = p; } } p.flag = 0; p = p.next; } } public boolean findWord1(String target1) { wordbase p = head; String dataAtP; while( p!=null) { dataAtP = p.word; if( dataAtP.equals(target1) ) { p.flag = 1; return true; } p = p.next; } return false; } public String AWord(String player) { wordbase p = head; while(((p.word).charAt(0) != player.charAt(0)) || (p != null)) { p = p.next; } if( p == null) { return null; } p.flag = 1; return (p.word); } public boolean reusedWord(String playerB) { wordbase p = head; while(p != null) { if((p.word).equals(playerB)) { if(p.flag == 1) { return true; } } p = p.next; } return false; } public class wordbase extends NodeDictionary { private int flag; private wordbase next; public wordbase(String word) { super(word); flag = 0; next = null; } } }Driver -------------------- :
import java.io.*; import java.util.*; public class Driver { public static void main(String[] args) { ListDictionary a = new ListDictionary(); DictionaryArray b = new DictionaryArray(a); wordbase1 play1 = new wordbase1(b); wordbase2 play2 = new wordbase2(b); Scanner keyboard = new Scanner(System.in); boolean exitProgram = false; while(!exitProgram) { System.out.println("Welcome to WordLink. Please choose a difficulty level (1 or 2)"); int wordbaseGame = keyboard.nextInt(); if(wordbaseGame == 1) { play1.playGame1(keyboard.nextLine()); } else if(wordbaseGame == 2) { play2.playGame2(keyboard.nextLine()); } else { System.out.println("Choose 1 or 2"); } System.out.println("1. Start a new game\n2. Exit\nPlease choose an option:"); if( keyboard.nextInt() == 2) { exitProgram = true; } } } }The Errors --------------------------------- 1 Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Head was null Exception in thread "main" java.lang.NullPointerException at wordbase1.playGame1(wordbase1.java:38) at Driver.main(Driver.java:30) |
Have you considered putting line breaks in your code? I have a spaghetti farm (an inch wide and a mile long) that you can have cheap.
|
yeah! I am not even going to bother reading that! Try making your source more legible!
@DaWei: How much? lol |
| All times are GMT -5. The time now is 12:57 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC