0

I’m trying to get PDFBox to rotate a PDF document 270 from 90 and save.. I have had some help from this community on a few errors as I am new to java. There wasn’t really a good working example that I could try/follow.. I try to run the code and get the following errors:

Exception in thread "main" java.lang.NullPointerException
    at java.io.File.<init>(Unknown Source)
    at PdfRotator.main(PdfRotator.java:36)

Code:

//import java.awt.List;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;


public class PdfRotator {


 private static final String pdfFile = null;

    public static void main(String[] args) throws IOException {

PDDocument document = PDDocument.load("PDFrotatorTEST.pdf");



        //public static void main(String[] args) throws Exception {

 List pages = (List) document.getDocumentCatalog().getAllPages();

 for (int i = 0; i < pages.size(); i++) {
     PDPage page = (PDPage) ((java.util.List) pages).get(i);// PDPage page = (PDPage) pages.get(i);
     if ((i + 1) % 2 == 0) {
         page.setRotation(0); 
     }
 }

 File f = new File(pdfFile);// File f = new File(pdfFile);
 String newFile = f.getParent() + File.separator + "out.pdf";
 try {
    document.save(newFile);
} catch (COSVisitorException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 document.close();
}
}

Is the Catch block right? I’m not sure why I would really need that.. Any help with this is greatly appreciated!

fabian
  • 80,457
  • 12
  • 86
  • 114
javajoejuan
  • 323
  • 3
  • 9
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – fabian Nov 06 '15 at 21:35
  • 1
    `null` is not allowed as parameter for the `File(String)` constructor. – fabian Nov 06 '15 at 21:36
  • Learn to use the debug mode of your IDE, and then single step through your statements, and hover your mouse over variables. What you're doing now is like taking your car to a race but having had only a few driving lessons. The community is of course willing to help you, but I'd suggest you do some tutorial about some java basics. – Tilman Hausherr Nov 06 '15 at 22:06

0 Answers0