Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4333327

Linux: PrinterJob.print() throws java.lang.NullPointerException

XMLWordPrintable

    • 2d
    • linux-rc1
    • x86
    • linux
    • Verified



      Name: asR10013 Date: 04/26/2000



          The following code demonstrates unexpeted behavior of method
      PrinterJob.print(). As a result nothing is printed on the paper.

      --------------------- SimplePrint.java -------------------------------
      import java.awt.*;
      import java.awt.print.*;

      public class SimplePrint implements Printable {
        private static Font fnt = new Font("Helvetica",Font.PLAIN,24);
          
        public static void main(String[] args) {
          // Get a PrinterJob
          PrinterJob job = PrinterJob.getPrinterJob();
          // Specify the Printable is an instance of SimplePrint
          job.setPrintable(new SimplePrint());

          try { job.print(); }
          catch (Exception e) { /* handle exception */
      System.out.println(e+" "+e.getMessage());
      e.printStackTrace();
          }
          System.exit(0);
        }

        public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
          if (pageIndex >= 2) return Printable.NO_SUCH_PAGE;
          g.setFont(fnt);
          g.setColor(Color.green);
          g.drawString("Page " + (pageIndex+1), 100, 100);
          return Printable.PAGE_EXISTS;
        }
      }
      -------------------------- output ------------------------------------
      $ java -version
      java version "1.3.0beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0beta-b01)
      Classic VM (build 1.3.0beta-b01, green threads, nojit)
      $ java SimplePrint
      java.lang.NullPointerException null
      java.lang.NullPointerException
              at java.lang.Runtime.exec(Runtime.java:536)
              at java.lang.Runtime.exec(Runtime.java:477)
              at java.lang.Runtime.exec(Runtime.java:443)
              at sun.awt.motif.PSPrinterJob$PrinterSpooler.run(PSPrinterJob.java:673)
              at java.security.AccessController.doPrivileged(Native Method)
              at sun.awt.motif.PSPrinterJob.endDoc(PSPrinterJob.java:718)
              at sun.java2d.RasterPrinterJob.print(RasterPrinterJob.java:355)
              at SimplePrint.main(SimplePrint.java:26)
      ----------------------------------------------------------------------
          
          The same code works fine under Solaris java version "1.3.0rc3" and
      under Linux java version "1.2.2"

      ======================================================================

      ======================================================================

            prr Philip Race
            alssunw Als Als (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: