• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JInternal Frame and ActionListener

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

I am trying to write a small app that opens a JFrame with a choice of menu.

When an Item on the menu is selected a JInternal frame opens with a button and textarea. You then should be able to click on the button for a message to appear in the text area.

Everything works - except when I click on the button I get a Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException error message.

The button works - with a System.out.println but not with anything else - I think its a focus problem.

The line of code that causes the problem is :

limittext.append("After button pushed");

And the whole program is :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.ActiveEvent;
import javax.swing.JInternalFrame;

public class userinterface extends JFrame
{
private JFrame frame;
private JDesktopPane theDesktop;
JTextArea limittext;
JButton action;
JInternalFrame limitframe;
private JPanel panel;

public userinterface()
{
}


public void go()
{
JDesktopPane theDesktop = new JDesktopPane();
frame = new JFrame();

JMenu system_menu = new JMenu("Start");
system_menu.setMnemonic('S');
//menu items
JMenuBar limitbar = new JMenuBar();

JMenuItem lowlimit_choice = new JMenuItem("limit");
lowlimit_choice.setMnemonic('L');
system_menu.add(limit_choice);

setJMenuBar(limitbar);

setSize(600,400);
setVisible(true);
limit_choice.addActionListener(new limit_choiceListener());

}



class limit_choiceListener implements ActionListener
{


//display new internal window
public void actionPerformed(ActionEvent event)
{

limit_gui();

}
}//end anonymous inner class


public void limit_gui() {
JInternalFrame limitframe = new JInternalFrame("limit frame", true,true,true,true);
limitframe.setDefaultCloseOperation(JInternalFrame.EXIT_ON_CLOSE);

//attach panel to internal frame content pane
JPanel panel = new JPanel();
Container container = limitframe.getContentPane();

container.add(panel,BorderLayout.CENTER);

String answer;
JButton action = new JButton();
action.setText("Action");
panel.add(action);
JTextArea limittext = new JTextArea(10,20);
panel.add(limittext);
limittext.append("Before test"); //This works
action.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )
{

System.out.println("New Test"); //This works
limittext.append("After button pushed");//This fails don�t know why?
}
}
);

limitframe.pack();
add(limitframe);
setSize(200,300);
limitframe.setVisible(true);


}//end of limit_gui()
}//end user interface

The apps is started by a separate class calling the go()method.
Any help would be great!

Thank you for reading.

James
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're going to access local variables from within inner classes, they should be declared FINAL. So, declare limittext like this

final JTextArea limittext = new JTextArea(10,20);

and you should be OK.

Also, you declared JMenuItem lowlimit_choice = new JMenuItem("limit");

and then used limit_choice a couple of times below. Need to change either the variable name or the references.

Hope this helped!
 
James Bedford
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lanny,

Thank you for your reply - unfortunately its still giving the same error - so it must be a number of things that I am doing wrong.

James
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've declared "limittext" twice - once as a class level variable, and once inside the limit_gui() method. The anonymous ActionListener class is assuming that you mean the class level one, which never gets initialized - so it's null - and that's why you're getting NullPointerException.
 
James Bedford
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nathan,

Thank you for this - its fixed it!

James
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic