链表数组上的空指针异常

时间:2015-04-27 22:30:05

标签: java

程序运行:从文件中获取单词给它们一个哈希值,如果发生冲突则将它们放入数组中在数组中创建一个列表。

当我打印一个相互冲突的列表时,我可以将它们插入并半打印正确,它只打印最后插入的列表。问题是当我打印整个数组时,我得到了一个空指针异常停止。

这是我从链表移动到链表的代码,以及我收到错误的地方:

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

错误:

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

我不明白为什么我会继续这样做。

完整代码:

WordList类 https://www.dropbox.com/s/6a4pk7kt2cllgga/WordList.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

词类 https://www.dropbox.com/s/hzrksrdwhbiw6on/Word.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

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

Access to an object's fields is only permitted within its methods.

- 非常感谢帮助。

0 个答案:

没有答案
相关问题