I am using the BZip2 compression library from Apache Ant. The documentation is given at –
http://javadoc.haefelinger.it/org.apache.ant/1.7.1/org/apache/tools/bzip2/CBZip2InputStream.html
I keep getting this error when I run the code –
Exception in thread "main" java.lang.NullPointerException
at org.apache.tools.bzip2.CBZip2InputStream.bsR(CBZip2InputStream.java:323)
at org.apache.tools.bzip2.CBZip2InputStream.bsGetUChar(CBZip2InputStream.java:341)
at org.apache.tools.bzip2.CBZip2InputStream.initBlock(CBZip2InputStream.java:230)
at org.apache.tools.bzip2.CBZip2InputStream.<init>(CBZip2InputStream.java:178)
at org.apache.tools.bzip2.bzipCompression.main(bzipCompression.java:37)
The error is at line –
**CBZip2InputStream cin = new CBZip2InputStream(fileInputStream);**
My source code is –
public static void main(String[] args) throws IOException, NullPointerException {
// TODO Auto-generated method stub
FileReader inFile = new FileReader("alice29.txt");
BufferedReader buff = new BufferedReader(inFile);
FileOutputStream fout = new FileOutputStream("alicea.txt");
fout.write("BZ".getBytes());
CBZip2OutputStream czout = new CBZip2OutputStream (fout);
String message = buff.readLine(); // read first line
while(message!=null){
byte[] input = message.getBytes();
czout.write(input);
message = buff.readLine();
}
czout.flush();
czout.close();
InputStream fileInputStream = new FileInputStream("alicea.txt");
CBZip2InputStream cin = new CBZip2InputStream(fileInputStream);
FileOutputStream decOut = new FileOutputStream("decompressedAlice.txt");
byte[] buf = new byte[100000];
int len;
while((len = cin.read(buf))>0){
decOut.write(buf, 0, len);
}
decOut.close();
cin.close();
}
Thank you so much for your help.
You are opening a FileOutputStream to alicea.txt
This OutputStream is never closed in your code, but in the error-Line you open a FileInputStream on the same file:
IMHO while you have an open FileOutputStream on a file, you can not open a file InputStream on the same file. That may be the reason, why your FileInputStream is NULL and the exception is thrown.
Update:
There is a known bug in the CBZip2InputStream and a solution described in the year 2002 (still can be found at MarkMail), which still exists. You have to download the java-classes and may not use a ready build .jar archive. Change the following lines in the initialize() method of CBZip2InputStream.java:
Original code (starting at line 213):
Change this to:
Last Update:
I just reviewed the svn of apache ant; the bug in trunk is fixed. But despite that fact, there are many, many downloads still containing this bug. I also downloaded just an old version accidently to test your problem.