• Post Reply Bookmark Topic Watch Topic
  • New Topic

HELP!!!  RSS feed

 
Selecta Zhangy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys im trying to write a swing program to display this window:

http://www.cs.ucl.ac.uk/staff/G.Roberts/images/ex2q3.jpg

When the button is clicked, the text typed into the JTextField at the top of the window is copied into the label in the middle of the window. Note the position and size of the button.


and i have written the code but the program has found a exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Q_3.createBackgroundPanel(Q_3.java:33)
at Q_3.createGUI(Q_3.java:24)
at Q_3.<init>(Q_3.java:15)
at Q_3$2.run(Q_3.java:72)
at java.awt.event.InvocationEvent.dispatch(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)





here is the program i wrote:



i tried so hard to find whats wrong with it, but really need help from you guys...thanks alot!!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stacktrace tells you what method the error occurs in (createBackgroundPanel()) and even what line it occurs on (33), so you really shouldn't have had to search very hard!

Your createBackgroundPanel() method expect the various member variables to have been initialized to point to components already, but you're calling it before calling some of the methods that do that initialization. Therefore those variables are null, and you get this NullPointerException. The solution is just to move the call to createBackgroundPanel() a bit later in time, when all the other initializations are done.
 
Selecta Zhangy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
The stacktrace tells you what method the error occurs in (createBackgroundPanel()) and even what line it occurs on (33), so you really shouldn't have had to search very hard!

Your createBackgroundPanel() method expect the various member variables to have been initialized to point to components already, but you're calling it before calling some of the methods that do that initialization. Therefore those variables are null, and you get this NullPointerException. The solution is just to move the call to createBackgroundPanel() a bit later in time, when all the other initializations are done.


thank you for your reply...i tried but its still not working with the same problem...would you please explain with more details?

thanks a lot!!!
 
Selecta Zhangy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PROBLEM SOLVED!!!

THANKS A LOT!!!
 
Selecta Zhangy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


with the code i wrote in the program, it doesnt paste the words i copied into the label which is in the middle of the backgroundpanel.BIG WHY???
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!