title: Java: How can I use objects instantiated in one class in another class?
body: <p>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".</p>
  
  <pre><code>public void launchGame(){
      ObjectInitialize init = new ObjectInitialize();
      init.objectInitialize();
      while(year &gt; 1900){
          util.checkEvents();
          prussia.setTactics(4);
  </code></pre>
  
  <p>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 <code>ObjectInitialize</code> named <code>init</code>. Then I call the <code>objectInitialize</code> method on <code>init</code>. This is the code for that method:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>As you can see, this method creates many objects. There are no obvious errors in this method. However, when I try to use the <code>util.checkEvents()</code> or <code>prussia.setTactics(4)</code> objects/methods, my IDE tells me that it "cannot find symbol". Is this because the class that the <code>launchGame()</code> 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!</p>
  
  <p>EDIT</p>
  
  <p>I have moved all of my code that was initially in the <code>objectInitialize()</code> method into the constructor, and created variables for each object outside like so:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>I also created that method for returning an object, like some of you suggested. However, I am still having problems here:</p>
  
  <pre><code>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); // &lt;-- Right here is the problem. My IDE
      }                                          // is telling me that I have a 
  }                                              // NullPointerException at this line
  </code></pre>
  
  <p>Even if I change the <code>init.france.equip</code> to <code>init.getFrance().equip</code> I get the same error. Any ideas? Here is the code for the Country class:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>When I change the <code>.equip</code> to <code>.getEquip</code> I still get the same error.</p>
  
  <p>I suppose that the reference is pointing to nothing? But shouldn't creating the <code>init</code> 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.</p>