-1

This is the class where the error is shown. I have commented the line number 245, where netbeans says: dereferencing possible null pointer. if i didn't initialize the endHours as null, it also gives an error. and when i print endHour, it clearly returns a value. but i get this error when i try to put the endHour to the array 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;
}

2 Answers2

0

may be, in your endHour you are getting a null value. your performing add operation on it so, your getting java.lang.NullPointerException, check for null. and give us full stack trace of your exception.

0

You need to instantiate endHours

public List<Integer> getRanges(String type) throws NullPointerException, FileNotFoundException, IOException{
    List<Integer> endHours = new ArrayList<Integer>();
Aragorn
  • 5,021
  • 5
  • 26
  • 37