[ACCEPTED]-Why am I getting a NullPointerException when trying to read a file?-itext
Sounds like the resource probably doesn't 19 exist with that name.
Are you aware that 18 Class.getResourceAsStream()
finds a resource relative to that class's 17 package, whereas ClassLoader.getResourceAsStream()
doesn't? You can use a 16 leading forward slash in Class.getResourceAsStream()
to mimic this, so 15
Foo.class.getResourceAsStream("/bar.png")
is roughly equivalent to
Foo.class.getClassLoader().getResourceAsStream("bar.png")
Is this actually a file 14 (i.e. a specific file on the normal file 13 system) that you're trying to load? If so, using 12 FileInputStream
would be a better bet. Use Class.getResourceAsStream()
if it's a resource 11 bundled in a jar file or in the classpath 10 in some other way; use FileInputStream
if it's an arbitrary 9 file which could be anywhere in the file 8 system.
EDIT: Another thing to be careful 7 of, which has caused me problems before 6 now - if this has worked on your dev box 5 which happens to be Windows, and is now 4 failing on a production server which happens 3 to be Unix, check the case of the filename. The 2 fact that different file systems handle 1 case-sensitivity differently can be a pain...
Are you checking to see if the file exists 3 before you pass it to readFilesInBytes()
? Note that Class.getResourceAsStream()
returns 2 null
if the file cannot be found. You probably 1 want to do:
private static byte[] readFilesInBytes(String file) throws IOException {
File testFile = new File(file);
if (!testFile.exists()) {
throw new FileNotFoundException("File " + file + " does not exist");
}
return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file));
}
or better yet:
private static byte[] readFilesInBytes(String file) throws IOException {
InputStream stream = TestConversion.class.getResourceAsStream(file);
if (stream == null) {
throw new FileNotFoundException("readFilesInBytes: File " + file
+ " does not exist");
}
return IOUtils.toByteArray(stream);
}
This class reads a TXT file in the classpath 6 and uses TextConversion to convert to PDF, then 5 save the pdf in the file system.
Here TextConversion 4 code :
package convert.pdf.txt;
//Conversion to PDF from text using iText.
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import convert.pdf.ConversionToPDF;
import convert.pdf.ConvertDocumentException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class TextConversion implements ConversionToPDF {
public byte[] convertDocument(byte[] documents) throws ConvertDocumentException {
try {
return this.convertInternal(documents);
} catch (DocumentException e) {
throw new ConvertDocumentException(e);
} catch (IOException e) {
throw new ConvertDocumentException(e);
}
}
private byte[] convertInternal(byte[] documents) throws DocumentException, IOException {
Document document = new Document();
ByteArrayOutputStream pdfResultBytes = new ByteArrayOutputStream();
PdfWriter.getInstance(document, pdfResultBytes);
document.open();
BufferedReader reader = new BufferedReader( new InputStreamReader( new ByteArrayInputStream(documents) ) );
String line = "";
while ((line = reader.readLine()) != null) {
if ("".equals(line.trim())) {
line = "\n"; //white line
}
Font fonteDefault = new Font(Font.COURIER, 10);
Paragraph paragraph = new Paragraph(line, fonteDefault);
document.add(paragraph);
}
reader.close();
document.close();
return pdfResultBytes.toByteArray();
}
}
And here the code to ConversionToPDF 3 :
package convert.pdf;
// Interface implemented by the conversion algorithms.
public interface ConversionToPDF {
public byte[] convertDocument(byte[] documentToConvert) throws ConvertDocumentException;
}
I think the problem come from my file system 2 (devbox on windows and server is Unix). I 1 will try to modify my classpath.
This problem may be caused by calling methods 4 on test.txt
, which can be a folder shortcut. In 3 other words, you're calling a method on 2 a file that doesn't exist, resulting in 1 a NullPointerException
.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.