This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Acces bean class property through a JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Acces bean class property through a JButton" Watch "Acces bean class property through a JButton" New topic
Author

Acces bean class property through a JButton

Kasun Wixkramanayake
Ranch Hand

Joined: Feb 09, 2010
Posts: 74
I have tried to assign a value to a bean class property through a jTextfield and retrive it (Using netbeans).



but when i try this
the following exception has been thrown
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at se.Log.jButton1ActionPerformed(Log.java:79)
at se.Log.access$000(Log.java:19)
at se.Log$1.actionPerformed(Log.java:46)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)



mohammad mahdi ghaseminya
Greenhorn

Joined: Jun 15, 2011
Posts: 6
Hi,
Are You create first object of your bean?
you must create an instance of it in your main class's constructor.

Ashutosh M Kulkarni
Ranch Hand

Joined: Jun 07, 2010
Posts: 41

This is what you could do.


Note that above code is not foolproof and in fact, is quite vulnerable. But I have just added one line to your code, to show what could be done. If you are to use this professionally for some project, you might wanna put some checks on what value is entered through jTextField1 because it may contain a lot of special characters, multiple decimal points, letters, etc.

In line 04 (refer code above), I have put a label on the frame which will be modified to whatever value user enters from the textfield.
Please refer to attachment to see an example.

Regards,
Ashutosh


[Thumbnail for frameDemo.png]



SCJP 6, next stop - OCPJWCD!
Kasun Wixkramanayake
Ranch Hand

Joined: Feb 09, 2010
Posts: 74
@mohammad mahdi ghaseminya


i have created a bean class instance inside the Action but still get the error message
mohammad mahdi ghaseminya
Greenhorn

Joined: Jun 15, 2011
Posts: 6
No!
if you create null object you cant reach attributes!
also if you want to use this bean in your main class it is useful to create it in construction,
Kasun Wixkramanayake
Ranch Hand

Joined: Feb 09, 2010
Posts: 74
@mohammad mahdi ghaseminya
thank you brother.it works
Ashutosh M Kulkarni
Ranch Hand

Joined: Jun 07, 2010
Posts: 41

So looks like I made the post for nothing... LOL

never mind.
 
Don't get me started about those stupid light bulbs.
 
subject: Acces bean class property through a JButton
 
Similar Threads
Trying to change the text on a button when clicked - crasches....
Acces bean class property through a JButton
How to print list of Files?
refreshing container get NullPointerException errors
Invoking DLL from a Swing GUI