Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 12th, 2006, 9:19 AM   #1
ELHEK
Newbie
 
Join Date: May 2006
Posts: 9
Rep Power: 0 ELHEK is on a distinguished road
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)
ELHEK is offline   Reply With Quote
Old Oct 12th, 2006, 9:21 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 12th, 2006, 9:26 AM   #3
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
yeah! I am not even going to bother reading that! Try making your source more legible!

@DaWei: How much? lol
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My first WIN32 API application ivan Show Off Your Open Source Projects 27 Jan 6th, 2006 5:04 PM
Urgent Java Help - Null Pointer Exceptions Xenon Java 10 Nov 28th, 2005 1:44 PM
Pointers in C (Part II) Stack Overflow C 2 Apr 29th, 2005 10:39 AM
Pointers in C (Part I) Stack Overflow C 4 Apr 28th, 2005 7:03 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC