• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime Error (ArrayIndexOutOfBoundsException)

 
Deepak Naithani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Employee.java


Output:
Enter the Number of Employee:3
Enter the Employee Id:2
Enter the Employee Number:52
Enter the Employee Name:deepak
Enter the Employee Id:4
Enter the Employee Number:62
Enter the Employee Name:Nitin
Enter the Employee Id:1
Enter the Employee Number:12
Enter the Employee Name:anil
Size of Array list:0
Size of Array list:3
Employee@19189e1
Employee@1f33675
Employee@7c6768
 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error occurs because you are trying to access an array outside of its bound. For example you define an array that can store 4 elements. But you are trying to access the array using index greater than the size - 1. As we know that array is zero based, the index start from zero to size - 1.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can’t read your code well because you didn’t use the code button. You can get a very similar exception from Lists if you try to get more elements out of the List than you put into it.
 
Deepak Naithani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now the things are working very well till the point of adding to array list of type <Employee>.

problem is occurring on retrieving Object of <Employee> from array list.

Output (For five Employee) :
Employee@1f33675
Employee@7c6768
Employee@1690726
Employee@5483cd
Employee@9931f5

{Output for one employee
Enter the Number of Employee:1
Enter the Employee Id:1
Enter the Employee Number:2
Enter the Employee Name:deepak
Size of Array list:0
Size of Array list:1
Employee@1f33675}

it is showing a hash code value too

For Sorting
Collections.sort(emplist, new EmployeeComparator());

Class EmployeeComparator where i'm overriding the compare() method of comparator.

my code for retrieving from array list is:

unsorted is of String type.

for (int i = 0; i < emplist.size(); i++) {
unsorted = emplist.get(i).toString();
System.out.println(unsorted);
}

I knew that all things are getting wrong at this place, please suggest me how to get the Object from Array List it should be retrieved by <Employee> type or casting should be done.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure what you are asking but if it is the string that is printed out that is the problem, then you need to override the toString method in your Employee class to print out something useful.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are getting Objects out of your List, and they are of type Employee. Read this, which I hope will explain what is happening.
 
Tony Docherty
Bartender
Posts: 2957
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew that all things are getting wrong at this place, please suggest me how to get the Object from Array List it should be retrieved by <Employee> type or casting should be done.

I'm not sure what you mean by this as you are getting the Employee object out if the ArrayList and are then calling toString() on it. If your problem is the print out is not printing details of the Employee it's because your Employee class doesn't override the toString() method and return a suitable string containing the details you want to output.

Edit: Beaten by 2 people this time, my typing must be getting slower.
 
Deepak Naithani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Overriding toString() method will solve my problem. Can anyone give me a sample example for it. How should i built this method just provide me start...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Naithani wrote:How should i built this method just provide me start...

This is a good place to start

And this is the method you need to override.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic