Alright. Take a deep breath. Now, look at your code:
And now look at my code:
See the difference? Also, you don't need the extra public void actionPerformed method you have arbitrarily stuck in your code. And just for the sake of learning, you don't run a class. Typically you would refer to this as instantiating a class. So your statement would be:
Actually I want it to instantiate a class and give me the results of that class.
posted 12 years ago
Okay I took a breath (smile...) Thanks for that...I needed it. It worked it printed out IT WORKED but then it gives me the below error. Can I not have a actionPerformed and a action listener together??? Again Thank You very much for your help.
public void actionPerformed(ActionEvent e) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Collections.CollectionsJAC.actionPerformed(CollectionsJAC.java:90) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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) [ October 01, 2007: Message edited by: Justin Charpenter ]
Aha! Well, you've stumbled on something that can be a real pain when it comes to Swing. There's more than one way to skin a cat. What you have is not wrong per say. But it's unnecessary.
Your class implements ActionListener. This requires you to implement the actionPerformed method in the body of your class, which you have done. You've also added this class as the action listener for your button.
Perfectly fine. Then you added an anonymous inner class for your button with another event:
Again, perfectly fine. A component can have multiple event listeners attached to them. However, for your purpose, its pointless. You should just choose one over the other.
As far as your actual error is concerned I believe it might have something to do with:
Which I honestly am not sure what you are trying to do. You already have a visible frCollectionsJAC frame. But you are calling visible on it again. I'm not sure where the parent stuff is coming from since that code was not included.
Here is my Main program This program is opening the collection jpanel with a button on it Now I want to click this button and open another jpanel that is reading data from a text file which I have working. I can read and write files but I can't get these buttons to work right.
posted 12 years ago
Rob are you saying I should take out the too string and just leave it as
As I said, getActionCommand() already returns a string (or null). Calling toString() on a string will just return the string itself. Calling toString() on a null reference however will throw a NullPointerException.
NullPointerExceptions (NPEs) are always caused because something before the . is null. Especially when chaining commands like e.getActionCommand().toString(), if the intermediate part (getActionCommand()) is null then it is harder to find.
If that line is still causing the NPE, then there are two probable causes: 1) parent is null 2) e is null
Seeing as 2) won't happen, parent has to be null. Seeing as you set it correctly in the constructor, it must be the value passed to the constructor. How do you instantiate your CollectionsJAC?