线性哈希程序中的NullPointerException

时间:2013-12-05 16:33:09

标签: java nullpointerexception

我试图抛光一个以线性哈希算法为模型的程序。然而,我很难过为什么我目前在这条线上获得了NullPointerException。我已经改变了我能想到的一切,但没有任何作用。

打印输出为:

Exception in thread "main" java.lang.NullPointerException
    at Directory.addKey(Directory.java:13)
    at Drive.main(Drive.java:37)

代码:

DRIVE

import java.io.*;

public class Drive {
    final static int REC_SIZE = 20;
    static int[] keySet = {1, 7, 3, 8, 12, 4, 11, 2, 10, 13, 14, 9};
    final static int INITIAL_FILE_SIZE = keySet.length;
    static Directory dir;


    public static void main(String[] args){
        dir = new Directory(3,0);
        StringBuffer strbuffer = new StringBuffer(REC_SIZE);
        byte[] bytebuffer = new byte[REC_SIZE];

        try {
            File file = new File("c:\\Users\\rcarp_000\\Desktop\\TestResults.bmp");
            RandomAccessFile randomfile = new RandomAccessFile(file, "rw");

            for (int i = 0; i < INITIAL_FILE_SIZE + 10; i++){
                strbuffer = new StringBuffer();
                strbuffer.setLength(REC_SIZE);
                randomfile.writeBytes(strbuffer.toString().replace("\0", "-"));
            }

            for (int i = 0; i < INITIAL_FILE_SIZE; i++){
                int position = (i) * REC_SIZE;
                randomfile.seek(position);

                String key = String.format("%05d", keySet[i]);
                String data = String.format("%15s", "[Data-row-" + i);

                strbuffer = new StringBuffer(key + data);
                strbuffer.setCharAt(REC_SIZE-1, ']');
                strbuffer.setLength(REC_SIZE);

                randomfile.writeBytes(strbuffer.toString().replace("\0", " "));
                dir.addKey(1, i);
                System.out.printf("\nAllocating>>%2d:%2d:%s", i, strbuffer.length(), strbuffer);




            }

            System.out.println("\nDone Allocating\n");
            randomfile.close();

            randomfile = new RandomAccessFile(file, "rw");
            for (int i = 0; i < INITIAL_FILE_SIZE + 10; i++){
                int position = (i) * REC_SIZE;


                randomfile.seek(position);
                randomfile.read(bytebuffer);
                String strRecord = new String(bytebuffer);
                System.out.printf("\nReading %2d:%2d:%s",i, strRecord.length(), strRecord);

            }
            /*if (dir.search(14) == -1){
                System.out.printf("\nError Not Found");
            } else {

                randomfile.seek(10);
                randomfile.read(bytebuffer);
                String strRecord = new String(bytebuffer);
                System.out.printf("Found Key %4d Pos %d Len ");
            }*/

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }

    }

}

BUCKETNODE

public class BucketNode {

    private int BUCKET_SIZE = 2;
    private int avail = 0;

    private int bucketKey[];
    private int bucketFileLocation[];
    private BucketNode next;

    public BucketNode(){
        bucketKey = new int[BUCKET_SIZE];
        bucketFileLocation = new int[BUCKET_SIZE];

    }
    public void add(int key, int fileLoca){
        bucketKey[avail] = key;
        bucketFileLocation[avail] = fileLoca;
        avail++;
    }
    public int getAvail(){
        return avail;
    }
    public int[] getKeys(){
        return bucketKey;
    }
    public int[] getLocs(){
        return bucketFileLocation;
    }
    public void setNext(BucketNode temp){
        this.next = temp;
    }
    public BucketNode getNext(){
        return next;
    }
    //public String showData(){

//  }
}

目录

public class Directory {
    private int m;
    private int s;
    private BucketNode directory[];
    final private int BUCKET_SIZE = 2;

    public Directory(int m, int s){
        this.m = m;
        this.s = s;
        directory = new BucketNode[8];
    }
    public void addKey(int key, int fileLoc){
        directory[key%m].add(key, fileLoc);
        System.out.printf("Key! %d", directory[0].getKeys()[1]);
    }
    public void processOverflow(){

    }
    public void showData(){

    }
}

1 个答案:

答案 0 :(得分:2)

创建数据库节点数组时,它包含null个。您需要初始化数组中的每个元素:

directory = new BucketNode[8];
for (int i = 0; i < 8; i++) {
    directory[i] = new BucketNode();
}

可以使用int,boolean,char等原语数组,因为元素设置为默认值,如0,false,0。如果您有对象数组 - 默认值为null