I have problem with the code, I got these messages:
java.lang.NullPointerException at java.io.Reader.<init>(Unknown Source) at java.io.InputStreamReader.<init>(Unknown Source) at org.mcgill.ccs2_505.assignment02.listeners.InventoryListener.contextInitialized(InventoryListener.java:90)
public void contextInitialized(ServletContextEvent event) {
// Get data from input file and store into the inventory
ServletContext context = event.getServletContext();
Inventory inventory = new Inventory();
String filePath = context.getInitParameter("inventory-file");
InputStream is=null;
BufferedReader reader=null;
try{
is=context.getResourceAsStream(filePath);
reader=new BufferedReader(new InputStreamReader(is));
String record;
//read every record (one per line)
while((record = reader.readLine())!=null){
// Put the read line in string
// Extract product information form each line
}
context.setAttribute("inventory", inventory);
context.log("The inventory content has been loaded.");
}
catch(Exception e){
context.log("Exception occured while processing the input file.",e);
}
finally{
if(is!=null){
try{
is.close();
}
catch(Exception e){}
}
if(reader!=null){
try{
reader.close();
}
catch(Exception e){}
}
}
}
}
This code is used to read from file and load data into java class structure. However, I receive the null pointer exception when trying to establish connection with file.