I have been trying to create a game for my programming class in high school, and I have ran into an issue to do with objects instantiated in different classes. My teacher has been unable to help, as he is "learning alongside us".
public void launchGame(){
ObjectInitialize init = new ObjectInitialize();
init.objectInitialize();
while(year > 1900){
util.checkEvents();
prussia.setTactics(4);
I have a main method in another class that calls this method. The first thing this method does is create a new object of the class ObjectInitialize named init. Then I call the objectInitialize method on init. This is the code for that method:
public void objectInitialize(){
Country france = new Country("France", 10, 200, 2, 100, 100, 1, 1, 800);
Country russia = new Country("Russia", 8, 150, 1, 100, 100, 1, 1, 1300);
Country austria = new Country("Austria", 8, 150, 1, 100, 100, 1, 1, 700);
Country prussia = new Country("Prussia", 8, 175, 2, 100, 100, 1, 1, 700);
FranceUI franceUI = new FranceUI();
RussiaUI russiaUI = new RussiaUI();
AustriaUI austriaUI = new AustriaUI();
PrussiaUI prussiaUI = new PrussiaUI();
EventHandlerUI eventHandlerUI = new EventHandlerUI();
GameStartUI gameStartUI = new GameStartUI();
AllianceUI allianceUI = new AllianceUI();
Utilities util = new Utilities();
}
As you can see, this method creates many objects. There are no obvious errors in this method. However, when I try to use the util.checkEvents() or prussia.setTactics(4) objects/methods, my IDE tells me that it "cannot find symbol". Is this because the class that the launchGame() method is in cannot access the objects created in the other class? If so, how do I fix this? I can post more of my code if anyone needs to see it. Thank you!
EDIT
I have moved all of my code that was initially in the objectInitialize() method into the constructor, and created variables for each object outside like so:
public class objectInitialize {
Country france;
Country russia;
Country austria;
Country prussia;
FranceUI franceUI;
RussiaUI russiaUI;
AustriaUI austriaUI;
PrussiaUI prussiaUI;
EventHandlerUI eventHandlerUI;
GameStartUI gameStartUI;
AllianceUI allianceUI;
Utilities util;
public objectInitialize(){
Country france = new Country("France", 10, 200, 2, 100, 100, 1, 1, 800);
System.out.println("france done");
Country russia = new Country("Russia", 8, 150, 1, 100, 100, 1, 1, 1300);
System.out.println("russia done");
Country austria = new Country("Austria", 8, 150, 1, 100, 100, 1, 1, 700);
System.out.println("austria done");
Country prussia = new Country("Prussia", 8, 175, 2, 100, 100, 1, 1, 700);
System.out.println("prussia done");
FranceUI franceUI = new FranceUI();
RussiaUI russiaUI = new RussiaUI();
AustriaUI austriaUI = new AustriaUI();
PrussiaUI prussiaUI = new PrussiaUI();
EventHandlerUI eventHandlerUI = new EventHandlerUI();
GameStartUI gameStartUI = new GameStartUI();
AllianceUI allianceUI = new AllianceUI();
System.out.println("ui done");
}
public Country getFrance(){
return france;
}
}
I also created that method for returning an object, like some of you suggested. However, I am still having problems here:
public class Game {
int year = 1836;
int currentTurn = 0;
public void launchGame(){
System.out.println("123");
objectInitialize init = new objectInitialize();
System.out.println("123");
System.out.println(init.france.equip); // <-- Right here is the problem. My IDE
} // is telling me that I have a
} // NullPointerException at this line
Even if I change the init.france.equip to init.getFrance().equip I get the same error. Any ideas? Here is the code for the Country class:
public class Country {
String name;
int income;
int treasury;
int industry;
int influence;
int prestige;
int tactics;
int equip;
int manpower;
public Country(String startName, int startIncome, int startTreasury, int startIndustry, int startInfluence, int startPrestige, int startTactics, int startEquip, int startManpower){
name = startName;
income = startIncome;
treasury = startTreasury;
industry = startIndustry;
influence = startInfluence;
prestige = startPrestige;
tactics = startTactics;
equip = startEquip;
manpower = startManpower;
}
public int getEquip(){
return equip;
}
When I change the .equip to .getEquip I still get the same error.
I suppose that the reference is pointing to nothing? But shouldn't creating the init object have allowed me to reference the country objects? Hopefully this isn't a bad question, I really appreciate all of your suggestions and assistance.