I want to build a file compression using Huffman. When I run the program compress and decompress the file it's OK. I close the program. After closing the program I open the program again. Then I only call the compression file which stores the bit sequence to decompress. But then it shows the null pointer exception. How can I solve it?? Below This are the compression and decompression code. I save the compression bit into a byte array. This works fine. but the problem arises when I only compress the file and close the program. Then if I want to decompress the file by calling the compression file by running the program again it shows me the null pointer exception. the exception is:
Exception in thread "main" java.lang.NullPointerException
at compress.Decode.decode(Decode.java:25)
at compress.MainClasss.main(MainClasss.java:133)
C:\Users\Tech Land\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
public class MainClasss {
public static void main(String[] args) throws IOException {
String s = "";
String test = "";
String decodee = "";
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("pls write the file name");
// String binaryString ="";
String name = scan.next();
try {
test = new String(Files.readAllBytes(Paths.get("C://Users//Tech Land//Desktop//" + name))); //first read the java file as a string
} //first read the java file as a string
catch (IOException e) {
e.printStackTrace();
}
String de = scan.next();
if (de.equals("com")) {
Encode ob = new Encode();
s = ob.encode(test);
BitSet bitset = new BitSet(s.length());
int bitcounter = 0;
for (Character c : s.toCharArray()) {
if (c.equals('1')) {
bitset.set(bitcounter);
}
bitcounter++;
}
FileOutputStream t = new FileOutputStream("C://Users//Tech Land//Desktop//" + name, false);
t.write(bitset.toByteArray());
System.out.println("file successfully compressed");
}
} else if (de.equals("dcom")) {
try {
decodee = new String(Files.readAllBytes(Paths.get("C://Users//Tech Land//Desktop//" + name))); //first read the java file as a string
} //first read the java file as a string
catch (IOException e) {
e.printStackTrace();
}
String binaryString = "";
File file = new File("C://Users//Tech Land//Desktop//" + name);
FileInputStream fin = null;
try {
// create FileInputStream object
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int) file.length()];
fin.read(fileContent);
for (int i = 0; i <= BitSet.valueOf(fileContent).length(); i++) {
if (BitSet.valueOf(fileContent).get(i)) {
binaryString += "1";
} else {
binaryString += "0";
}
}
//System.out.println("");
// System.out.println(""+fileContent.length);
System.out.println("" + binaryString);
} catch (Exception e) {
e.printStackTrace();
}
Decode ddecode=new Decode();
String k=ddecode.decode(binaryString);
FileWriter fw = new FileWriter("C://Users//Tech Land//Desktop//" + name, false);
System.out.println("");
for (int j = 0; j < k.length(); j++) {
fw.write(k.charAt(j));
}
System.out.println("successful");
fw.close();
}
}
}