I am having an issue with a Null Pointer Exception, but I can't figure out what the issue is. Here is my code in a doPost:
HttpSession session = request.getSession();
session.setAttribute("product", product);
url = "/login.jsp";
// create product list and store it in the session
String path = getServletContext().getRealPath("/WEB-INF/products.txt");
ArrayList<Product> products = ProductIO.selectProducts(path);
session.setAttribute("products", products);
// forward request and response objects to specified URL
getServletContext().getRequestDispatcher(url).forward(request, response);
Here is the code from my ProductIO class:
private static ArrayList<Product> products = null;
public static ArrayList<Product> selectProducts(String filename) {
products = new ArrayList<Product>();
File file = new File(filename);
try {
if(file.exists())
{
BufferedReader in
= new BufferedReader(
new FileReader(file));
String line = in.readLine();
while (line != null) {
StringTokenizer t = new StringTokenizer(line, "|");
if (t.countTokens() >= 3) {
String code = t.nextToken();
String description = t.nextToken();
String priceAsString = t.nextToken();
double price = Double.parseDouble(priceAsString);
Product p = new Product();
p.setCode(code);
p.setDescription(description);
p.setPrice(price);
products.add(p);
}
line = in.readLine();
}
in.close();
}
return products;
} catch (IOException e) {
System.out.println(e);
return null;
}
}
I get the error with the line "ArrayList products = ProductIO.selectProducts(path);" but I cannot figure out why that doesn't work, since I took it directly from an example that I learned.
Here are the errors that I am getting:
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.valueOf(Double.java:502)
at murach.business.ProductManagementServlet.doPost(ProductManagementServlet.java:140)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)