wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Runtime Error (ArrayIndexOutOfBoundsException) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime Error (ArrayIndexOutOfBoundsException)" Watch "Runtime Error (ArrayIndexOutOfBoundsException)" New topic
Author

Runtime Error (ArrayIndexOutOfBoundsException)

Deepak Naithani
Greenhorn

Joined: Aug 30, 2012
Posts: 25

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

Joined: Feb 05, 2004
Posts: 104

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.


Website: Learn Java by Examples
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Aug 30, 2012
Posts: 25
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

Joined: Aug 05, 2005
Posts: 3429
    
  12
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.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Aug 07, 2007
Posts: 2173
    
  47
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

Joined: Aug 30, 2012
Posts: 25
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

Joined: Aug 05, 2005
Posts: 3429
    
  12
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Runtime Error (ArrayIndexOutOfBoundsException)
 
Similar Threads
Converting Number to Word
Problems w/ a HashMap
Linked List Problem
Program to print numbers from 0 to 100 in words
Using java.util.HashSet for Custom Class