Я пытаюсь отполировать программу, которая смоделирована после линейного алгоритма хэширования. Я, однако, не понимаю, почему я сейчас получаю NullPointerException
на этой линии. Я изменил все, о чем я могу думать, но ничего не работает.NullPointerException в линейной хеширующей программе
Отпечаток:
Exception in thread "main" java.lang.NullPointerException
at Directory.addKey(Directory.java:13)
at Drive.main(Drive.java:37)
И код:
ПРИВОД
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(){
}
}
Это не полный стек, пожалуйста, отправьте его полностью, потому что это информация. – fonZ