Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Norm

Thread: Hash Table problem with null pointer exception, need help

  1. #1
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Hash Table problem with null pointer exception, need help

    Java Code:
    public class HashTable<T> implements HashTableInterface<T> {
    
    	int numEntries, size, location, hashVal, index;
    	String[] hashTable;
    
    	public void HashTable() {
    		hashTable = new String[101];
    	}
    
    	public void HashTable(int n) {
    		hashTable = new String[size];
    	}
    
    	public void Insert(String S) {
    		location = HashValue(S);
    		System.out.println("Location" + S);
    		while (hashTable[location] != null)
    			location = (location + 1) % hashTable.length;
    		hashTable[location] = S;
    		NumEntries();
    	}
    
    	private int HashValue(String S) {
    		int i, ch1, ch2, ch3;
    		i = 0;
    		hashVal = 0;
    		i = S.length();
    		while (i > 0){
    			if(i - 3 >= 0){ // change to ascii value for extra credit
    				ch1 = S.charAt(i - 1);
    				ch2 = S.charAt(i - 2);
    				ch3 = S.charAt(i - 3);
    			System.out.println("ch1 " + ch1 + ", ch2 " + ch2 + ", ch3 " + ch3);
    			hashVal = hashVal + (ch3 * 1024) + (ch2 * 32) + ch1;
    			System.out.println("Chunk value: " + hashVal);
    			}
    			else if (i - 3 == -1){
    				ch1 = S.charAt(i - 1);
    				ch2 = S.charAt(i - 2);
    				hashVal = hashVal + (ch2 * 32) + ch1;
    				System.out.println("Ch1: " + ch1 + "Ch2: " + ch2);
    				System.out.println("Chunk value: " + hashVal);
    			}
    			else if (i - 3 == -2){
    				ch1 = S.charAt(i - 1);
    				hashVal = hashVal + ch1;
    				System.out.println("Ch1 " + ch1 + ", Chunk: " + hashVal);
    			}
    		    i = i - 3;
    	}
    		hashVal = hashVal % 101;
    		return hashVal;
    	}
    
    	public boolean Contains(String S) {
    		return false;
    	}
    
    	public int NumEntries() {
    		return numEntries++;
    	}
    }
    When I try to set the location in the hashTable array I get a null pointer exception. Error occurs on the hashTable[location] = S; line. I have S printing out and it prints out the string I set it to. I don't really get why I am getting the null pointer exception so any help would be appreciated.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Hash Table problem with null pointer exception, need help

    Is the variable hashTable null?

    Can you post the full text of the error message?

  3. #3
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Re: Hash Table problem with null pointer exception, need help

    Enter a word
    cat
    Exception in thread "main" java.lang.NullPointerException
    at HashTable.Insert(HashTable.java:17)
    at TestHashFunction.main(TestHashFunction.java:18)
    ch1 116, ch2 97, ch3 99
    Chunk value: 104596
    Locationcat

    I copied the insertion code directly out of the textbook. The test program I am using follows.
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class TestHashFunction {
    
    	public static void main(String[] args) throws FileNotFoundException {
    		Scanner input = new Scanner(System.in);
    		String S;
    		int i, numWords;
    		numWords = 0;
    		HashTable hTable = new HashTable();
    		HashTable dicTable = new HashTable();
    		i = 0; 
    		while (i < 5){
    			System.out.println("Enter a word ");
    			S = input.next();
    			hTable.Insert(S);
    			i++;
    		}
    		File F = new File("Dictionary.txt");	
    		Scanner dictFile = new Scanner(F);		
    		String line;
    		while (dictFile.hasNextLine()) {
    		   line = dictFile.nextLine();
    		   dicTable.Insert(line);
    		   numWords++;
    		}
    	}
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Hash Table problem with null pointer exception, need help

    Is the variable hashTable null?

    Why didn't this line print out??? I don't see it in your last post.
    System.out.println("Location" + S);

  5. #5
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Re: Hash Table problem with null pointer exception, need help

    Quote Originally Posted by Norm View Post
    Is the variable hashTable null?

    Why didn't this line print out??? I don't see it in your last post.
    System.out.println("Location" + S);
    The variable hashTable is null by default right?

    That line printed out right below chunk value.

    in the book they had S.HashValue(); but when I tried that I got an error.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Hash Table problem with null pointer exception, need help

    The variable hashTable is null by default right?
    If the value is null, then this statement will not work and you'll get the exception.
    while (hashTable[location] != null)

    add a println to show the value of hashTable?
    If it is null, then find out why it is null.

  7. #7
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Re: Hash Table problem with null pointer exception, need help

    Quote Originally Posted by Norm View Post
    If the value is null, then this statement will not work and you'll get the exception.
    while (hashTable[location] != null)

    add a println to show the value of hashTable?
    If it is null, then find out why it is null.
    I changed it to this:
    Java Code:
    	public void Insert(String S) {
    		location = HashValue(S);
    		System.out.println("Location " + location);
    		hashTable[location] = S;
    		NumEntries();
    	}
    Enter a word
    cat
    ch1 116, ch2 97, ch3 99
    Chunk value: 104596
    Location 61
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 61
    at Hash.Insert(Hash.java:19)
    at TestHashFunction.main(TestHashFunction.java:20)

    So I fixed the NullPointerException but there is an ArrayIndexOutofBoundsException. hashTable is set to size 101 by default so position 61 shouldn't be out of bounds.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Hash Table problem with null pointer exception, need help

    Which statement is at line 19?

    To find the length of the hashTable print its length value.
    chris1 likes this.

  9. #9
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Re: Hash Table problem with null pointer exception, need help

    The hashTable length was 0 but I was able to solve it. I had to change the
    hashTable = new String [size]; to String [] hashTable = new String[size];

    Thanks for helping me out.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Hash Table problem with null pointer exception, need help

    Does that mean that you have two versions of the same variable? One at the class level and one local.
    That will be a problem.

  11. #11
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Re: Hash Table problem with null pointer exception, need help

    Java Code:
    public class Hash<T> implements HashTableInterface<T> {
    
    	int numEntries, size, location, hashVal, index;
    	String word;
    	String[] hashTable = new String[101];
    
    	public void HashTable() {
    		String[] hashTable = new String[101];
    	}
    
    	public void HashTable(int n) {
    		String[] hashTable = new String[size];
    	}
    That is my code for setting the length of hashTable.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Hash Table problem with null pointer exception, need help

    The hashTable variables on line 8 and line 12 are local to those methods and will NOT exist when those methods exit.
    The variable on line 5 will exist for the life of the class.

  13. #13
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Re: Hash Table problem with null pointer exception, need help

    I figured it out. I had the HashTable() methods in the interface so therefore when I was creating the hashTable in my test class it was never initializing the length of the hashTable. It now works correctly. Thanks for the help.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Hash Table problem with null pointer exception, need help

    See you when you get to the next problem.

Similar Threads

  1. null pointer exception problem
    By dylandrop in forum New To Java
    Replies: 12
    Last Post: 04-02-2011, 07:30 PM
  2. Problem with null pointer exception?
    By dunafrothint in forum AWT / Swing
    Replies: 3
    Last Post: 11-11-2009, 03:34 PM
  3. Integration Problem-Null pointer Exception
    By uhdam in forum AWT / Swing
    Replies: 0
    Last Post: 04-07-2009, 11:46 AM
  4. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •