4 Replies - 704 Views - Last Post: 10 December 2012 - 07:55 AM Rate Topic: -----

#1 BlackFiredDragon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-July 12

non-static variable this cannot be referenced from a static context?

Posted 09 December 2012 - 04:49 AM

My little brother was bored, so I wanted to make him a little silly program that does the simplest thing. I only know the basics of java programming, and I'm having this error. Here's the error:

bomb.java:22: error: non-static variable this cannot be referenced from a static
context
objB1.addActionListener(new action());
^
1 error

Note: The little arrow (^) is under the word 'new'

Here's my code:

import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class bomb
{
	public static void main(String args[])
	{
		Frame frame = new Frame("BOMB");
		frame.setVisible(true);
		frame.setSize(800,800);

		JPanel panel = new JPanel();
		frame.add(panel);
		
		JButton objB1 = new JButton("BOMB");
		objB1.setBounds(40,150,80,90);
		
		panel.add(objB1);
		
		objB1.addActionListener(new action());
		
	}
	class action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			JFrame frame = new JFrame("BOMB");
			frame.setVisible(true);
			frame.setSize(200,200);
			JPanel panel = new JPanel();
			JLabel label1 = new JLabel("BOOM");
			
			frame.add(panel);
			panel.add(label1);
		}
	}
}



Thanks.

This post has been edited by BlackFiredDragon: 09 December 2012 - 04:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: non-static variable this cannot be referenced from a static context?

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: non-static variable this cannot be referenced from a static context?

Posted 09 December 2012 - 05:00 AM

Typically in Java, your classes are each in their own file. When you have an inner class, such as action in this case, that class requires an instance of the outer class in order to be instantiated, because it shares all of its instance variables.

A quick fix would be to declare the inner class as static, or to declare each class in its own file. In this case the bomb class does nothing at all, so you could just make bomb implement ActionListener, and scratch the action class.
Was This Post Helpful? 1
  • +
  • -

#3 BlackFiredDragon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-July 12

Re: non-static variable this cannot be referenced from a static context?

Posted 09 December 2012 - 05:24 AM

View PostSheph, on 09 December 2012 - 05:00 AM, said:

Typically in Java, your classes are each in their own file. When you have an inner class, such as action in this case, that class requires an instance of the outer class in order to be instantiated, because it shares all of its instance variables.

A quick fix would be to declare the inner class as static, or to declare each class in its own file. In this case the bomb class does nothing at all, so you could just make bomb implement ActionListener, and scratch the action class.



Ok, I scratched the action class and made class bomb implement ActionListener. But how do I add the actionlistener to the button?

New Code:
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class bomb implements ActionListener
{
	public static void main(String args[])
	{
		Frame frame = new Frame("BOMB");
		frame.setVisible(true);
		frame.setSize(800,800);

		JPanel panel = new JPanel();
		frame.add(panel);
		
		JButton objB1 = new JButton("BOMB");
		objB1.setBounds(40,150,80,90);
		
		panel.add(objB1);
		
		objB1.addActionListener(new actionPerformed());
	}
	public void actionPerformed (ActionEvent e)
	{
		JFrame frame = new JFrame("BOMB");
		frame.setVisible(true);
		frame.setSize(200,200);
		JPanel panel = new JPanel();
		JLabel label1 = new JLabel("BOOM");
		
		frame.add(panel);
		panel.add(label1);
	}
}


Also, with this new code, I get this error:

bomb.java:22: error: cannot find symbol
objB1.addActionListener(new actionPerformed());
^
symbol: class actionPerformed
location: class bomb
1 error

This post has been edited by BlackFiredDragon: 09 December 2012 - 05:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: non-static variable this cannot be referenced from a static context?

Posted 10 December 2012 - 05:05 AM

This is happening because you are treating the method 'actionPerformed' as an object. You should be creating a new listener object that implements ActionListener, this new object must have the method 'actionPerformed();' and the object itself (the listener) must be then added to your button.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: non-static variable this cannot be referenced from a static context?

Posted 10 December 2012 - 07:55 AM

Quote

You should be creating a new listener object that implements ActionListener, this new object must have the method 'actionPerformed();' and the object itself (the listener) must be then added to your button.


Not really, a lot complicated for nothing. :) Your class bomb implements ActionListener and has an actionPerformed() method so it is simply

objB1.addActionListener(this);
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1