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

NullPointerException occurs in beening invoked "drawString" method on printing.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.0
    • client-libs
    • None
    • beta
    • sparc
    • solaris_2.6

       (1) Working environments
       OS : Solaris2.6
       JDK : JDK1.1.8, JDK1.2 fcs
       Window : CDE
       Japanese Input : Atok7, Atok8
       
       (2) Symptom

      When "drawString" method has been invoked without setting a font to the
      graphics gotten by "getGraphics" method of "PrintJob" class,
      NullPointerException occurs.

      Note: This exception doesn't occur in displaying the character string
      on the frame drawn by "drawString" method.

      ---------------<TestPrint.java>----------------

      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;

      public class TestPrint extends Frame {

      public TestPrint () {

      MenuBar mb = new MenuBar();
      Menu m = new Menu ("File");
      MenuItem mi = new MenuItem ("Print");
      mi.addActionListener (
      new ActionListener () {
      public void actionPerformed ( ActionEvent evt) {
      print ();
      }
      });

      m.add (mi);
      mb.add(m);
      setMenuBar(mb);

      addWindowListener (
      new WindowAdapter () {
      public void windowClosing (WindowEvent e) {
      System.exit(0);
      }
      });
      }

      public void print () {

      // get the screen's specifics
      int resolution = getToolkit().getScreenResolution();

      // get the printer's resolution specifics
      PrintJob pjob = getToolkit().getPrintJob (this, "testing", null);

      if (pjob != null) {
      Graphics pg = pjob.getGraphics ();
      if (pg != null) {
      print(pg);
      }
      pjob.end();
      }
          }
         
          public void paint (Graphics g) {
               int resolution = getToolkit().getScreenResolution();
      // draw a 4"x4" rectangle: (4*resolution) x (4*resolution)

      g.translate(getInsets().left + 20, getInsets().top +10);

      g.drawRect (0, 0, 4 * resolution, 4 * resolution);

      // this is an error position
      g.drawString ("This is a testprint",20, 20);

      }

      public static void main (String[] args) {
      Frame f = new TestPrint ();
      f.setSize (420, 420);
      f.show();
      }
      }

      --------------<TestPrint.java end>----------------

      --------------<the content of the Exception>------
      Exception occurred during event dispatching:
      java.lang.NullPointerException
              at sun.awt.SunToolkit.getFontMetrics(Compiled Code)
              at sun.awt.motif.MToolkit.getFontMetrics(Compiled Code)
              at sun.awt.motif.PSGraphics.getFontMetrics(Compiled Code)
              at sun.awt.motif.PSPrintStream.sendString(Compiled Code)
              at sun.awt.motif.PSGraphics.drawString(Compiled Code)
              at TestPrint.paint(Compiled Code)
              at java.awt.Component.print(Compiled Code)
              at java.awt.Container.print(Compiled Code)
              at TestPrint.print(Compiled Code)
              at TestPrint$1.actionPerformed(Compiled Code)
              at java.awt.MenuItem.processActionEvent(Compiled Code)
              at java.awt.MenuItem.processEvent(Compiled Code)
              at java.awt.MenuComponent.dispatchEventImpl(Compiled Code)
              at java.awt.MenuComponent.dispatchEvent(Compiled Code)
              at java.awt.EventQueue.dispatchEvent(Compiled Code)
              at java.awt.EventDispatchThread.run(Compiled Code)

      --------------<the content of the Exception end>------


      Strangely, a sentence, "Font f = g.getFont();" , is only added before the
      sentence include "drawString" in "paint" method, so this exception doesn't
      occur.

      I suspect that this is a bug, and that it is inconvenient to have to set
      a font to the graphics gotten by "getGraphics" method of "PrintJob" class
      before "drawString" method.


      --------------<"paint" method in TestPrint.java>--------

      public void paint (Graphics g) {
      int resolution = getToolkit().getScreenResolution();
      // draw a 4"x4" rectangle: (4*resolution) x (4*resolution)

      g.translate(getInsets().left + 20, getInsets().top +10);

      g.drawRect (0, 0, 4 * resolution, 4 * resolution);

      Font f = g.getFont(); //add

      g.drawString ("This is a testprinti",20, 20);
      }

      --------------<"paint" method in TestPrint.java end>--------

            rraysunw Richard Ray (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: