Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

setting ArrayList value using constructor string. Out of Bounds exception

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to Java and I keep trying to debug my code to no avail. I'm trying to use arrayList to build an array for a newEmployee and append it an arraylist of employees ( ArrayList<ArrayList<String>> ).  The dateParse, nameParse and addressParse are separate methods being called within this array method.

Where error is:



From main, the variables are input correctly and sent within specifications, but I get an error at

this.currentEmployee.set(0, number);



If anyone can help understand how to make this work or why it's not working that would be amazing!
 
Saloon Keeper
Posts: 13851
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When asking questions about an exception, please post the full stack trace.
 
Marshal
Posts: 75646
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you got a List<String> instead of individual fields? You should have fields called name, address, etc., and maybe Name, Address, classes to encapsulate those data.
What does the documentation for List#set() say? Have you got at least enough elements in the List before you call set() to accommodate the indices you are setting?
 
Bartender
Posts: 4911
185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not only that, but if the first constructor is used before the second, then currentEmployee is null.
 
Casey Jayne
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:When asking questions about an exception, please post the full stack trace.



Do you mean the full exception? Okay, sorry about that- this is my first post here I've just been reading until this point.  I will edit.
 
Casey Jayne
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why have you got a List<String> instead of individual fields? You should have fields called name, address, etc., and maybe Name, Address, classes to encapsulate those data.
What does the documentation for List#set() say? Have you got at least enough elements in the List before you call set() to accommodate the indices you are setting?



I agree that it's not the design I would choose, but the assignment is to use separate classes for the input and parsing methods (address, class, etc) and then to store everything within an Employee array and at the end of processing multiple employees, to print all of them.  The best way I could think about handling that was with a nested ArrayList<ArrayList<String>> that could handle the inputs.  

I understand what you mean about the set.  I will try allocating 10 slots to start and then calling set and let you know if that works.  
 
Casey Jayne
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Not only that, but if the first constructor is used before the second, then currentEmployee is null.



Isn't only one constructor used if there is only one call? Currently, the call that I'm using from main is:

NewEmployee emp = new NewEmployee(number, name, address, hireDate);

Sorry if that's a really elementary question.  I just started learning about constructors a week ago.  I appreciate all of your help.
 
Piet Souris
Bartender
Posts: 4911
185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you have this in main

you will get a NullPointerException.

A simple way to prevent that is to initialize the ArrayList right away by using:

But a thing to ask yourself is:
Why would a class called 'NewEmployee' that, as Campbell writes, should contain fields like name, adress, function, et cetera, also contain a List of currentEmplyees, and even a List of Lists of currentEmployees?
 
Saloon Keeper
Posts: 9266
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Casey Jayne
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:if you have this in main

you will get a NullPointerException.

A simple way to prevent that is to initialize the ArrayList right away by using:


I tried this and unfortunately it didn't help.

But a thing to ask yourself is:
Why would a class called 'NewEmployee' that, as Campbell writes, should contain fields like name, adress, function, et cetera, also contain a List of currentEmplyees, and even a List of Lists of currentEmployees?



This is a good way to think about it! I've been working on a rewrite- thanks for helping me with object oriented thinking.
 
Casey Jayne
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:



You're right.  I now have it so that the NewEmployee class has all of the sugested attributes and one String array.  I have a separate class file of AllEmployees that contains an arrayList
 
Carey Brown
Saloon Keeper
Posts: 9266
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of this, create a field 'hireDate' that is of type LocalDate and use these 3 parameters to construct the hireDate. There are lots of things that you can do with a LocalDate that are very difficult to do with just month/day/year, like compare two dates.
 
Carey Brown
Saloon Keeper
Posts: 9266
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a reminder: it helps to periodically re-post your code in its current state so that we keep in sync with you and are not making comments on your prior posts that may no longer apply.
 
Casey Jayne
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Just a reminder: it helps to periodically re-post your code in its current state so that we keep in sync with you and are not making comments on your prior posts that may no longer apply.



Thank you-- this assignment is due tonight so I have to turn it in as it is currently, even though I never got it working.  I still think it's better than before I asked.
Thank you for your help!
 
Casey Jayne
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Instead of this, create a field 'hireDate' that is of type LocalDate and use these 3 parameters to construct the hireDate. There are lots of things that you can do with a LocalDate that are very difficult to do with just month/day/year, like compare two dates.



Thanks- that's a good tip.  Unfortunately we were not allowed to use the local date or java date methods.
 
Campbell Ritchie
Marshal
Posts: 75646
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Casey Jayne wrote:. . . the assignment is to use separate classes for the input . . . and then to store everything within an Employee array . . . .  

But you haven't got a separate input class. And I wouild interpret that bit about employee array as meaning that an employee has name, address, etc. fields.
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic