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