0

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)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Glitteropia
  • 107
  • 2
  • 10

2 Answers2

1

Whats is the specific line that is throwing NullPointerException? Because there is nothing wrong in ArrayList products = ProductIO.selectProducts(path);

Try to change products = new ArrayList<Product>(); to ArrayList<Product> products = new ArrayList<Product>();.

Bruno
  • 2,889
  • 1
  • 18
  • 25
  • ArrayList products = ProductIO.selectProducts(path); is the line causing the exception. May it be another portion of my code helping to cause that, as well? – Glitteropia Oct 29 '16 at 00:55
  • Yes! @Glitteropia, try to change what I said...Maybe your static field was set to null in some other portion of the code.. – Bruno Oct 29 '16 at 01:00
  • Okay, so I tried that and it didn't work either, so I tried commenting out that whole section and it STILL doesn't work. I copy and pasted the errors: 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) – Glitteropia Oct 29 '16 at 01:16
  • This error is happening on ProductManagementServlet line 140. You are passing a `null` String to Double.valueOf(String). – Bruno Oct 29 '16 at 01:24
  • Could you edit you post and show us the line 140 of that class? – Bruno Oct 29 '16 at 02:46
1

You wantto return ArrasyList <Products> and you declare this as a private variable in your ProductIO class.

Remove the private static ArrayList<Product> products = null; Initialize and build this new one inside the method and return that ArrayList<Product> products = new ArrayList<Product>();.

Edit: From the comments/discussion above, this ANSWER is not relevant anymore for the question/issue.


Update

Based on user discussion in another answer submitted..

  String priceAsString = t.nextToken();
  double price = 0.0;
 //Check for null
  if(priceAsString  != null && !priceAsString  .isEmpty())
  {
    price =  Double.parseDouble(priceAsString);
  }
Searching
  • 2,269
  • 1
  • 17
  • 28