This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Why this object gets null in actionPerformed Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Why this object gets null in actionPerformed" Watch "Why this object gets null in actionPerformed" New topic
Author

Why this object gets null in actionPerformed

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Hello Everybody,
I was trying to develop following simple program but have some questions. Could you please help me solve same..

Q1: Why this mp gets null in actionPerformed? Moment I am clicking it on "Changeuser/login" option under File Menu it sends error on console.
Q2: Can you help me to wrtie this code in better fashion?
Q3: Is this style of coding will manage the system Memory or this wil take more than required?
I would appreciate if some one will answer it pointwise to understand it better.

regards,
arun

Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

OK, I looked over your class and patched it up... Here are some of the biggest problems I noticed...
  • You were adding the action listener in a really strange way... The usual way to do this is to create one instance of the class and call addActionListener( myActionListener ) on each button you want to listen to. I think that your reference to mp got lost due to the way you were using the actionlistener.
  • You don't need a seperate JFrame to build the menu bar... it is easier and better to do it inside the frame that is actually using the menu bar.
  • The way that you were figuring out which button or menuitem was pushed was strange... it is easier to use the action command of the button that was pressed to figure it out...
  • You need to call validate() instead of repaint() in your makePanel. Repaint should be called if only the graphics drawn on components are changed. If components are added or removed, you need to re - validate() the component.
  • You can make your actionlistener an inner class to prevent the reference to mp getting passed all over the place...
  • That's all for now...


  • Here's the modified code...



    -Nate
    [ May 08, 2002: Message edited by: Nathan Pruett ]

    -Nate
    Write once, run anywhere, because there's nowhere to hide! - /. A.C.
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: Why this object gets null in actionPerformed
     
    Similar Threads
    how to enable/disable this item?
    How to listen toolbar buttons?
    why this component access is multiplying?
    How to set this component?
    Can you help me to write better code ?