Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList with JavaBean

 
Booma Devi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to Java.. I need help please..

I have one CSV file like the below (without heading)

AAA,123
BBB,125

I need to read the CSV file and mapping to java bean.. I wrote the code like this.. But it doesn't work...


javaBean:

Main Class:




I want to return the list of elements (Name, Age)....
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Booma Devi,

Please post complete code. I'm assuming that class CsvReader works properly.

Also, what is 'products' at line 15 in PersonDao class? (you are calling it as Main class, but I do not see any main method).

Next to this, you are returning a List of Person objects, and each Person contains a name and age (why age is String by the way?), and if I understand correctly, you want to return simply name and age pairs instead of List of Person object.

In this case, you can use a Map (say HashMap), but again, you'll lose duplication with it. e.g. if you are having name as key then you cannot put two persons with same name but different age in HashMap (which is possible in List).

So, I would prefer what you are doing - returning the List itself.

I hope this helps.
 
Booma Devi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anayonkar,

Thank you so much for your quick response... My requirement is I need to send the list of Person objects to the client side (Flex)...


 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to create a new Person object for every line you read. Move the object creation code inside your while loop, so that for each line a new object is created and added into the List.
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Booma Devi wrote:Hi Anayonkar,

Thank you so much for your quick response... My requirement is I need to send the list of Person objects to the client side (Flex)...





You should also return list after try catch block.. suppose while working you get some exception that will be handled in catch block but tell me in that case you will not be able to return the type list, which is wrong as method return type is List<Person> .
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajat's correct - I missed to see the stated error in the code block.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
Booma Devi wrote:I got some error here like "This method must return a result of type List<Person>"

This is because your return statement is in try block. Outside the block, there's no return statement.
I would suggest to put only relevant code inside try block and everything else should be outside try block. Also, do not catch Exception directly (rather catch some meaningful exception like FileNotFoundException etc.) e.g.
declare list
try {
read file
populate file data
}
catch (file operation related exception) {
print stack trace etc.
return null - because there's no point in iterating over file data now
}
iterate over the file data and populate list
return list


Secondly, in your main method, you are calling getList two times, and during first invocation, you are not storing the returned value in any variable. Please try to avoid this - you are doing all file operations two times unnecessarily.

I hope this helps.
 
Booma Devi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Anayonkar... I will try and update you.. Thanks for your tips..
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. You are welcome.
 
Booma Devi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Anayonkar, Now my code is working fine... Thank you so much for spent the time to read my code..
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome. I'm glad that you are able to fix your code
 
Booma Devi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to John and Rajat... :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic