• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList not loading on JSP

 
Ram Cruz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I 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:



Here is my code snippet in CarPartService:

// Method for retrieving carPartArrayList


**Here is my servlet code snippet:**



**Here is my jsp code snippet:**

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most likely your ArrayList is empty. The way to know for sure is like this:



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.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ram Cruz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote: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.
 
Ram Cruz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Most likely your ArrayList is empty. The way to know for sure is like this:



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.


Thanks, I will try that. I do have a feeling it's returning empty to the JSP, just can't figure out how. Is there a way to trow an exception in the JSP or servlet if the arrayList is returning null?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ram Cruz wrote: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.


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.
 
Ram Cruz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Ram Cruz wrote: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.


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.


Paul could this be because I'm using an absolute path for the text file. The reason why I'm doing this is because I cannot read the text file or write to it using relative path so I'm using absolute path for the time being. So when I run my application from Netbeans and Tomcat, this absolute path is ignored? Is that why it's empty?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ram Cruz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blame other code? Obviously I don't have the rest of the code posted. If you see the full code I have file= (Local drive the txt file is located). I posted the code snippets where I think I'm having issue and felt there is no need to post the rest that has nothing to do with it. I also commented out my original code that get the relative path. I turn it off because it would not read the file and used absolute path to continue with the rest of the code. Anyways thanks for your help I will continue to debug my issue and try to solve the relative path issue as well.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ram Cruz wrote:I posted the code snippets where I think I'm having issue and felt there is no need to post the rest that has nothing to do with it.

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.
 
Ram Cruz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good, I'm not trying to argue at all. I'm taking your advices just trying clarify things. I'm just a student trying to learn from the experts. I apologize if I gave you the wrong impression. I will post tonight when I have access to it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!