Java另一个HashMap<;对象,ArrayList<;字符串>&燃气轮机;空指针异常

Java另一个HashMap<;对象,ArrayList<;字符串>&燃气轮机;空指针异常,java,arraylist,nullpointerexception,null,Java,Arraylist,Nullpointerexception,Null,好的,我重写了我的类,我很匆忙,所以代码还不干净,不管怎样,现在它已经堆起来了,运行main方法问题仍然存在 import java.util.*; public class myClass { public Random myRandom; public HashMap<String, ArrayList<String>> myMap; public ArrayList<St

好的,我重写了我的类,我很匆忙,所以代码还不干净,不管怎样,现在它已经堆起来了,运行main方法问题仍然存在

   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");




            }
            }
import java.util.*;
公共类myClass{
公共随机myRandom;
公共HashMap-myMap;
公共ArrayList ar;
公共阵列列表nexts;
//ArrayList紧随其后;
公共myClass(){
myRandom=新随机数();
}
公共void setRandom(整数种子){
myRandom=新随机(种子);
}
公共HashMap buildHashMap(){
HashMap myMap=新HashMap();
ArrayList ar=新的ArrayList();
ar.add(“2”);
ar.add(“2”);
字符串test=“test”;
字符串anothertest=“anothertest”;
myMap.put(测试,ar);
myMap.put(另一个测试,ar);
返回myMap;
}
公共ArrayList arrayListGetter(字符串st){
System.out.println(“+;
System.out.println(myMap.size());
ArrayList ar=myMap.get(st);
返回ar;
}
公共方法(){
HashMap myMap=新HashMap();
myMap=buildHashMap();
System.out.println(“\n\n找到的键数:”+myMap.size());
for(字符串st:myMap.keySet()){
System.out.println(st+“:”);
ArrayList al=myMap.get(st);
System.out.println(al.size());
}
StringBuilder sb=新的StringBuilder();
字符串test=“test”;
System.out.println(myMap.get(test));
系统输出打印LN(sb);
System.out.println(myMap.get(test.size());
System.out.println(myMap.get(test.size());
//ArrayList follows=getFollows(键);
System.out.println(“某物”);
int index=myRandom.nextInt(myMap.get(test.size());
系统输出打印项次(索引);
//index=myRandom.nextInt(follows.size());
字符串next=myMap.get(“测试”).get(索引);
某人(下一个);
系统输出打印LN(sb);
System.out.println(myMap.get(“test”).getClass());

ArrayList follows=new ArrayList();通过在方法中重新声明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");
    }
}
import java.util.*;
公共类MyClass2{
public HashMap myMap;//这家伙是空的
//ArrayList紧随其后;
公共HashMap buildHashMap(){
HashMap myMap=新HashMap();
返回myMap;
}
公共ArrayList arrayListGetter(字符串st){
System.out.println(“+;
System.out.println(myMap.size());
ArrayList ar=myMap.get(st);
返回ar;
}
公共方法(){
//这个myMap是一个局部变量,可以给它赋值
//将类字段保留为空
HashMap myMap=新HashMap();
myMap=buildHashMap();//类中的字段仍然为空!!!
System.out.println(“\n\n找到的键数:”+myMap.size());
for(字符串st:myMap.keySet()){
System.out.println(st+“:”);
ArrayList al=myMap.get(st);
System.out.println(al.size());
}
//这会抛出一个NPE
ArrayList someMap=arrayListGetter(“测试”);
}
}
这意味着您正在初始化一个已声明为某个方法本地的HashMap。是的,您已将其命名为与类中的字段相同的名称myMap,但通过将其声明为方法本地,该变量仅在该方法中可见,并且类中的字段保持为null


解决方案:不要这样做,不要给本地字段与字段同名,如果要分配给类中的字段,不要在本地范围内重新声明变量。

“我认为您可以放心地假设,这是通过在主函数中调用的单独方法正确构建和填充的。”
——在调试错误之前,您不能“安全地假设”任何事情。说真的。“我认为您可以安全地假设”.不。考虑到错误,我想说事实并非如此……你会想给我们一个体面的回答,因为在我们能够真正测试代码之前,没有人能够或应该安全地假设任何事情。不管怎样,一个或它没有发生。事实是:1)这个问题在目前的状态下是完全无法回答的,因为没有人能够猜出错误在哪里代码未显示。2)我们中的许多人都表现出极度克制,没有将此作为“另一个NPE问题”结束,所以很多人都认为这个问题可能有一些价值。3)但我们在看到你的之前不会知道。这不是一个完整的代码转储或链接,而是一个新的小程序,你可以完整地发布在这里,我们可以不加修改地运行,并向我们显示你的错误。我想可能是这个,也可能是关闭。非常感谢你的回复!!!事实上,我看到myMap是本地的-老实说,我太努力了,现在我还没有消化你文章中的所有内容。之前,我用声明变量为“全局”的相同方法测试了许多类似的类然后用相同的名称创建新的实例,它一直都是有效的。现在我想我将尝试在类的构造函数中调用我的HashMap生成器,希望myMap可以从类的所有方法中看到。现在我已经用注释代码再次阅读了您的消息,现在我更清楚地看到了这个问题,顺便说一句,我已经在我曾经用本地初始化的HashMaps编写过类似的方法,但我的错误代码侥幸逃脱了;我能想到的解决方案是