Akaraka
Akaraka

Reputation: 173

Cannot be referenced from a static function?

 import java.util.*;
 import java.io.*;

public class LootGenerator{

public static void main (String[] args)
    throws FileNotFoundException{
        System.out.println("This program simulates the random item generator");
        System.out.println("from the game Diablo II.  Happy hunting!");
        System.out.println();
        String file="monstats.txt";
        Monster[] array = getArray(file);
        //Monster alex=getRandomMonster(array);
        }

public Monster getRandomMonster(Monster[] array){
  int randMonster = (int)(Math.random() * array.length);
    return array[randMonster];
}

public Monster[] getArray (String file)
    throws FileNotFoundException{
Scanner sc = new Scanner (new File (file));
Monster[] array = new Monster[sc.nextInt()];
sc.next(); //takes away the word Class
sc.next(); //takes away the word Type
sc.next(); //takes away the word Level
sc.next(); //takes away the word TreasureClass
for(int a = 0; a < array.length; a++)
{
    array[a] = new Monster(sc.next(), sc.next(), sc.nextInt(), sc.next());
}
return array;
}

}

  class Monster{
    private String monsterClass;
private String type;
    private int level;
    private String treasureClass;

public Monster(String myClass, String myType, int myLevel, String myTreasureClass){
    monsterClass = myClass;
      type = myType;
    level = myLevel;
    treasureClass = myTreasureClass;
}

public String getMonsterClass(){
    return monsterClass;
}

public String getType(){
    return type;
}

public int getLevel(){
    return level;
}

public String getTreasureClass(){
    return treasureClass;
}

}

I can't figure out what is wrong with my program...any advice? I keep getting that it can't be referenced from a static context - the line Monster[] array = getArray(file) that is. The object of the program is to randomly generate a monster from a text file - the assignment is here if you need to look at the text file itself: http://www.cis.upenn.edu/~cis110/hw/hw06/index.html

Upvotes: 0

Views: 351

Answers (7)

Daan
Daan

Reputation: 1546

You're trying to call a regular method from a static method, which isn't possible. You could instantiate LootGenerator and then call the method or make the method static (simply add the static modifier to the method).

One solution:

public static void main (String[] args)
    throws FileNotFoundException {
        System.out.println("This program simulates the random item generator");
        System.out.println("from the game Diablo II.  Happy hunting!");
        System.out.println();
        String file = "monstats.txt";
        LootGenerator lg = new LootGenerator();
        Monster[] array = lg.getArray(file);
        Monster alex = lg.getRandomMonster(array);
}

You can find more information about instance and class members here.

In short, instance methods can only be called on instances of a class. Static methods belong to the class and can be called from anywhere, but they can't access instance variables or call instance methods unless they actually have a reference to an instance of a class.

Upvotes: 3

Ron Avilo
Ron Avilo

Reputation: 21

get array is not static method, you need to initiate a object or to make it static

Upvotes: 2

Kevin
Kevin

Reputation: 56059

The main function is static, meaning that it can be called on the class as a whole, not just a particular instance of that class. getArray is not defined as static, so if you call it you have to call it on a particular instance of a LootGenerator, but you're trying to call it on the class as a whole. I don't see any non-static code in getArray, so you should be able to just declare it static.

Upvotes: 1

Bozho
Bozho

Reputation: 597036

getArray() and getRandomMonster() are non-static - they need an instance of your object to be called on. Your main method is static, so you don't have an instance. You have to create it:

LootGenerator generator = new LootGenerator();
generator.getArray(..);
generator.getRandomMonster(..);

Upvotes: 1

MByD
MByD

Reputation: 137282

You cannot reference a non-static method (getArray) which is related to a specific object directly from a static method (main), which is not related to a specific object.

To fix it, you may simply change your method to static:

public static Monster[] getArray (String file) {
    // ...
}

Upvotes: 1

Zak
Zak

Reputation: 7078

You should make

public Monster[] getArray (String file)
    throws FileNotFoundException

static so that you can call it from inside main() as you are doing currently , you dont need to instantiate the class LootGenerator to call it (if the method is static, it belongs to the class where it is declared and can be called without using any instance of that class).

Upvotes: 1

Sean
Sean

Reputation: 62472

getArray is an instance method, and you're calling it from main which is declared as static. If you change getArray to be static it'll compile.

You'll also have the same problem with getRandomMonster.

Upvotes: 1

Related Questions