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