import java.util.*;
public class myClass {
public Random myRandom;
public HashMap<String, ArrayList<String>> myMap;
public ArrayList<String> ar;
public ArrayList<String> nexts;
// ArrayList<String> follows;
public myClass(){
myRandom = new Random();
}
public void setRandom(int seed){
myRandom = new Random(seed);
}
public HashMap<String, ArrayList<String>> buildHashMap(){
HashMap<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
ArrayList<String> ar = new ArrayList<String>();
ar.add("2");
ar.add("2");
String test = "test";
String anothertest = "anothertest";
myMap.put(test, ar);
myMap.put(anothertest, ar);
return myMap;
}
public ArrayList<String> arrayListGetter(String st){
System.out.println("++++++++++++++++++++++++++++++++++");
System.out.println(myMap.size());
ArrayList ar = myMap.get(st);
return ar;
}
public void mainMethod(){
HashMap<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
myMap = buildHashMap();
System.out.println("\n\nNumber of keys found: " + myMap.size());
for (String st : myMap.keySet()){
System.out.println(st + ": ");
ArrayList<String> al = myMap.get(st);
System.out.println(al.size());
}
StringBuilder sb = new StringBuilder();
String test = "test";
System.out.println(myMap.get(test));
System.out.println(sb);
System.out.println(myMap.get(test).size());
System.out.println(myMap.get(test).size());
// ArrayList<String> follows = getFollows(key);
System.out.println("something");
int index = myRandom.nextInt(myMap.get(test).size());
System.out.println(index);
// index = myRandom.nextInt(follows.size());
String next = myMap.get("test").get(index);
sb.append(next);
System.out.println(sb);
System.out.println(myMap.get("test").getClass());
ArrayList<String> follows = new ArrayList<String>();<------new empty ArrayList
System.out.println(follows.size() + " **********");
ArrayList<String> nexts = new ArrayList<String>();
nexts = arrayListGetter ("test");
}
}
我错过了一些明显的东西吗? 提前感谢您的时间和耐心。
答案 0 :(得分:1)
您通过在方法中重新声明myMap变量并将该字段保留为null来隐藏myMap变量。
import java.util.*;
public class MyClass2 {
public HashMap<String, ArrayList<String>> myMap; // this guy is null
// ArrayList<String> follows;
public HashMap<String, ArrayList<String>> buildHashMap() {
HashMap<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
return myMap;
}
public ArrayList<String> arrayListGetter(String st) {
System.out.println("++++++++++++++++++++++++++++++++++");
System.out.println(myMap.size());
ArrayList ar = myMap.get(st);
return ar;
}
public void mainMethod() {
// this myMap is a local variable, and assigning it anything
// will leave the class field null
HashMap<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
myMap = buildHashMap(); // the field in the class is still null!!!
System.out.println("\n\nNumber of keys found: " + myMap.size());
for (String st : myMap.keySet()) {
System.out.println(st + ": ");
ArrayList<String> al = myMap.get(st);
System.out.println(al.size());
}
// this throws a NPE
ArrayList<String> someMap = arrayListGetter("test");
}
}
这意味着您正在初始化一个已为某些方法声明为本地的HashMap。是的,你给它的名字是myMap,与类中的字段相同,但是通过在方法中声明它是局部的,变量只在该方法中可见,并且类中的字段保持为空强>
解决方案:不要这样做,不要给本地字段提供与字段相同的名称,如果要分配给类中的字段,则不要在本地范围内重新声明变量。