Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我得到,线程中的异常;“主要”;java.lang.NullPointerException错误。我的代码有什么问题?_Java - Fatal编程技术网

I';我得到,线程中的异常;“主要”;java.lang.NullPointerException错误。我的代码有什么问题?

I';我得到,线程中的异常;“主要”;java.lang.NullPointerException错误。我的代码有什么问题?,java,Java,这是显示错误的类。 我已经注释了第245行,其中netbeans说:取消引用可能的空指针。如果我没有将endHours初始化为null,它也会给出一个错误。当我打印endHour时,它显然会返回一个值。但是当我尝试将endHour放入数组endHours时,会出现这个错误 public List getRanges(String type) throws NullPointerException, FileNotFoundException, IOException{ List endH

这是显示错误的类。 我已经注释了第245行,其中netbeans说:取消引用可能的空指针。如果我没有将endHours初始化为null,它也会给出一个错误。当我打印endHour时,它显然会返回一个值。但是当我尝试将endHour放入数组endHours时,会出现这个错误

public List getRanges(String type) throws NullPointerException, FileNotFoundException, IOException{
    List endHours = null;


    String fileName="";
    if(type.equals("tcp")){
        fileName="tcprange";
    }else if(type.equals("udp")){
        fileName="udprange";
    }

    String line = null;

    try {

        FileReader fileReader=new FileReader("conf/"+fileName+".txt");
        BufferedReader bufferedReader=new BufferedReader(fileReader);
        int i=0;
        HashMap tempEndHours=new HashMap();
        while((line = bufferedReader.readLine()) != null) {
            int endHour=0;
            String[] split=line.split("\t");
            endHour=Integer.parseInt(split[1]);
            if(!tempEndHours.containsKey(endHour)){
                tempEndHours.put(tempEndHours,i);
                //System.out.println(endHour);
                endHours.add(i,endHour);// ** line 245
            }
            //System.out.println(endHour);

            i++;

        }
    }catch(FileNotFoundException ex) {
        System.out.println("Unable to open file '"+"conf/"+type+".txt"+ "'");                
    }

    //Collections.sort(endHours);
    return endHours;
}

可能是,在您的
endHour
中,您得到的是一个空值。您对它执行add操作,因此,获取
java.lang.NullPointerException
,检查null。并为我们提供异常的完整堆栈跟踪。

您需要实例化
endHours

public List<Integer> getRanges(String type) throws NullPointerException, FileNotFoundException, IOException{
    List<Integer> endHours = new ArrayList<Integer>();
public List getRanges(字符串类型)抛出NullPointerException、FileNotFoundException、IOException{
List endHours=new ArrayList();

哪里定义了
endHours
?您能发布所有相关代码并指出发生异常的行吗?我们看不到您的IDE行号是的,我在代码“endHours.add(i,endHour);//第245行”中注释了行号。混淆在哪里?您编写了
List endHours=null;
,然后您想知道为什么
endHours
null
?列表中允许使用null值。(假设endHours是一个列表)非常感谢,我对java还是新手。再次感谢您