Request Help in Solving the Exception in Main thread NullPointerException

//This Program will Perform diffrent operations on Floating Point Numbers
import java.io.;
import java.util.
;
public class Floot{
public static void main(String args){
ArrayList numbers=new ArrayList();

float i;
// To Read the data from the file we will use the Streams

//Introducing the try block in order to avoid any Exception in the code
try{

FileReader fr=new FileReader("assi.txt");
BufferedReader br=new BufferedReader(fr);

//It will read the File LineByLine
//Declaring a variable to store the read line and also a loop that will save the data
String s=br.readLine();
while(!s.equals(null)){

	numbers.add(s);
	System.out.println("The Numbers are: "+s);
	float sum=0;
	s=br.readLine();
}
for(int j=0;j<numbers.size();j++)
{
float sum=0;
String getNum=(String)numbers.get(j);
float f=Float.parseFloat(getNum);
sum=sum+f;
System.out.println("The Sum of Numbers is: "+sum);
}


}
catch(IOException ex){
System.out.println("The file not found");
}

}}

Are you sure that is the way you want to handle your readLine?

You should probably do it inside a while like:

while((s=br.readLine()) != null ) { … }

You can setup counter’s above that while and then increment inside the while and use a toString method on the counter to get the key string for your hash map.

food for thought.

Sorry meant to say String.valueOf() instead of toString().


package com.iackay.brexample;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.HashMap;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * Hello world!
 *
 */
public class Breader {
    private static Logger log = Logger.getLogger(Breader.class);
    private static HashMap<String, String> list = new HashMap<String, String>();
    
    public static void main(String args[]) {
        
        PropertyConfigurator.configure(Breader.class.getClassLoader().getResource("log4j.properties"));
        
        String s = new String();
        float sum = 0;
        float f = 0;
        int counter = 0;
        

        // To Read the data from the file we will use the Streams

        //Introducing the try block in order to avoid any Exception in the code
        try {

            FileReader fr = new FileReader("txt/floats.txt");
            BufferedReader br = new BufferedReader(fr);

            while ((s = br.readLine()) != null) {
                log.info("The Numbers are: " + s);
                list.put(String.valueOf(counter), s);
                counter++;
            }

            for (int j = 0; j < counter; j++) {
                String key = Integer.toString(j);
                if (list.containsKey(key)) {
                    f = Float.parseFloat(list.get(key));
                    sum = sum + f;
                    log.debug("The Sum of Numbers is: " + sum);
                }
            }
        } catch (IOException ex) {
            log.error("IOException: ", ex);
        }
        
        System.exit(1);
    }
}