Okay,
So this code I have is giving me an error: non-static variable this cannot be referenced from a static context.

Here is the code:

package macey;
/**
 * @author WolfShield
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DemoConv implements ActionListener {

    private JLabel answerL;

    public static void Conv() {
        // Content
        JPanel content = new JPanel();
        content.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        // Actual Content
        JTextField userInputNum, userInputUnit, userOutputUnit;
        JButton convCalculateB;
        JLabel leftBarL, rightBarL, multSymbolL, left1L, right1L, equalsL, answNumL, answUnitL;
        // Variable Labels
        JLabel rightBottomConvUnitL, rightTopConvNumL;

        //Assign all above content
        leftBarL = new JLabel("__________");
        c.gridy=2;
	c.gridx=0;
	c.gridwidth=2;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(leftBarL, c);
        rightBarL = new JLabel("__________");
        c.gridy=2;
	c.gridx=3;
	c.gridwidth=2;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(rightBarL, c);
        multSymbolL = new JLabel("X");
        c.gridy=2;
	c.gridx=2;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(multSymbolL, c);
        left1L = new JLabel("1");
        c.gridy=3;
	c.gridx=0;
	c.gridwidth=2;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(left1L, c);
        right1L = new JLabel("1");
        c.gridy=3;
	c.gridx=3;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(right1L, c);
        equalsL = new JLabel("=");
        c.gridy=2;
	c.gridx=5;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 10, 0, 10);
        content.add(equalsL, c);
        convCalculateB = new JButton("Calculate");
        c.gridy=5;
	c.gridx=2;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(10, 0, 0, 0);
        convCalculateB.addActionListener(this);
        content.add(convCalculateB, c);
        userInputNum = new JTextField();
        c.gridy=1;
	c.gridx=0;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=39;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(userInputNum, c);
        userInputUnit = new JTextField();
        c.gridy=1;
	c.gridx=1;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=25;
	c.ipady=0;
	c.insets=new Insets(0, 3, 0, 0);
        content.add(userInputUnit, c);
        userOutputUnit = new JTextField();
        c.gridy=1;
	c.gridx=4;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=25;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(userOutputUnit, c);
        rightTopConvNumL = new JLabel("0.08"); // Changes
        c.gridy=1;
	c.gridx=3;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(rightTopConvNumL, c);
        rightBottomConvUnitL = new JLabel("in.");
        c.gridy=3;
	c.gridx=4;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(rightBottomConvUnitL, c);
        answNumL = new JLabel("0.4");
        c.gridy=2;
	c.gridx=6;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(answNumL, c);
        answUnitL = new JLabel("ft.");
        c.gridy=2;
	c.gridx=7;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 5, 0, 0);
        content.add(answUnitL, c);

        //Menubar
        JMenuBar menuBar = new JMenuBar();

        JMenu fileMenu = new JMenu("File");
        JMenuItem fileQuit = new JMenuItem("Quit");
        fileMenu.add(fileQuit);
        menuBar.add(fileMenu);

        JMenu helpMenu = new JMenu("Help");
        JMenuItem helpHowToUse = new JMenuItem("How To Use The Converter");
        helpMenu.add(helpHowToUse);
        menuBar.add(helpMenu);

        // Converter Window
        JFrame convWin = new JFrame("Converter Demo");
        convWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        convWin.setResizable(false);
        convWin.setSize(new Dimension(340, 160));
        convWin.setLocation(450, 150);
        convWin.getRootPane().setDefaultButton(convCalculateB);
        convWin.setJMenuBar(menuBar);
        convWin.setContentPane(content);
        convWin.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Conv();
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        String f;
        f = e.getActionCommand();

    }

}

The line the error is on is line 89:

convCalculateB.addActionListener(this);

Thanks in advance,

- WolfShield

Recommended Answers

All 5 Replies

conv is a static method. It doesn't have a "this" - it executes without an object.

Thanks,
When I change Conv() from static I then have a problem with the main() class.

It says that non-static Conv() cannot be called from a static.

But if I change the main() to non-static it says that there is no main() class.

- WolfShield

What's normally done here is to have all the methods & variables non-static (unless there is a good reason otherwize) then in your main method create a new instance of the class. The main method must be static to be recognised.

The code you have in the static Conv() method should be in the constructor public DemoConv() {...}.

main has to be static - that signature is required.

There are some standard idioms to get around this, some good and some less good. What I usually do is to instantiate an object of the class and call the methods on that:

public class Foo{
  public static void main(String[] args)
  {
    Foo f = new Foo();
    f.instanceMethod1();
    f.instanceMethod2(); 
  }
}

For a larger program, I might make a driver class whose only job is to contain main() and start other objects - for example, if I have a model, view, controller, main might instantiate the three objects and give each a reference to the others, and then get out of the way.

Okay,
So here's the weird part: I switched it over from NetBeans into Eclipse and it works.

Huh...maybe I'll stick with Eclipse :)

Thanks guys for all of your help!

- WolfShield

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.