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 7697367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:59:45+00:00 2026-05-31T21:59:45+00:00

I am having trouble trying to make a HashMap accessible to other methods in

  • 0

I am having trouble trying to make a HashMap accessible to other methods in the class that it’s in.

Here is basically what I am trying to do,

class object
    method main
        this.test=9
    method fire
        output this.test

Here is the real code

import java.util.*;
import java.lang.String;
import java.util.HashMap;

public class problem {
    public HashMap dict;

    public problem() {
        HashMap<String, String[]> dict = new HashMap<String, String[]>();

        // put everything into hashmap
        String[] items = { "toys", "sun" };
        dict.put("animal", items);
        String[] items_2 = { "fun", "games" };
        view.put("human", items_2);

        this.view = view;

        // start
        this.BeginM();
    }

    public void BeginM() {
        System.out.println(this.view.get("human")[0]); // should give "fun"
    }
}

I get this error at the output stage:

array required, but java.lang.Object found
  • 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-05-31T21:59:46+00:00Added an answer on May 31, 2026 at 9:59 pm

    You know what? I’ve had one of those days where no matter how hard you try nothing quite ends up working right so, in order to have something actually working right, I’m going to fix up your code!

    I sometimes see people post total rewrites of strange questions here and I’ve always wondered why!? Now I know, it’s to compensate for having a day where one of your virtual machines goes belly up for no reason, you’ve got a bug that eludes reproduction until the moment you look away and your dog forgot his housetraining spontaneously.

    Anyway, you need to figure out whether you want that variable to be called dict or view. Pick one, but you’ll need to stick with it. I don’t really care, but I’m using dict here. If you prefer the other, hey, it’s your code, do what you like! But don’t use both, that’ll get confusing.

    Your problem is that in your field, you’re just using HashMap. Use the fancy well-typed stuff, or cast. Otherwise, HashMap just holds Objects. And an Object isn’t a String[]. So you either need to cast the results of get() to a String[], or you can just forget about all that and use the fancy well-typed stuff (sometimes we call that “generics”). I’m going to use the fancy well-typed stuff (HashMap<String, String[]>), but like I said – it’s your code, cast if you want to!

    Anyway, that gets us to:

    public class problem
    {
        HashMap<String, String[]> dict;
    
        public problem()
        {
            HashMap<String, String[]> dict = new HashMap<String, String[]>();
    
            // put everything into hashmap
            String[] items =
            {
                "toys", "sun"
            };
            dict.put("animal", items);
            String[] items_2 =
            {
                "fun", "games"
            };
            dict.put("human", items_2);
    
            this.dict = dict;
    
            // start
            this.BeginM();
        }
    
        public void BeginM()
        {
            System.out.println(this.dict.get("human")[0]); // should give "fun"
        }
    }
    

    See my line 3? By declaring that field dict as a HashMap<String, String[]>, now BeginM() knows what kind of objects it holds and you won’t get that error any more.

    Although I’d take it a step further and make it a bit more concise and a bit less error prone:

    public class Problem
    {
        private final HashMap<String, String[]> dict;
    
        public void Problem()
        {
            dict = new HashMap<String, String[]>();
    
            dict.put("animal", new String[] { "toys, "sun" });
            dict.put("human", new String[] { "fun", "games" });
    
            BeginM();
        }
    
        public void BeginM()
        {
            System.out.println(dict.get("human")[0]);
        }
    }
    

    So what did I do there? Well, first I capitalized Problem. It’s sort of convention to have class names that start with a capital letter. Not mandatory, of course, but it’s nice to have, especially when you’re working with other developers. Case in point: I thought your constructor for problem was a method that lacked a return value! Also, I made dict final and private, this is so that you don’t accidentally overwrite that field later. And I made it private, which is good design. If somebody else needs to get at it, we can give them an accessor method. Finally, I got rid of this., because I don’t really like it – but, hey, still your code, put it back if you want!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with trying to use make to place object files in a
I'm trying to make a simple drawing app in c++, but i'm having trouble
I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
Hey, I currently am having trouble trying to get this to work. Here's a
I'm trying to make an SDL application in Xcode, but I'm having trouble loading
im fairly new to android development, but i'm having some trouble trying to make
I'm having trouble trying to find any tutorials on how to make a paging
I am trying to make a dynamic function but i am having trouble because
I am trying to make this ViewGroup programmatically, but I am having trouble doing
So, I'm having a little trouble trying to make a card reader work properly

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.