• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing a GUI  RSS feed

 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having trouble printing my JFrame with a printerJob. I have been trying to figure this our for awhile on my own, so this is my last resort. If someone could at least point me in the right direction to get answers, that would be great.
Thanks in advance.

P.S. This slice of code keeps giving me a NullPointerException on the call to budgetFrameTest2.BudgetFrame.printAll(g)

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then your object that you're trying to use is null and the error has little to do with the code you've posted. Time to do a little debugging to figure out why it's null.
 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am self-admittedly not a great programmer due to my inexperience, but I never cease to be amazed with my fellow programmers' abilities in pointing out the obvious.
What I know of people, is that when they ask a question, they are asking for possible ideas of what could be wrong, or to be at least pointed in the right direction so that they may find the answer themselves.

Like I said Pete, I am not by any means a good programmer, but a reiteration of the problem is definitely not going to help in the least. Do you have any ideas why my object may be returning "null"?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the stack trace?
 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete and Rob,

Okay, so I fixed the problem, but I fixed it in a way that still leaves me with questions.

Basically, I have two classes, a MAIN class to run everything, and a class that holds all of the contents. In the MAIN class I create an instance of the content class called BudgetFrame. When referencing the BudgetFrame from the print() method directly, it gave me a NullPointerException. However, when I create a static variable in the MAIN class and then make the staticVariable = BudgetFrame, and reference the staticVariable from the print method, it works fine. Any ideas on why this works?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not without some more code and the stack trace.
 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,

Here is the stack trace when the program does not work:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Budget.budgetFrame2$printHandler.print(budgetFrame2.java:635)
at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1786)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1335)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1197)
at Budget.budgetFrame2$printHandler.actionPerformed(budgetFrame2.java:648)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
BUILD SUCCESSFUL (total time: 1 minute 16 seconds)

Here is the code in the MAIN class when it produces the stack trace above:




Here is the code in the MAIN class when it does work. Notice the variable assignment line (BudgetFrameA = BudgetFrame - the last line) that is not present in the error producing code:



Here is the code in the "content" class that will not work: (notice the lines budgetFrameTest2.BudgetFrame.printAll(g);)




Here is the code in the "content" class that will work: (notice the lines budgetFrameTest2.BudgetFrameA.printAll(g);)




Why the heck does it matter if I plug the object instance BudgetFrame into a variable named BudgetFrameA and reference the variable? Why can I not just reference the object instance directly instead of plugging it into a variable? This seems like alot of busy work to me.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Hoyle wrote:Like I said Pete, I am not by any means a good programmer, but a reiteration of the problem is definitely not going to help in the least. Do you have any ideas why my object may be returning "null"?


I wasn't trying to be a pain in the neck, but based on what you posted initially, I had no idea. Like I said, the problem wasn't in the code you posted. My suggestion to do debugging was valid. If you use an IDE, you have a debugger.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line in your budgetFrame2 class worries me:



You appear to be calling a print on a static variable of the testing class, and you shouldn't do this. budgetFrame2 and associated classes shouldn't call methods on static variables in a testing class and in fact should know nothing about the testing class. Instead if you want to print the current GUI then perhaps you want to call the print method on the active JFrame itself. How you get a reference to it will depend on the structure of your program which we are not fully privy to.
 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Stein,

I wasn't sure if you were initially just being a smartass or what. My suspicion was that you were in fact not being a smartass, but it just seem to come off that way. I am not a person that is really interested in throwing insults back and forth, so I apologize.
Back to the problem at hand. Why is it a bad idea to reference a static variable in the testing class?

Also, what are some ways to reference the the active JFrame itself? Are there any good links you could recommend? Thanks in advance.

-Tim Hoyle
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Hoyle wrote: I wasn't sure if you were initially just being a smartass or what. My suspicion was that you were in fact not being a smartass, but it just seem to come off that way. I am not a person that is really interested in throwing insults back and forth, so I apologize.

No problem, and ditto. My goal was to get you to debug your code so you could ask an answerable question. Sometimes this means running your code in a debugger, and other times it's as simple as sprinkling println statements throughout your code.

Back to the problem at hand. Why is it a bad idea to reference a static variable in the testing class?

1) You basically have a main class which you are running in a test class. The test class is what it says it is ... simply a class that demonstrates the functionality of your program, and as such it shouldn't play an integral part of your program. The main program should know nothing about the test program and shouldn't really care if it exists or not.
2) Be suspicious about any time you use static anything unless it is your static void main method or a constant expression. Otherwise you may be at risk breaking OOPs rules. Corollary: use static sparingly and only for good reasons.

Also, what are some ways to reference the the active JFrame itself? Are there any good links you could recommend?

This depends on your program, and perhaps smarter minds will be able to discern how you should do it based on the information as presented, but based on my review of your code, I can't tell you the best way. You may wish to show more code, or better still, distill your code into an SSCCE, a small program that we can compile and run and that demonstrates your problem.

Edit: for instance, one solution would be to give the PrintHandler class (note that class names should begin with a capital letter) a JFrame field that is set by either a constructor paramter or a setter method, and then call printAll() on this field. e.g.,


Much luck.

Respectfully yours,

Pete
 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete,

Thanks for the remarks. Though this project is for an app. to handle my personal budget, I am going to school online for software engineering, and as a result I do not get much of a chance to interact with other programmers to talk about problems and programming rules.

In fact, I do not know any programmers personally, other than the ones I chat with online.

Regards,

Tim Hoyle
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!