ArrayList not loading on JSP

ArrayList not loading on JSP 

 have an ArrayList method in CarPartService that returns an ArrayList of CarPart object. In my servlet CarPartServlet, I call this method to retrieve an ArrayList of CarPart object which is being read from a text file and sharing it to the jsp by request.setAttribute("partList", partList). In my JSP, I want to load these objects(name) in a combo box. That's where my issue is at the moment, I see nothing. I'm stuck cannot figure out where I went wrong. I have done some testing through a main method in CarPartService to make sure it is reading the text file and creating the ArrayList properly. Also, my CarPart object has all the appropriate getter and setter methods for its fields. I'm using tomcat 7 and jstl1.1 for the Jsp forEach tags. Thanks in advance!

CarPart object:

view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
 [javadoc] public class CarPart{  
        // Declare variables  
        String partID;  
        String name;  
        String description;  
        String price;  
        String quantityInStock;  
          
        // Default constructor  
        public CarPart(){  
            this("","","","","");  
        }  
        // Constructor for creating new CarPart object  
        public CarPart(String partID, String name,String price, String quantityInStock,   
                String description){  
            this.partID = partID;  
            this.name = name;  
            this.description = description;  
            this.price = price;  
            this.quantityInStock = quantityInStock;  
    }  
        // Get and Set Methods  
        public String getPartID(){  
            return partID;  
        }  
         public void setPartID(String partID){  
            this.partID=partID;  
        }  
        public String getName(){  
            return name;  
        }  
        public void setName(String name){  
            this.name=name;  
        }  
        public String getPrice(){  
            return price;  
        }  
        public void setPrice(String price){  
            this.price=price;  
        }  
        public String getQuantityInStock(){  
            return quantityInStock;  
        }  
        public void setQuantityInStock(String quantityInStock){  
            this.quantityInStock=quantityInStock;  
        }  
        public String getDescription(){  
            return description;  
        }  
        public void setDescription(String description){  
            this.description=description;  
        }  
    }  
[/javadoc] 
 [javadoc] public class CarPart{
        // Declare variables
        String partID;
        String name;
        String description;
        String price;
        String quantityInStock;
       
        // Default constructor
        public CarPart(){
            this("","","","","");
        }
        // Constructor for creating new CarPart object
        public CarPart(String partID, String name,String price, String quantityInStock,
                String description){
            this.partID = partID;
            this.name = name;
            this.description = description;
            this.price = price;
            this.quantityInStock = quantityInStock;
    }
        // Get and Set Methods
        public String getPartID(){
            return partID;
        }
         public void setPartID(String partID){
            this.partID=partID;
        }
        public String getName(){
            return name;
        }
        public void setName(String name){
            this.name=name;
        }
        public String getPrice(){
            return price;
        }
        public void setPrice(String price){
            this.price=price;
        }
        public String getQuantityInStock(){
            return quantityInStock;
        }
        public void setQuantityInStock(String quantityInStock){
            this.quantityInStock=quantityInStock;
        }
        public String getDescription(){
            return description;
        }
        public void setDescription(String description){
            this.description=description;
        }
    }
[/javadoc]

Here is my code snippet in CarPartService:

// Method for retrieving carPartArrayList
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
    public ArrayList<CarPart> getCarPartArrayList(){  
        partList = new ArrayList<CarPart>();  
        CarPart carPart=null;  
        String ptID;  
        String nm;  
        String desc;  
        String pr;  
        String qty;  
        // Read text file  
        try {  
            Scanner scanner=new Scanner(new FileReader(file+"ItemsCatalog.txt")).useDelimiter(":");        
            while (scanner.hasNext()){  
                ptID=scanner.next();  
                nm=scanner.next();  
                pr=scanner.next();  
                qty=scanner.next();  
                desc=scanner.nextLine();  
                // Add to arraylist  
            carPart=new CarPart(ptID,nm,pr,qty,desc);  
            partList.add(carPart);  
         }  
      }  
      catch (FileNotFoundException exception) {  
            System.err.println("FileNotFoundException: " + e.getMessage());  
      }  
        return partList;  
    } 
    public ArrayList<CarPart> getCarPartArrayList(){
        partList = new ArrayList<CarPart>();
        CarPart carPart=null;
        String ptID;
        String nm;
        String desc;
        String pr;
        String qty;
        // Read text file
        try {
            Scanner scanner=new Scanner(new FileReader(file+"ItemsCatalog.txt")).useDelimiter(":");     
            while (scanner.hasNext()){
                ptID=scanner.next();
                nm=scanner.next();
                pr=scanner.next();
                qty=scanner.next();
                desc=scanner.nextLine();
                // Add to arraylist
            carPart=new CarPart(ptID,nm,pr,qty,desc);
            partList.add(carPart);
         }
      }
      catch (FileNotFoundException exception) {
            System.err.println("FileNotFoundException: " + e.getMessage());
      }
        return partList;
    }

**Here is my servlet code snippet:**

view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
 public class CarPartServlet extends HttpServlet {  
        public void doPost(HttpServletRequest request, HttpServletResponse response)   
                throws IOException, ServletException {  
            //Jsp page  
            String page = "ItemsCatalog.jsp";  
            //Declare the dispatcher for the View  
            RequestDispatcher view=null;   
            // Dispatch the request  
            processRequest(request, response);  
            // Creat a CarPrtService object  
            CarPartService cpSvc = new CarPartService();  
            try{  
                //New ArrayList   
                ArrayList partList = new ArrayList();  
                partList = cpSvc.getCarPartArrayList();  
                request.setAttribute("partList", partList);  
                // Dispatch request  
                view=request.getRequestDispatcher(page);  
                view.forward(request, response);  
            }  
            catch (Exception e) {  
           System.out.println(e); }  
        } 
 public class CarPartServlet extends HttpServlet {
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            //Jsp page
            String page = "ItemsCatalog.jsp";
            //Declare the dispatcher for the View
            RequestDispatcher view=null;
            // Dispatch the request
            processRequest(request, response);
            // Creat a CarPrtService object
            CarPartService cpSvc = new CarPartService();
            try{
                //New ArrayList
                ArrayList partList = new ArrayList();
                partList = cpSvc.getCarPartArrayList();
                request.setAttribute("partList", partList);
                // Dispatch request
                view=request.getRequestDispatcher(page);
                view.forward(request, response);
            }
            catch (Exception e) {
           System.out.println(e); }
        }
 

**Here is my jsp code snippet:**

view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
    <p><b>Parts Selection</b></p><hr>  
       <table>  
        <select>  
          <c:forEach var="CarPart" items="${partList}" >  
                 <option>${CarPart.partID}</option>  
            </c:forEach>  
       </select>  
       </table> 
Most likely your ArrayList is empty. The way to know for sure is like this:

view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
          <c:forEach var="CarPart" items="${partList}" >    
                 <option>XXX</option>    
            </c:forEach>   
          <c:forEach var="CarPart" items="${partList}" > 
                 <option>XXX</option> 
            </c:forEach>  

But assuming it's empty, now you have to find out why. My guess is that your code is throwing an exception and then ignoring it (which you shouldn't do, at least log the exception) and returning the empty list. And my guess for why that's happening is that it can't find the file you told it to look in.

All of this could be cleared up if you didn't ignore exceptions and if you did some debugging -- for example is the code in the loop being executed at all, and so on.

Your code looks alright except the getCarPartArrayList() method. Is the file available? I see you catch the FileNotFoundException but do nothing (bad practice).

Also there is a simpler approach than using Scanner class just for the delimiter.
Thanks, I meant to put something there but forgot to do so. I edited my original post. The file is being read correctly. I have done a quick test in a main method that calls the getCarPartArrayList() then printed the elements by overwriting toString method. So I know that's working. "Also there is a simpler approach than using Scanner class just for the delimiter". Can you please tell me what you mean by this or how to do it.

I'm not convinced by that. Now, if your quick test was run on the same machine as the place where your servlet is running, and if the value of the "file" variable (whose declaration and value I don't see) was the same in both cases, then your test would be valid. But I suspect you have configuration or environment differences which you haven't taken into account.

So you should do some debugging, as I suggested before. Insert some code which logs what's actually happening into the code where you don't know what is happening.

I don't see where you are using an absolute path. I see some code which might do that, if a particular variable were set correctly. But I don't see any code which sets that variable. That code might exist, but you're asking me to comment on it. Which I can't, of course.

And by the way it appears that you're using a classic form of problem-solving in which you try to blame other code for your problems rather than looking at your own code. (This is extremely common and hard to avoid until you know you should be looking out for it.) When you start coming up with bizarre ideas like Netbeans and Tomcat ignoring absolute paths, that should suggest to you that you are on the wrong track. The right track would be to suspect that perhaps you aren't using an absolute path after all. So you should check your code to see what file path you are actually using. A little debugging should tell you that.

Firstly, it does have something to do with it. How the file variable is set is very germane to the problem.

Secondly, dismissing the help of people who are volunteering their own time to try and help you isn't a winning strategy. I would suggest that instead of arguing, show the code that more experienced people know to be relevant even if you do not think so.

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright