7 Replies Latest reply: Mar 10, 2008 8:44 AM by 807601 RSS

    Printing strings

      I have a problem printing in java.
      Printing simple text trough g.drawString("printing text", x,x); goes fine,
      but whenever I try to get text from a TextField printed I get the nullpointer.
      When I press the print button the strings do have value, but I suspect I need to get the text out of the textfields somewhere different in my application.

      This is what I've got so far, note: printing the typed strings works well, but when printing from my java strings
      it won't work. I have also documented these things in the code below.
          public int print(Graphics g, PageFormat pf, int page) throws PrinterException 
                 strFirstname = txtFirstname.getText();  // Where I get the value from the textfields
                strLastname = txtLastname.getText();
            if (page > 0) 
            {           // We have only one page, and 'page' is zero-based 
                 return NO_SUCH_PAGE;
                Graphics2D g2d = (Graphics2D)g;
                g2d.translate(pf.getImageableX(), pf.getImageableY());
                g.drawString ("First name:", 0, 75); //These lines get printed perfectly.
                g.drawString (strFirstname, 115, 75);
                g.drawString ("Last name:", 0, 100); //This line is getting printed perfectly aswell.
                g.drawString (strLastname, 115, 100);
                return PAGE_EXISTS;
      And the code from the print button:
          private class PrintHandler implements ActionListener
               public void actionPerformed (ActionEvent e)
               PrinterJob job = PrinterJob.getPrinterJob();
                   Book book = new Book();
               PageFormat documentPageFormat = new PageFormat();
               documentPageFormat.setOrientation(PageFormat.LANDSCAPE );
               book.append(new frmModify(), documentPageFormat); // frmModify = the class wich extends the frame and implements PrintAble.
               System.out.println(strFirstname); // The values of the strings get printed perfectly IN THE CONSOLE, not on paper.
               boolean ok = job.printDialog();
               if (ok) 
                   } catch (PrinterException ex) 
                    /* The job did not successfully complete */
        • 1. Re: Printing strings

          Are you using an IDE? if so debug though it and check if strFirstname and strLastname are null... they allmost certainly are.

          If you're not using an IDE then print the values to the console with good 'ole System.out.println statements... just don't forget to remove them before the code goes into production.

          Cheers. Keith.
          • 2. Re: Printing strings

            I have added the
                      System.out.println("First name" +strFirstname);
                      System.out.println("Last name" +strLastname);
            Both to the button handler and to the method print.

            In the button handler it gives the right values.

            In the method print (actually where the strings are being gotten out of the text fields) they give nothing.
            Not "null" no just
            First name
            with nothing behind it.

            I find this very weird because of the fact I am getting the values out of the textfields right above it.
            • 3. Re: Printing strings
              I find this very weird
              Yep. It's weird.

              Ok, try shifting the code which gets the values from the text fields to your "go" button event listener, and pass them to the print method... I believe the best way to tackle passing such things around is to populate a new instance of a transfer object (a simple "bean" class) in the event listener and then pass that to the "doerator" methods... this helps towards seperation of the interface from the rest of the program.

              Cheers. Keith.
              • 4. Re: Printing strings
                This is the exact nullpointer message:
                Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
                     at sun.print.PeekGraphics.drawString(Unknown Source)
                     at frmModify.print(frmModify.java:466)
                     at sun.print.RasterPrinterJob.printPage(Unknown Source)
                     at sun.print.RasterPrinterJob.print(Unknown Source)
                     at sun.print.RasterPrinterJob.print(Unknown Source)
                     at frmModify$PrintHandler.actionPerformed(frmModify.java:624)
                     at java.awt.Button.processActionEvent(Unknown Source)
                     at java.awt.Button.processEvent(Unknown Source)
                     at java.awt.Component.dispatchEventImpl(Unknown Source)
                     at java.awt.Component.dispatchEvent(Unknown Source)
                     at java.awt.EventQueue.dispatchEvent(Unknown Source)
                     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                     at java.awt.EventDispatchThread.run(Unknown Source)
                It probably doesn't help much..
                • 5. Re: Printing strings
                  I might have come closes to the answer but I'm not there yet !

                  When I use this piece of code in my constructor (where the textfield is made)
                  txtFirstname.setText ("Derek");
                  txtLastname.setText ("Jeter");
                  The string in the print method ain't null anymore but it prints correctly "Derek" and "Jeter" in the console.

                  So it might has something to do with the print method being called before the textfields are actually called by my stored procedure.

                  This is how I call the frame:
                                  fmod = new frmModify();
                                  fmod.vernieuwOfficeCC(); // Renewing my choice menu's.
                                  fmod.vernieuwBusinessUnitCC(); // Renewing my choice menu's
                                  fmod.showUser(oFirstname, oLastname, oJobtitle, oDepartment, oPersref, oOffice, oBusinessUnit); //This is where they fill the text fields with data.
                                  fmod.openWindow(); //Method with setVisible(true);
                  Actually it has to be in the way I call the frame and methods. I just can't figure it out at the moment.
                  Because when the constructor frmModify() is called the text fields are empty because they won't get filled until showUser();.
                  Any idea how I fix this ?

                  Edited by: Nimzor on Mar 10, 2008 6:08 AM
                  • 6. Re: Printing strings
                    so move your print to a place where it won't run until after you initialize the textfields...
                    • 7. Re: Printing strings
                      Just need to figure out where to move it :)-

                      Edited by: Nimzor on Mar 10, 2008 6:44 AM