Problem printing from Applet....

I am having trouble getting printing to work from an applet. My code is JDK 1.1.8 and my browser is Netscape 4.5. My applet is signed and I am getting the Print dialog just fine. However, the actual printing is failing. Please advise me in the errors of my ways...

Here is my printing routine...

public void getData()
   {
      try
      {
         textArea.append("Hello World!!!!!\n");

         PrivilegeManager.enablePrivilege("UniversalPrintJobAccess");

         Toolkit tk = Toolkit.getDefaultToolkit();
         PrintJob pj = tk.getPrintJob(new Frame(), "Print Test", null);

         if (pj == null) return;

         pageWidth = pj.getPageDimension().width;
         pageHeight= pj.getPageDimension().height;

         Graphics g = pj.getGraphics();

         g.drawString(textArea.getText(), 50, 50);

         pj.end();
         g.dispose();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

Here is the error stack trace...

java.lang.NullPointerException
  at sun.awt.windows.WGraphics.drawChars(Compiled Code)
* at sun.awt.windows.WGraphics.drawString(Compiled Code)
  at PrintTest.getData(Compiled Code)
  at PrintTest.start(Compiled Code)
  at netscape.applet.DerivedAppletFrame$StartAppletEvent.dispatch(Compiled Code)
  at java.awt.EventDispatchThread$EventPump.dispatchEvents(Compiled Code)
  at java.awt.EventDispatchThread.run(Compiled Code)
  at netscape.applet.DerivedAppletFrame$AppletEventDispatchThread.run(Compiled Code)


Thanks in Advance!!!

dmanzana

dmanzanaAsked:
Who is Participating?
 
heyhey_Connect With a Mentor Commented:
just another (better) idea
your frame is not Visible so it does not have a peer

replace
   PrintJob pj = tk.getPrintJob(new Frame(), "Print Test", null);
with
Frame f = new Frame();
f.addNotify();
PrintJob pj = tk.getPrintJob(f, "Print Test", null);
0
 
heyhey_Commented:
what happens if you replace
textArea.getText()
with a static String ('Hello')
0
 
dmanzanaAuthor Commented:
Changing textArea.getText() to a static string "Hello" does not change anything. The exact error still occurs.

dmanzana
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dmanzanaAuthor Commented:
No luck. The same problem is happening. I really appreciate your time and effort in helping me with this.

dmanzana
0
 
dmanzanaAuthor Commented:
I have found the problem. The Color and Font within the Graphics were null. As soon as I instantiated a Color and Font and passed them into the Graphics, all is well. Thanks for your efforts. I will give you the points anyway.

dmanzana
0
 
heyhey_Commented:
thanks :)
0
All Courses

From novice to tech pro — start learning today.