Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

non-static method cannot be referenced from a static context

843810Apr 30 2007 — edited Aug 17 2007
Hi im a newbie at this ^^;

I get the error message "non-static method cannot be referenced from a static context" when i try to compile my Asteroids class. Im trying to access an arraylist from my spaceRock class.
package Asteroids;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import static java.lang.Character.*;
import java.awt.image.BufferedImage;
import static java.lang.System.*;

import Asteroids.Ship;
 
public class Asteroids extends JFrame implements KeyListener
{
	private Timer timer;
	private final static int SLEEP = 50;
	private int xHelm = 300;
	private int yHelm = 100;
	private Ship Enterprise = new Ship();
	private spaceRock Rock = new spaceRock();
	private spaceRock s = new spaceRock();

	public Asteroids()
	{
		super("Asteroids :D");
		setSize( 650, 550 );
		setBackground(Color.black);

		this.addKeyListener(this);

		ActionListener paintCaller = new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{
				repaint();
				xHelm--;
				yHelm++;
			}
		};
		timer = new Timer(SLEEP, paintCaller);
		timer.start();

		setVisible(true);
	}

	public void paint( Graphics window )
	{
		Graphics2D twoDGraph = (Graphics2D)window;

		int width = this.getWidth();
		int height = this.getHeight();

		BufferedImage back = (BufferedImage)(this.createImage(width,height));
		Graphics graphToBack = back.createGraphics();

		Enterprise.launchShip(graphToBack,xHelm,yHelm);
		
		for(int i = 0; i<spaceRock.spawns.size(); i++) //Gives me errors
		{
		spaceRock r = spaceRock.spawns.get(i); //Gives me errors
		r.releaseAsteroid(graphToBack,yHelm+50,xHelm+50);
		}

		twoDGraph.drawImage(back, null, 0, 0);
	}

    public void keyTyped(KeyEvent e)
	{
		if (e.getKeyChar() == 'b' || e.getKeyChar() == 'B')
		{
			Enterprise.damageShields();
		}
	}

	public void keyPressed(KeyEvent e)
	{
	}

    public void keyReleased(KeyEvent e)
	{
	}

	public static void main( String args[] )
	{
		Asteroids Game = new Asteroids();
	}
}
// spaceRock Class
package Asteroids;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.Image;
import javax.swing.JFrame;
import java.util.ArrayList;

public  class spaceRock extends JFrame
{
	private int width;
	private int height;
	private Image asteroid;
	ArrayList<spaceRock> spawns = new ArrayList<spaceRock>();

  	public spaceRock()
  	{
		width=50;
		height=50;
		asteroid=null;
    }

	public void releaseAsteroid(Graphics window, int x, int y)
	{
		asteroid = Toolkit.getDefaultToolkit().getImage("astero6b.png");
		window.drawImage(asteroid,x,y,width,height,this);
	}
}
I comment-flagged the lines that give me errors.

Thankyou for your time ^_^
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 14 2007
Added on Apr 30 2007
2 comments
333 views