• Post Reply Bookmark Topic Watch Topic
  • New Topic

JInternal Frame and ActionListener

 
James Bedford
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Lanny Gilbert
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi Nathan,

Thank you for this - its fixed it!

James
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!