1

I come from writing a lot of JavaScript, so bear with me.

I've got 3 HashMaps, which i reference in a method in a different class. My code (very simply) looks like so:

public class MainClass {
    private HashMap<String,Nation> randomHashMap = new HashMap<String,Nation>();
    DifferentClass d = new DifferentClass(this);
} //with getters/setters

public class DifferentClass {
    private MainClass mc;
    public void randomMethod() {
        System.out.println("randomHashMap is " + (mc.getRandomHashMap() == null));
    } //returns null
    public DifferentClass(MainClass c) {
        this.mc = c;
    }
} 

However, when I call them in my other method, they're null.

How do I create a new, empty HashMap?

6
  • 3
    Make sure you initialize them in the constructor or before using them in your code. Commented Nov 18, 2012 at 8:33
  • 1
    For best understanding the problem, show your code. Commented Nov 18, 2012 at 8:35
  • 1
    The code you've provided won't even compile, unless you've really got a static method called getRandomHashMap() in MainClass. Commented Nov 18, 2012 at 8:40
  • 1
    My condolence for moving from the land of dynamic freedom to Java's type tyranny. Commented Nov 18, 2012 at 9:01
  • 1
    @MarkoTopolnik: The code has changed. The call to getRandomHashMap() was originally MainClass.getRandomHashMap(). (See the edit history.) Commented Nov 18, 2012 at 9:05

2 Answers 2

5

You need to initialize your MainClass mc variable before using it in the DifferentClass#randomMethod method. Also, make sure you're using the mc variable instead of the MainClass.getRandomHashMap() method (by your actual code, we don't know how it behaves). Your code will look like this:

public class DifferentClass {

    private MainClass mc = new MainClass();

    public void randomMethod() {
        //assuming getRandomHashMap is the getter of randomHashMap attribute (and non static)
        System.out.println("randomHashMap is " + (mc.getRandomHashMap() == null));
    }
}

public class MainClass {
    private HashMap<String,Nation> randomHashMap = new HashMap<String,Nation>();
    DifferentClass d = new DifferentClass(this);

    public HashMap<String,Nation> getRandomHashMap() {
        return this.randomHashMap;
    }
} //with getters/setters
Sign up to request clarification or add additional context in comments.

4 Comments

when i pass the reference in the constructor, i get errors to the stars (stack overflow errors)
@SomekidwithHTML it would depend on your design. If you want to use an instance of MainClass to initialize the DifferentClass attribute (like your code shows), it would be better to do it in the MainClass constructor.
I don't see how this is a solution. OP's code clearly initializes the mc field in its constructor, and the constructor is passed a definitely non-null value from MainClass.
@MarkoTopolnik this solution does not directly solve my problem, but it made me realize what i needed to do differently, along the lines of "it would depend on your design". I realized that how I am writing this does not comply with how it needs to be designed.
0

The code you posted is in fact perfectly all right as far as field initialization. I made an SSCCE from it with minimal intervention:

class Nation{}

public class MainClass {
  private HashMap<String,Nation> randomHashMap = new HashMap<String,Nation>();
  DifferentClass d = new DifferentClass(this);
  public Object getRandomHashMap() {
    return randomHashMap;
  }
  public static void main(String[] args) {
    new MainClass().d.randomMethod();
  }
} //with getters/setters

class DifferentClass {
  private MainClass mc;
  public void randomMethod() {
      System.out.println("randomHashMap is " + (mc.getRandomHashMap() == null));
  } //returns null
  public DifferentClass(MainClass c) {
      this.mc = c;
  }
}

and it prints

randomHashMap is false

which proves that randomHashMap is indeed non-null.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.