Hello im making a programm and when i run it,it shows me this message:exception in thread main java.lang.nullpointerexception at FileReader.readfile(TestFiles.java:53) at TestFiles.main(TestFiles.java:240)
So here is where the problem seems to be in my programm
class FileReader
{
private String path;
private File object;
private Scanner inputReader;
private String Line;
public FileReader(String path)
{
this.path=path;
}
public boolean initReader()
{
object = new File(path);
Scanner inputReader = null;
try
{
inputReader = new Scanner(new FileInputStream("universities.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("File universities.txt was not found");
System.exit(0);
}
if (object.exists())
{
return true;
}else
{
return false;
}
}
public ArrayList<String> readFile()
{
ArrayList<String> myList = new ArrayList<String>();
while(inputReader.hasNextLine()); //here is the line:53
myList.add(Line);
myList.remove(0);
return myList;
}
public void closeReader()
{
inputReader.close();
}
}
and the main is here
public class TestFiles
{
public static void main(String[] args)
{
FileReader reader = new FileReader("universities.txt");
if(reader.initReader()){
FileEditor editor = new FileEditor(reader.readFile()); //here is the line 240
reader.closeReader();
editor.fillHashMap();
FileWriter writer = new FileWriter("universities_2015_output.txt");
if(writer.initWriter()){
writer.writeFile(editor.getScoresOfYear(2015));
writer.closeWriter();
}
else{
System.out.println("Error creating file");
}
System.out.println("Average university score of year 2015: "+editor.getAverageOfYear(2015));
System.out.println("Min university score of year 2015: "+editor.getMinOfYear(2015));
System.out.println("Max university score of year 2015: "+editor.getMaxOfYear(2015));
}
else{
System.out.println("Error opening file");
}
}
}
i already read for this problem and i think that my problem is on the inputreader which shows null. Any idea will be helpfull.I must complete this programm in this week so i need your help to finish it. Thank you