0

Program run down : take in words from file give them a hash value, place them into an array if collisions occur create a list in the array.

I can get them to be inserted and semi printed correctly for some reason when I print a collided list it only prints the last inserted. The problem is when I print the entire array I get Stopped with a null pointer exception.

This is the code I to move from linked list to linked list, and where I am getting the error:

WordList list = (WordList)arrayString[3];
list.display();

with the error:

Exception in thread "main" java.lang.NullPointerException
at p7b.main(p7b.java:45)

I don't understand why I keep getting this.

COMPLETE CODE:

WordList class https://www.dropbox.com/s/6a4pk7kt2cllgga/WordList.java?dl=0

class WordList{

    public Word firstWord = null;

    public void insert(Word newWord, int hashKey){
        Word prev = null;
        Word curr = firstWord;

        newWord.key = hashKey;

        while(curr != null && newWord.key > curr.key){

        prev = curr;
        curr = curr.next;

        }

        if(prev == null)
            firstWord = newWord;

        else
            prev.next = newWord;
            newWord.next = curr;
    }//end insert

    public String display(){
    if(firstWord == null){
    return "null";
    }
    Word curr = firstWord;

    while (curr != null){

        System.out.println(curr.returnWord() + "    " +curr.returnHashVal());
        curr = curr.next;


    }
    return "done";

    }//end display

Word Class https://www.dropbox.com/s/hzrksrdwhbiw6on/Word.java?dl=0

import java.util.*;


class Word{
protected String theWord;
protected int hashValue;
protected Word next;
protected int key;
public Word(String theWord,int hashValue){
    this.theWord = theWord;
    this.hashValue = hashValue;

}

public String returnWord(){
return theWord;
}

public int returnHashVal(){

return hashValue;
}


}//end class

Main https://www.dropbox.com/s/3o46bp1i3aqanih/p7b.java?dl=0

import java.util.*;
import java.io.*;

public class p7b{
    public static void main(String[] args){
    int[] arrayNums = new int[40];
    Object[] arrayString = new Object[40];

       // Location of file to read
        File file = new File("p7.dat");

        try {

            Scanner in = new Scanner(file);

            while (in.hasNextLine()) {
                String key = in.nextLine(); //READ LINE
                System.out.println("Input: "+key);  //PRNT LINE
        int length = length(key);   //KEY LENGTH
        //System.out.println(length);     //PRNT LENGTH

                int Val = assignVal(0,key);
        //System.out.println("val: "+Val);

        int ValTwo = assignVal(length-1,key);
        //System.out.println("valTwo: "+ValTwo);

        int hashVal = keyVal(Val,ValTwo);
        //System.out.println("hash value: " + hashVal);

        Insert(hashVal,key,arrayNums,arrayString);
        //arrayNums[hashVal] = hashVal;
        //arrayString[hashVal] = key;


         }

            for(int i = 2; i < 38; i++){
        //System.out.println(arrayString[i]+"    "+arrayNums[i]);

        //System.out.println(String.format("Word:%11s-----------Hash Value:%5s ".replace(" ", "ƒ"),
       // arrayString[i], arrayNums[i]).replace(" ", "-").replace("ƒ", " "));

    WordList list = (WordList)arrayString[i];
    list.display();



}
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }//end main
//-------------------------------------------------------------
    public static void Insert(int hashVal, String key,int[] arrayNums, Object[] arrayString){
    Word word = new Word(key,hashVal);
    WordList list = new WordList();

            arrayString[hashVal] = list;
            //System.out.println("here");
            list.insert(word,hashVal);
            //System.out.println("X");
            //list.display();




    }//end Insert


    public static int keyVal(int first,int last){
    int num = first + last;

    return num;
    }//end keyVal

    public static int length(String key){
    int length = key.length();
    return length;
    }//end length   

    public static int assignVal(int position, String key){
    char Hkey = key.charAt(position);


    int Val = 0;
    switch(Hkey) {
        case 'A': Val = 11;
              break;

        case 'B': Val = 15;
              break;

        case 'C': Val = 1;
              break;

        case 'D': Val = 0;
              break;

        case 'E': Val = 0;
              break;

        case 'F': Val = 15;
              break;

        case 'G': Val = 3;
              break;

        case 'H': Val = 15;
              break;

        case 'I': Val = 13;
              break;

        case 'J': Val = 0;
              break;

        case 'K': Val = 0;
              break;

        case 'L': Val = 15;
              break;

        case 'M': Val = 15;
              break;

        case 'N': Val = 13;
              break;

        case 'O': Val = 0;
              break;

        case 'P': Val = 15;
              break;

        case 'Q': Val = 0;
              break;

        case 'R': Val = 14;
              break;

        case 'S': Val = 6;
              break;

        case 'T': Val = 6;
              break;

        case 'U': Val = 14;
              break;

        case 'V': Val = 10;
              break;

        case 'W': Val = 6;
              break;

        case 'X': Val = 0;
              break;

        case 'Y': Val = 13;
              break;

        case 'Z': Val = 0;
              break;


    }//end switch

    return Val;
    }//end assignVal


}//end class

-any help would be great thanks.

Smit
  • 4,685
  • 1
  • 24
  • 28
cmehmen
  • 249
  • 1
  • 3
  • 12
  • 1
    Please edit your question to include the code so that the people helping you don't have to take extra steps to help you. Be sure to format is properly and use the code tags to differentiate the code from normal text. – MarsAtomic Apr 27 '15 at 22:33
  • It fails because not all elements of `arrayString` array are initialized and their default values are nulls. So, `list` variable will be null and you get NullPointerException because of invoking display() method on null reference. – Sergey Alaev Apr 27 '15 at 22:42
  • That even compiles: `WordList list = (WordList)arrayString[3];`?? – YoYo Apr 27 '15 at 22:43
  • @JoD thst was meant to be i sorry I forgot to change that back. I was trying to see if the collisions in the array would display – cmehmen Apr 27 '15 at 22:45
  • So are you sure that the array at that location contains a non-null value? That's *very likely* what's going wrong here. – Makoto Apr 27 '15 at 22:53

0 Answers0