aspose file tools
The moose likes Swing / AWT / SWT and the fly likes NullPointerException on swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "NullPointerException on swing" Watch "NullPointerException on swing" New topic

NullPointerException on swing

Angelo Tan

Joined: Oct 29, 2010
Posts: 10
I believe this is my first post here in the ranch so let me start this by saying hello to you all!

Alright on topic, the below is supposed to be the main page for a class project i'm currently working on. I actually based the this code on an example from a book i'm reading. But then when i tried expanding it, adding several components, i keep on getting the following error:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(
at java.awt.Container.add(
at Panels.<init>(
at Panels.main(

In addition, the above error is the one i get when i try to run the file ( separately, meaning outside of a project.

But then when i try to run the whole project including the same exact code/file (, once i hit on the Login button (which should setVisible(false) the "Login frame", and then call the MainPage constructor so it will appear on screen) this is what i get instead:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(
at java.awt.Container.add(
at MainPage.<init>(
at LoginFrame.actionPerformed(
at javax.swing.AbstractButton.fireActionPerformed(
at javax.swing.AbstractButton$Handler.actionPerformed(
at javax.swing.DefaultButtonModel.fireActionPerformed(
at javax.swing.DefaultButtonModel.setPressed(
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(
at java.awt.Component.processMouseEvent(
at javax.swing.JComponent.processMouseEvent(
at java.awt.Component.processEvent(
at java.awt.Container.processEvent(
at java.awt.Component.dispatchEventImpl(
at java.awt.Container.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.LightweightDispatcher.retargetMouseEvent(
at java.awt.LightweightDispatcher.processMouseEvent(
at java.awt.LightweightDispatcher.dispatchEvent(
at java.awt.Container.dispatchEventImpl(
at java.awt.Window.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(

How should i go about getting rid of these errors?

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 3791

It looks pretty straightforward. You are getting an exception at line 60 in the code you provided. It's a NullPointerException, so chances are, the argument you are passing in line 60 is null.

What could cause such a thing?
Angelo Tan

Joined: Oct 29, 2010
Posts: 10
Got it. Apparently i forgot initialize the exitButton.
It is sorta covered in the JavaRanch Style Guide.
subject: NullPointerException on swing