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
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
dictorview. Pick one, but you’ll need to stick with it. I don’t really care, but I’m usingdicthere. 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,HashMapjust holdsObjects. And anObjectisn’t aString[]. So you either need to cast the results ofget()to aString[], 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:
See my line 3? By declaring that field
dictas aHashMap<String, String[]>, nowBeginM()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:
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 forproblemwas a method that lacked a return value! Also, I madedictfinal 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 ofthis., because I don’t really like it – but, hey, still your code, put it back if you want!