0

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.

4
  • If you code, post the two class files in question. My main question is, why is the objectInitialize method in a separate class from the ObjectInitialize class? Commented Jun 9, 2014 at 12:40
  • put the initialization code in a constructor. I am not sure if I understand your problem correctly. If you are trying to access the variables declared in objectInitialize() in other methods, you should rather declare them as instance variables. Commented Jun 9, 2014 at 12:42
  • This question appears to be off-topic because it is answered in the questions in the "related questions" sidebar. Commented Jun 9, 2014 at 12:42
  • I think that two negatives votes is a bit too strict for someone who is learning how to program and still in high school. It is not easy for him but he is trying to. Commented Jun 9, 2014 at 12:44

6 Answers 6

1

McMill! What is actually happening is that you are creating an object and never returning a reference to it. You should better try to define the countries as a part of the general class. something like:

    public class ObjectInitialize(){
Country france,russia,austria,prussia;
...

public void objectInitialize(){
    france = new Country("France", 10, 200, 2, 100, 100, 1, 1, 800);
    russia = new Country("Russia", 8, 150, 1, 100, 100, 1, 1, 1300);
    austria = new Country("Austria", 8, 150, 1, 100, 100, 1, 1, 700);
    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();
}
...
}

Hope this helps!

And then add the following getters for each variable:

public Country getPrussia(){
 return prussia;
}

and repeat that for the other countries...

Sign up to request clarification or add additional context in comments.

1 Comment

I have done what you recommend, but the IDE still tells me that there is a Null Pointer Exception on the line that I pointed out in my edit. There is also a warning that the local variables declared in the constructor "hide a field". Maybe that is the problem?
1

you need to save those initialized objects somewhere.

As it stands, your ObjectInitialize() function creates a bunch of object, which then go out of scope at the end of the method.

If you declare each object as a class variable in the ObjectInitialize class, you will be able to reference them as you have shown

public class ObjectInitialize{

    public Country france, russia, austria, prussia;
    .
    .
    .
    public void objectInitialize(){
        france = new Country("France", 10, 200, 2, 100, 100, 1, 1, 800);
        russia = new Country("Russia", 8, 150, 1, 100, 100, 1, 1, 1300);
        austria = new Country("Austria", 8, 150, 1, 100, 100, 1, 1, 700);
        prussia = new Country("Prussia", 8, 175, 2, 100, 100, 1, 1, 700);
        .
        .
        .
        }
    }

Also, after re-reading, I see that you could further skip a step by putting that method as the Constructor. Simply change public void ObjectInitialize to public ObjectInitialize() That way you can set everything up by calling ObjectInitialize int = new ObjectInitialize() without the need for calling init.ObjectInitialize()

Finally, when you want to access each of those countries, make sure you reference them like init.prussia.setTactics()

2 Comments

Okay, that works in the method that I instantiated the init object in, but in order to access the Country objects from another method I have to create a new init object. And the Country objects that the new init objects creates seem to be different from the old Country objects, while I need them to be the same object. Am I possibly just making a mistake still? Or is there another way that I should be going about this? If there is another direction I should be headed in, could you point me in the right direction? I'm sure google can help me if I just know what I'm looking for. Thanks :)
without knowing too much about what you are doing, it sounds like you need a class to hold all these countries. Keep in mind when you create a new instance of ObjectInitialize, the fields will be populated within that object. To reference those fields, reference init.france etc
1

Change your class like this. Declare your variables outside the function. and make it public.

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 void objectInitialize(){
    france = new Country("France", 10, 200, 2, 100, 100, 1, 1, 800);
    russia = new Country("Russia", 8, 150, 1, 100, 100, 1, 1, 1300);
    austria = new Country("Austria", 8, 150, 1, 100, 100, 1, 1, 700);
    prussia = new Country("Prussia", 8, 175, 2, 100, 100, 1, 1, 700);
    franceUI = new FranceUI();
    russiaUI = new RussiaUI();
    austriaUI = new AustriaUI();
    prussiaUI = new PrussiaUI();
    eventHandlerUI = new EventHandlerUI();
    gameStartUI = new GameStartUI();
    allianceUI = new AllianceUI();
    util = new Utilities();
}


}

Here you initialized the variable inside a function, better use constructor instead of this

ObjectInitialize init = new ObjectInitialize();
    init.objectInitialize();

Change to public ObjectInitialize() { // content of the function is moved to this class. }

Now create an object ObjectInitialize init = new ObjectInitialize(); which will initialize your variables.

Youy can call the values inside like this. Object.variableName.

  init.russia 

This will get you the values of the russia, likewise you can use any object you want.

Comments

0

You need to add a getter method inside the ObjectInitialize class.

So,

public PrussiaUI getPrussiaUi() { return prussiaUi; }

Also, your objects created in the constructor are method scoped. So after construction, they are eligible for garbage collection. They need to be moved to class level variables.

Outside of the constructor needs to read like,

private PrussiaUI prussiaUi;

ObjectInitialize()
{
    prussiaUi = new PrussiaUi(....)
    ....
}

Comments

0

You can have getter methods for each object you have created in your ObjectInitialize class, and use those getter methods to get required objects.

As Example for one:

public Country getFrance(){
     return france;
}

this way for each object, you can get in any other java class like:

ObjectInitialize obji = new ObjectInitialize();
Country france = obji.getFrance();

Comments

0

The problem is that you are declaring france both as an instance variable and as a local variable in the constructor of your class objectInitialize. So in fact they are two different variables. The same problem occurs with the other variables representing countries.

Then, if you initialize your local variable france in the constructor you are leaving null the one declared as an instance variable.

You should change your code to something like:

public class objectInitialize {
    Country france;
    Country russia;
   ...

    public objectInitialize(){
        //Country france = new Country("France", 10, 200, 2, 100, 100, 1, 1, 800); <-- WRONG
        france = new Country("France", 10, 200, 2, 100, 100, 1, 1, 800); // <-- CORRECT
        System.out.println("france done");
        russia = new Country("Russia", 8, 150, 1, 100, 100, 1, 1, 1300);
        System.out.println("russia done");
        ...
    }
    public Country getFrance(){
        return france;
    }
}

Also, try to use a meaningful name for your class, objectInitialize is a very bad choice, since it does not say anything about what is the role of such class and its instances. You could rename it to GameUI for example, but that is up to you. Whatever your name choice is, try to respect the conventions of the language. For example, class names should start with a capital letter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.