File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JButton not displaying, throwing NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton not displaying, throwing NullPointerException" Watch "JButton not displaying, throwing NullPointerException" New topic

JButton not displaying, throwing NullPointerException

Justin Dutch

Joined: Aug 12, 2013
Posts: 5
Trying to display buttons through a method. Keep getting NullPointerException at line 41. What am I doing wrong?

K. Tsang

Joined: Sep 13, 2007
Posts: 1966

Can you determine which of those 4 variables is/are null? Once you do you can solve your own problem.

And welcome to the Ranch.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
Your addOpButton() method doesn't do what you expect it to do. Java uses "pass by value" so only a copy of the button reference is passed to the method. So your variable still remains null. You need to change your code to something like:

I agree. Here's the link:
subject: JButton not displaying, throwing NullPointerException
Similar Threads
urgent: retrieval of data from database to JList
Button and Keyboard linking
What happens to events when an actionlistener is hogging the event dispatch thread
Positioning and size of GUI elements
How to navigate between multiple GUI classes