[RESOLVED]HELP!!BitSet set() returns null...
Does anyone knows why my fingerprint2 is null when I print it out? Here is my code:
Code:
try
{
FileInputStream input = new FileInputStream ("fp.bin");
DataInputStream binData = new DataInputStream (input);
BitSet fingerprint2 = new BitSet[3]; //my fp.bin file only consists of 2 binaries
for(int i=0;i<3;i++)
{
for(int j=0;j<1024;j++)
{
boolean bit = binData.readBoolean();
fingerprint2[i].set(j,bit); //something wrong here?
}
System.out.println(fingerprint2[i]); //throw NullPointerException
}
binData.close();
input.close();
}
catch(EOFException eof)
{
}
catch(NullPointerException npe)
{
System.out.println("Null Pointer");
}
catch (FileNotFoundException fe)
{
System.out.println("No such file.");
}
catch (IOException ie)
{
System.out.println(ie.toString());
}
Re: [RESOLVED]HELP!!BitSet set() returns null...
I have found a solution for this problem. Here is the code:
Code:
BitSet[] fingerprint2 = new BitSet[3];
for(int i=0;i<3;i++)
{
fingerprint2[i] = new BitSet(); //have to initialise fingerprint2 first
for(int j=0;j<1024;j++)
{
boolean bit = binData.readBoolean();
fingerprint2[i].set(j,bit);
}
System.out.println(fingerprint2[i]);
}
Re: [RESOLVED]HELP!!BitSet set() returns null...
When you allocate an array, the individual members are initialized to their default values. In the case of object references, that's null. You have to allocate each array member individually.
Which is exactly the solution you came up with.