Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:34:52+00:00 2026-06-14T14:34:52+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:34:53+00:00Added an answer on June 14, 2026 at 2:34 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a lot of code in javascript lately and I'm using Prototype.js to
I'm writing an application which sends a lot of emails throughout the app's lifecycle.
I'm writing my own class, very similar to a toolbar, which will contain widgets
Edit: I have found seq_file that eases writing a lot of data from kernel
I come from a Java background, and am very used to wrapping code into
Wouldn't the best way to dismantle a botnet come from writing your own Virus?
I'm writing some C++ MPI code for a Parallel Computing class. My code works,
So I admit that I'm new to javascript and that I come from a
I'm in Eclipse writing Java. I come from Visual Studio with Resharper writing C#.
I come from the Java world, so to me it's all object.foo() , but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.