java itext catching null exception pdf text extraction - java

When extracting text form pdf using itext 5.3.4 using this code:
try {
reader = new PdfReader(thepdffilename);
} catch (IOException e) {
openok=false;
}
if (openok==true){
int numberOfPages = reader.getNumberOfPages();
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
for (int page = 1; page <= numberOfPages; page++){
try {
SimpleTextExtractionStrategy strategy = parser.processContent(page, new SimpleTextExtractionStrategy());
content = content + strategy.getResultantText();
} catch (Throwable t) {
crap=true;
break;
}
}
reader.close();
}
However occasionally GooglePlay crashes & ANRs reports that there has been a NP exception in itext.
java.lang.NullPointerException in com.itextpdf.text.pdf.PdfReader$PageRefs.readPages at
com.itextpdf.text.pdf.PdfReader$PageRefs.readPages(PdfReader.java:3382) at
com.itextpdf.text.pdf.PdfReader$PageRefs.<init>(PdfReader.java:3350) at com.itextpdf.text.pdf.PdfReader$PageRefs.<init>(PdfReader.java:3328) at
com.itextpdf.text.pdf.PdfReader.readPages(PdfReader.java:1003) at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:530) at
com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:170) at
com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:159)
The 5.3.4 source code at line 3382 is:
http://grepcode.com/file/repo1.maven.org/maven2/com.itextpdf/itextpdf/5.3.4/com/itextpdf/text/pdf/PdfReader.java?av=f
3374 void readPages() throws IOException {
3375 if (refsn != null)
3376 return;
3377 refsp = null;
3378 refsn = new ArrayList<PRIndirectReference>();
3379 pageInh = new ArrayList<PdfDictionary>();
3380 iteratePages((PRIndirectReference)reader.catalog.get(PdfName.PAGES));
3381 pageInh = null;
3382 reader.rootPages.put(PdfName.COUNT, new PdfNumber(refsn.size()));
3383 }
3384
3385 void reReadPages() throws IOException {
3386 refsn = null;
3387 readPages();
3388 }
So something is going wrong when certain pdf files are having their text extracted and the reason why that could be happening is probably never going to be sorted as I do not have the pdfs in question.
What I require is a method of catching the NP exception so my app does not crash.
I've tried
} catch (Exception e) {
and as a last resort to try and catch any exception
} catch (Throwable t) {
Does anyone have an idea how I can get this particular itext error to be caught?
thanks

If I understand you correctly, your attempts to catch that NPE have been made in your loop through the document pages:
for (int page = 1; page <= numberOfPages; page++){
try {
SimpleTextExtractionStrategy strategy =
parser.processContent(page, new SimpleTextExtractionStrategy());
content = content + strategy.getResultantText();
} catch (Throwable t) {
crap=true;
break;
}
}
If you look closely at your Exception, though...
java.lang.NullPointerException in com.itextpdf.text.pdf.PdfReader$PageRefs.readPages at
com.itextpdf.text.pdf.PdfReader$PageRefs.readPages(PdfReader.java:3382) at
[...]
com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:159)
you see that the exception already occurs in the PdfReader construction (PdfReader.<init>). Thus, you have to catch the NPE already where you construct your PdfReader:
try {
reader = new PdfReader(thepdffilename);
} catch (IOException e) {
openok=false;
} catch (NullPointerException npe) { // !!
openok=false; // !!
}
Or if you want to take no chances
try {
reader = new PdfReader(thepdffilename);
} catch (Throwable t) { // !!
openok=false;
}
If you have other code locations, too, in which a PdfReader is constructed, you may want to harden them, too.
#BrunoLowagie This NPE had better be transformed to a tagged exeption, hadn't it?

This is ugly but if you really want to catch it , try and catch RuntimeException

Related

How could I deal with this custom Exception?

I have this bit of code which depends from a custom Exception thrown by a function inside findID() it throws a NoClientFound Exception that I made whenever this mentioned function returns a null (The client does not exist).
The IDE suggests that I shall apply that Exception into the code, but in this bit of code, where I need the ID to be null (unique IDs) I "can't catch that exception" since if I catch it, the function will not be executed as intended.
Question: How I can manage this?
Function with the Exception problem
public boolean add(Client c) {
StringBuilder sb = new StringBuilder();
boolean added = false;
try {
if (findID(c.getID()) == null) {
try (BufferedWriter bw = new BufferedWriter(
new FileWriter(fitxer, true));) {
//Add client to file
bw.write(sb.append(c.getID()).append(SEPARADOR).
append(c.getName()).toString());
bw.newLine();//New line
bw.flush(); //Push to file
added = true;
} catch (IOException e) {
Logger.getLogger(DaoClient.class.getName()).log(Level.SEVERE,
null, "Error appeding data to file" + e);
}
}
} catch (IOException ex) {
Logger.getLogger(DaoClient.class.getName()).log(Level.SEVERE, null,
"Error appeding data to file" + ex);
} finally {
}
return addded;
}
Exception Code
public class NoClientFound extends Exception {
private String msg;
public NoClientFound() {
super();
}
public NoClientFound(String msg) {
super(msg);
this.msg = msg;
}
#Override
public String toString() {
return msg;
}
You can catch that exception and handle it accordingly. When you catch NoClientFound exception that means findID(c.getID()) is null. So without handling that in the if block you can handle that within the catch block.
public boolean add(Client c) {
StringBuilder sb = new StringBuilder();
boolean added = false;
try {
// call the function
findID(c.getID());
} catch (NoClientFound ex) {
//handle the NoClientFound exception as you like here
BufferedWriter bw = new BufferedWriter(
new FileWriter(fitxer, true));
//Add client to file
bw.write(sb.append(c.getID()).append(SEPARADOR).
append(c.getName()).toString());
bw.newLine();//New line
bw.flush(); //Push to file
added = true;
}catch (IOException ex) {
Logger.getLogger(DaoClient.class.getName()).log(Level.SEVERE, null,
"Error appeding data to file" + ex);
}finally {
}
return addded;
}
I assume you already have a null check on findID(...)
if( c == null || findID(c.getID()) == null){
throw new NoClientFound("Client not found!");
}else{
//add your file writing operation
}
and Also in NoClientFound class extend it from RuntimeException, not the Exception.
public class NoClientFound extends RuntimeException {
...
}
Caller method:
public void caller(){
Client client = new Client();
client.setId(1);
...
try{
add(client);
}catch(NoClientFound ex){
//client not found then create one for ex...
}
catch(Exception ex){
//somthing else happend
log.error(ex.getmessge());
}
}

How can I resume code where I left off after FILENOTFOUNDEXCEPTION is thrown

I am writing a file into a directory. There might be the chance that the directory becomes unreachable.
What I want to do is..
As the code is writing to the file, if the directory becomes unreachable or a file not found exception is thrown I want it to keep checking if the directory exists and continue where I left off after the directory exists again.
After some time if the directory does not come back up then I would shut the program down.
My problem is that when a file not found exception is thrown the program just shuts down all together. Here is my code:
public class BusStopsProcessor implements Runnable
{
private BlockingQueue<Bus<buses>> busQueue;
private Bus<buses> BusObject;
public BusStopsProcessor(BlockingQueue<Bus<buses>> busQueue)
{
this.busQueue = busQueue;
}
#Override
public void run()
{
try
{
String path = "C:\\Users\\Me\\Documents\\";
File file = new File(path + "busStopsFile.txt");
FileWriter fw = new FileWriter(file, true);
CSVWriter writer = new CSVWriter(fw, '|', CSVWriter.NO_QUOTE_CHARACTER);
while(true)
{
BusObject = busQueue.take();
//each bus object should have a bus date if it does not then it is a
//poison bus object.
if(BusObject.getBusDate() != null)
{
createBusFile(BusObject, writer);
else
{
try
{
//Finished processing bus stops so close writer.
writer.close();
fw.close();
break;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
e.printStackTrace();
}
catch (FileNotFoundException e)
{
//If a FILENOTFOUND exception is thrown here I want
//my code to be able to pick up where I left off
e.printStackTrace();
logger.warn(e.getMessage());
}
catch (IOException e)
{
e.printStackTrace();
}
}
Here is the method that I want to keep checking if the directory exists. If the file is being written into and all of a sudden the directory goes down. I dont want to repeat the same information in the file I want it to continue to write from where it left off but I just cant figure out how to do that. thank you.
private void createBusFile(Bus<buses> aBusObject, CSVWriter writer) throws InterruptedException
{
//Get bus information here
for(Bus<buses> busStop : aBusObject.getBusStops())
{
busNumber = busStop.getBusNumber();
busArrivalTime = busStop.getBusArrivalTime();
busStop = busStop.getBusStop();
String[] busFields = {busNumber, busDate, busStop};
//If a file not found exception is thrown here I want it to keep checking if the directory exists. And pick up from where I left off
writer.writeNext(busFields);
}
}
}

Java InputStream with different Object Classes

my code has to read in two different Object Types (Bestellung, AKunde) through a ObjectOutputStream and save it in a csv file, which works.
But when i try to read them from the file it doesn't work.
Here is the code:
OutputStream:
LinkedList<Bestellung> bestellListe = verwaltungBestell.getBestellListe();
try {
fileOutputStream = new FileOutputStream(file);
outputStream = new ObjectOutputStream(fileOutputStream);
for (AKunde kunde : kundenliste) {
outputStream.writeObject(kunde);
}
for (Bestellung bestellung : bestellListe) {
outputStream.writeObject(bestellung);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream:
ArrayList<AKunde> kundenImport = new ArrayList<AKunde>();
ArrayList<Bestellung> bestellungenImport = new ArrayList<Bestellung>();
boolean cont = true;
try {
ObjectInputStream objectStream = new ObjectInputStream(new FileInputStream(directorie));
while (cont) {
AKunde kunde = null;
try {
kunde = (AKunde) objectStream.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (kunde != null) {
kundenImport.add(kunde);
} else {
cont = false;
}
}
while (cont) {
Bestellung bestellung = null;
try {
bestellung = (Bestellung) objectStream.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (bestellung != null) {
bestellungenImport.add(bestellung);
} else {
cont = false;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
}
But it won't read the "Bestellungen" and won't save them into "bestellungenImport".
Anyone has a solution???
Your code never reaches the Bestellung reader part.
You have a false assumption that kunde =(AKunde)objectStream.readObject(); returns null.
Instead, it throws exception.
Oneway you can do is cast it like #luk2302.
Another way is to add a object count when writing your object stream:
outputStream.writeInt(kundenliste.size());
for (AKunde kunde : kundenliste) {
outputStream.writeObject(kunde);
}
outputStream.writeInt(bestellListe.size());
for (Bestellung bestellung : bestellListe) {
outputStream.writeObject(bestellung);
}
Then replace your while(cont) loop with a for each loop:
int kundeCount = objectStream.readInt();
for (int i = 0; i < kundeCount; i++) {
// Read and import kunde
}
You need to change your logic for reading objects. There are two main issues:
you never reset cont so the second while loop will never do anything
even if you did that you would always skip the first Bestellung since it was already read when the second loop is reached
I would propose something along the lines of:
Object object = objectStream.readObject();
if (object instanceof AKunde) {
kundenImport.add((AKunde) object);
} else if (object instanceof Bestellung) {
bestellungenImport.add((Bestellung) object);
} else {
// something else was read
}
You simply need to loop over this code and add proper error handling where needed.
I would suggest, you change the way you write your objects to ObjectOutputStream in the first place:
Directly write the kundenListe and bestellListe objects, so you dont't have to worry about types or number of elements when reading the objects again. Your stream of object then always contains two objects, the two lists.
// use try-with-resources if you're on Java 7 or newer
try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file))) {
// write the complete list of objects
outputStream.writeObject(kundenliste);
outputStream.writeObject(bestellListe);
} catch (IOException e) {
e.printStackTrace(); //TODO proper exception handling
}
Then you could read it just like that:
ArrayList<AKunde> kundenImport = new ArrayList<>();
ArrayList<Bestellung> bestellungenImport = new ArrayList<>();
//again try-with-resources
try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file))) {
kundenImport.addAll((List) inputStream.readObject());
bestellungenImport.addAll((List) inputStream.readObject());
} catch (IOException | ClassNotFoundException e) { //multi-catch, if Java 7 or newer
e.printStackTrace(); //TODO proper exception handling
}
Further reads:
The try-with-resources Statement
Catching Multiple Exception Types (...)

Java 8 - effectively final variables, lambdas and try/catch/finally block

So I've started playing with Java 8 streams/lambda expressions and have encountered and interesting issue that I'm not really sure how to resolve. So here I am, asking you for help.
Sample code in question:
public void insertBlankPages(File inputFile, String outputFile, final int OFFSET) {
PDDocument newDocument;
PDDocument oldDocument;
try {
newDocument = createNewDocument();
oldDocument = createOldDocument(inputFile);
List<PDPage> oldPages = getAllPages(oldDocument);
oldPages.stream()
.limit(oldPages.size() - OFFSET)
.forEach(page -> {
newDocument.addPage(page);
newDocument.addPage(new PDPage(page.getMediaBox()));
});
newDocument.save(outputFile);
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
} finally {
newDocument.close();
oldDocument.close();
}
}
With the code above, compiler complains about the call to close() in the finally block. The error is: "Variable newDocument might not have been initialized". Same for oldDocument.
Naturally, I go ahead and initialize the variables as follow:
PDDocument newDocument = null;
PDDocument oldDocument = null;
Now I get compiler error "Variable used in lambda expression should be effectively final".
How to go about this?
Methods createNewDocument and createOldDocument throw an exception, so the calls must be within try/catch block. I also need to close the documents within the finally block.
I should be able to work around this issue by using try-with-resources. But, I'm curious to hear if there's any other proper solution for this.
Thanks.
The proper solution is try-with-resources.
Without that, you'd need to manually nest some more try/finally blocks:
try {
PDDocument newDocument = createNewDocument();
try{
PDDocument oldDocument = createOldDocument(inputFile);
try{
// .....
}
finally { oldDocument.close() }
}
finally{ newDocument.close(); }
}
catch (IOException e) {
e.printStackTrace();
}
Thanks everyone for the ideas. It appears that there isn't a "good" way of doing this without try-with-resources. Here is an example using try-with-resources.
public void insertBlankPages(File inputFile, String outputFile, final int OFFSET) {
try (PDDocument newDocument = createNewDocument();
PDDocument oldDocument = createOldDocument(inputFile)) {
List<PDPage> oldPages = getAllPages(oldDocument);
oldPages.stream()
.limit(oldPages.size() - OFFSET)
.forEach(page -> {
newDocument.addPage(page);
newDocument.addPage(new PDPage(page.getMediaBox()));
});
newDocument.save(outputFile);
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
}
Simple work-around is to use an array, which itself is final so you can reference it in a lambda, but its contents are free to change.
This code is essentially the same as your, but has no compile errors:
public void insertBlankPages(File inputFile, String outputFile, final int OFFSET) {
PDDocument[] newDocument = new PDDocument[1]; // automatically filled with a null
PDDocument oldDocument = null;
try {
newDocument[0] = createNewDocument();
oldDocument = createOldDocument(inputFile);
List<PDPage> oldPages = getAllPages(oldDocument);
oldPages.stream()
.limit(oldPages.size() - OFFSET)
.forEach(page -> {
newDocument[0].addPage(page);
newDocument[0].addPage(new PDPage(page.getMediaBox()));
});
newDocument[0].save(outputFile);
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
} finally {
if (newDocument[0] != null)
newDocument[0].close();
if (oldDocument != null)
oldDocument.close();
}
}

Handle java.nio.file.AccessDeniedException

I'm using this code to read value from a file.
public String getChassisSerialNumber() throws IOException
{
File myFile = new File("/sys/devices/virtual/dmi/id/chassis_serial");
byte[] fileBytes;
String content = "";
if (myFile.exists())
{
fileBytes = Files.readAllBytes(myFile.toPath());
if (fileBytes.length > 0)
{
content = new String(fileBytes);
}
else
{
return "No file";
}
}
else
{
return "No file";
}
return null;
}
I get this error:
java.nio.file.AccessDeniedException: /sys/devices/virtual/dmi/id/chassis_serial
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.Files.readAllBytes(Files.java:3149)
How I can handle this error? Because now I the code stops execution? Is there some better way without interruption the code execution?
You have to use try-catch, either within getChassisSerialNumber() on when calling it. E.g.
try {
getChassisSerialNumber();
} catch (java.nio.file.AccessDeniedException e) {
System.out.println("caught exception");
}
OR
try {
fileBytes = Files.readAllBytes(myFile.toPath());
} catch (java.nio.file.AccessDeniedException e) {
return "access denied";
}
This way your program does not terminate.
For a clean design you should either return null in cases you could not read the file (returning "magic strings like "No file" or "access denied" are no good design, because you cannot differentiate if this string came from the file or not) or catch the exception outside of the method (my first example).
Btw. by just putting the content of the file into the content variable you don't return it (i.e., replace content = new String(fileBytes); with return new String(fileBytes);)
public String getChassisSerialNumber()
{
File myFile = new File("/sys/devices/virtual/dmi/id/chassis_serial");
if (myFile.exists())
{
byte[] fileBytes;
try {
fileBytes = Files.readAllBytes(myFile.toPath());
} catch (java.nio.file.AccessDeniedException e) {
return null;
}
if (fileBytes != null && fileBytes.length > 0)
{
return new String(fileBytes);
}
}
return null;
}
You should catch the exception instead of throwing it. I think that you need to put a try-catch block around the call to the method getChassisSerialNumber.
Something like this should work in your case:
String result = null;
try {
result = getChassisSerialNumber();
} catch (java.nio.file.AccessDeniedException ex) {
// do something with the exception
// you can log it or print some specific information for the user
}
return result; // if the result is null, the method has failed
In order to understand better this kind of things you should have a look to this page

Categories

Resources