Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to set printservice but getting a null pointer exception

Status
Not open for further replies.

mes123

Programmer
Jul 29, 2005
62
GB
I'm trying to set the printService for a printerJob but keep getting a null pointer excpetion and I can see what the error is...

Here is the constuctor which does the setup...

Code:
public DocumentRenderer() {
    pFormat = new PageFormat();
    //pJob = PrinterJob.getPrinterJob();
    PrintService service[] = PrinterJob.lookupPrintServices();
    for (int i = 0; i < service.length; i++) {
        System.out.println("\t" +service[i] + " " + i);
    }
    try {
        PrintService pserv = service[3];
        System.out.println("trying to use " + pserv);
        pJob.setPrintService(pserv);
    } catch (PrinterException pe) {
        System.out.println(pe.getMessage());
    }
  }

When its called I get the following output:
Win32 Printer : \\Heather\HP DeskJet 1120C 0
Win32 Printer : Microsoft Office Document Image Writer 1
Win32 Printer : HP LaserJet 4100 PCL 6 2
Win32 Printer : hp LaserJet 1300 PCL 6 3
Win32 Printer : Acrobat PDFWriter 4
trying to use Win32 Printer : hp LaserJet 1300 PCL 6
java.lang.NullPointerException
at printUtils.DocumentRenderer.<init>(DocumentRenderer.java:85)
at printagain.main(printagain.java:29)
Exception in thread "main"


Any ideas?
 
I think pJob (whatever it is and comes from) is null so the line
Code:
pJob.setPrintService(pserv);
throws the exception.

Cheers,
Dian

 
ah, (slaps head) give me a star for daft mistake of the day... line 2 in the code is commented out - so you are right, pJob is null. D'oh.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top