Reputation: 169
I am attempting to polish off a program that is modeled after a linear hashing algorithm. I am, however, stumped at why I am currently getting a NullPointerException
on this line. I have changed everything I can think of, but nothing works.
The printout is:
Exception in thread "main" java.lang.NullPointerException
at Directory.addKey(Directory.java:13)
at Drive.main(Drive.java:37)
And the code:
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(){
// }
}
Directory
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(){
}
}
Upvotes: 0
Views: 127
Reputation: 15434
When you created array of bucket nodes - it contains null
s. You need to initialize each element in array:
directory = new BucketNode[8];
for (int i = 0; i < 8; i++) {
directory[i] = new BucketNode();
}
It's ok for array of primitives like int,boolean,char, etc because elements set to default values like 0,false,0. If you have array of objects - default value if null
.
Upvotes: 2