I'm having some problems when trying to set a panel within my JFrame to visible when someone either presses enter or clicks the JButton within my two methods shown in previous post.

I am getting the following error:

Cannot make a static reference to the non-static field Interface.panel.

The code for my the TextHandler method which uses an ActionListener when a user presses enter on the text field is as follows:

private class TextHandler implements ActionListener {
     
    public void actionPerformed(ActionEvent e) {
    Interface.panel.setVisible(true);    //(where Interface is the method with my GUI components in)
    disp = "Searching: " + e.getActionCommand() + "\t...";
    jtfUneditableText.setText(disp);
    }
    }

When i just use

panel.setVisible(true);

it does compile, but comes up with a bunch of errors when i press enter and try to show the panel, probably due to it not recognising panel in the TextHandler method as it is in Interface().

the code for my JPanel panel is within my class Interface() along with all the other code for creating the GUI textfields and such.

JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
     
    b = new JLabel("Blah blah");
    c = new JLabel("blah blah");
     
    panel.add(b);
    panel.add(c);
    panel.setVisible(false);

what am i doing wrong?

Edited 4 Years Ago by tleverington1: n/a

comes up with a bunch of errors

Please copy and paste the error messages here.

Interface.panel.

The syntax of that statement says the panel is a static variable in the Interface class.
If panel is not static, you get an error message.

Your use of java class names: interface and Panel for your classes and variables makes for confusing reading the code.

Please copy and paste the error messages here.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Interface$TextHandler.actionPerformed(Interface.java:143)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)

that is just the beginning of the error messages.

Interface.panel.

The syntax of that statement says the panel is a static variable in the Interface class.
If panel is not static, you get an error message.

Your use of java class names: interface and Panel for your classes and variables makes for confusing reading the code.

Ok sorry if it is confusing, Interface builds the GUI JFrame, and i have a constructor for Interface. Panel is just a JPanel underneath my 'search box' to display search results so therefore only need to set it visible when i click the Search button or press enter.

About the static error message, I have added static JPanel panel; and have used Interface.panel.setVisible(true);.


the beginning of my code is:

public class Interface extends JFrame {

	//Class Declarations
	JTextField jtfText1, jtfUneditableText;
	String disp = "";
	String highlight = "";
	String term = "";
	TextHandler handler = null;
	JButton searchButton, highlightButton, termButton;
	JLabel searchResult;
	ButtonHandler button = null;
	static JPanel panel;
	
	//Constructor
	public Interface() {

		super("TextField Test Demo");
		Container container = getContentPane();
		container.setLayout(new FlowLayout());
		searchButton = new JButton("Search!");

I get no compiling errors now, just the nullpointerexception mentioned at the start.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Interface$TextHandler.actionPerformed(Interface.java:143)

Look at line 43 and find the variable with the null value.
Then backtrack in the code to find out why that variable does not have a non-null value.

I moved this line

JPanel panel = new JPanel();

into my public class Interface extends JFrame from my constructor

and now it works, I'm guessing that panel had the value null because it was in my Interface constructor?

Edited 4 Years Ago by tleverington1: n/a

Yeah it was the line:

panel.setVisible(true);

Edited 4 Years Ago by tleverington1: n/a

This question has already been answered. Start a new discussion instead.