Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Method is undefined for type main 1

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
Hi,


I'm getting the error message "undefined for type main" for a function I'm trying to call from the run method of my java applet. The function definition is


public abstract class InvaderMovement
{

public static Invader getRightMostInvader(Invader[][] array, int invaderArrayWidth, int invaderArrayHeight)
{
for (int i = invaderArrayWidth - 1; i >= 0; i--)
{
for (int j = 0; j < invaderArrayHeight - 1;j++)
{
if(array[j] != null)
{
//Invader leftMostInvader = array[j];
//return leftMostInvader;
return array[j];
}
}
}

//System.out.println("Right most invader XPos is " + array[invaderArrayWidth][0]);

return null;
}

}


The line in the run method that is calling this function and is reporting the error message is

Invader rightMostInvader =
InvaderMovement.getRightMostInvader(invaderArray, InvaderArrayWidth, invaderArrayHeight);


If anyone can give me any help with this problem I would breatly appreciate it

Thanks!!
 
To execute a class from you must have the following signature :

Code:
public class Test {
 public static void main(String args[]) {
    System.err.println("Hello");
 }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Hi,

The error is actually a compile error staing that getRightMostInvader is not defined in type main. Basically what I'm trying to do here is take a bunch of methods that I've written and consolidate them into a class. Right now if i have them defined in "public class Main extends Applet implements Runnable" then they work but it's getting quite hectic in there as there are so many functions defined so I want a way to put all the methods that are similar inside a class. This class would be an abstract class since it's purpose is to control the movement of a group of "space invaders" (i.e check to see if they hit the applet border, move them from side to side etc.) So it wouldn't make sense to make an instance of this class. I tried inplementing it as an abstract class and then calling the methods by InvaderMovement.getRightMostInvader(...) etc. but it doesn;t seem to work. Is there a different way to accomplish what I'm trying to do?

Thanks for all your help
 
Here is the code for the applet, it may give you a better idea of what i'm trying to accomplish


public class Main extends Applet implements Runnable
{

// variables
private Thread th;
private Player player;
private Invader invader;
private int invaderArrayWidth = 10;
private int invaderArrayHeight = 3;
private int invaderArrayXPlacementIncrement = 40;
private int invaderArrayYPlacementIncrement = 40;
private Invader[][] invaderArray = new Invader[invaderArrayWidth][invaderArrayHeight];
private Shot [] shots;
private boolean invadersMovingRight;
private boolean invadersMovingLeft;
private boolean invaderHit;
private final int appletWidth = 500;
private final int appletHeight = 400;

// speed constants
private final int shotSpeed = -10;
private final int playerLeftSpeed = -2;
private final int playerRightSpeed = 2;

// move flags
private boolean playerMoveLeft;
private boolean playerMoveRight;

// double buffering
private Image dbImage;
private Graphics dbg;

public void init()
{
setBackground (Color.black);
player = new Player(150, 280);

int invaderXPos = 10;
int invaderYPos = 5;

invadersMovingRight = true;
//initialize invader array
for (int i = 0; i < invaderArrayWidth; i++)
{
for (int j = 0;j < invaderArrayHeight; j++)
{
invaderArray[j] = new Invader(invaderXPos, invaderYPos, this);
invaderYPos = invaderYPos + invaderArrayYPlacementIncrement;
}
invaderYPos = 5;
invaderXPos = invaderXPos + invaderArrayXPlacementIncrement;
}
invader = new Invader(100, 50, this);
shots = new Shot[1];
}

public Invader getRightMostInvader(Invader[][] array)
{
for (int i = invaderArrayWidth - 1; i >= 0; i--)
{
for (int j = 0; j < invaderArrayHeight - 1;j++)
{
if(array[j] != null)
{
//Invader leftMostInvader = array[j];
//return leftMostInvader;
return array[j];
}
}
}

//System.out.println("Right most invader XPos is " + array[invaderArrayWidth][0]);

return null;
}

public Invader getLeftMostInvader(Invader[][] array)
{
for (int i = 0; i < invaderArrayWidth; i++)
{
for (int j = 0; j < invaderArrayHeight;j++)
{
if(array[j] != null)
{
//Invader leftMostInvader = array[j];
//return leftMostInvader;
return array[j];
}
}
}

return null;
}

public boolean invaderHitRightBorder(Invader invader)
{
if (invader != null)
{
if(invader.getX() + invaderArrayXPlacementIncrement > appletWidth )
{
return true;
}
}
return false;

}

public boolean invaderHitLeftBorder(Invader invader)
{
if (invader != null)
{
if(invader.getX() < 0 )
{
return true;
}
}

return false;

}

private void moveInvadersLeft(Invader[][] array)
{
for (int i = 0; i < invaderArrayWidth; i++)
{
for (int j = 0; j < invaderArrayHeight;j++)
{
if(invaderArray[j] != null)
{
invaderArray[j].moveLeft();
}
}
}
}

private void moveInvadersRight(Invader[][] array)
{
for (int i = 0; i < invaderArrayWidth; i++)
{
for (int j = 0; j < invaderArrayHeight;j++)
{
if(invaderArray[j] != null)
{
invaderArray[j].moveRight();
}
}
}
}

public void start ()
{
th = new Thread(this);
th.start ();
}

public void stop()
{
th.stop();
}

public void destroy()
{
th.stop();
}

public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while (true)
{
// do operations on shots in shots array
for(int i=0; i<shots.length; i++)
{
if(shots != null)
{
// move shot
shots.moveShot(shotSpeed);

// test if shot is out
if(shots.getYPos() < 0)
{
// remove shot from array
System.out.println("Shot is applet border\n");
shots = null;
break;
}

Shot currentShot = shots;

invaderHit = hitInvader(currentShot, invaderArray);
if (invaderHit == true)
{
shots = null;
invaderHit = false;
}




If you notice all the methods like invaderHit etc are defined here inside the Main.java class. I want to put these all inside their own class called InvaderMovement and then call them by issuing something like InvaderMovement.invaderHit(...);

Figured an abstract class with static methods would do the trick, but it doesn't look like it.

 
You needn't make the class abstract.
That's meant to derive subclasses, and enforce a common interface.

Just make the methods static.

And search a better name than 'Main' I suggest. 'main' is a reserved word in java, and while only minor different from 'Main' it's confusing.

Be sure to use the right number and types of parameters.

Code:
Invader rightMostInvader =  
  InvaderMovement.getRightMostInvader (
    invaderArray,
    InvaderArrayWidth,
    invaderArrayHeight); 
// in Main you wrote:
public Invader getRightMostInvader(Invader[][] array)

And: We don't have much phantasies, we want to see the WHOLE and EXACT errormessage, together with the whole command.

Code:
javac Main.java > compile.log 2> compile.err
prints the output to files, for easy cut'n'paste.

On linux you can even put them to the same file:
Code:
javac Main.java > compile.log 2>&1
which must be possible on win too, but I don't know if it's the same syntax.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top