Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why this object gets null in actionPerformed  RSS feed

arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Nathan Pruett
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

    [ May 08, 2002: Message edited by: Nathan Pruett ]
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!