#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    What is wrong with my code?


    Basically i have to create 5 objects of the class Person and then sort the array of objects (here array name is" persons") in accending order of their ages..

    Code:
    public class Person {
    	String name;
    	int age;
    	
    	Person(String name, int age) {
    		this.name = name;
    		this.age = age;
    	}
    	
    	void birthday () {
    		age++;
    		System.out.println(name + " is now " + age);
    	}
    }
    Code:
    public class HelloWorldPerson {
    	public static void main (String [] args) {
    		String name[] = { "Rohan", "Ankur", "Giri", "Aaditya", "Suhail"};
    		int age[] = {19, 20, 40, 80, 30};
    		
    		Person [] persons = new Person [10];
    		Person [] sorted_array = new Person [10];
    		
    		for(int i = 0; i< 5; i++) {
    			persons[i] = new Person(name[i], age[i]);
    			System.out.println("Name : " + persons[i].name + " Age : " + persons[i].age);
    		}
    		
    		sorted_array = persons.clone();
    		
    		System.out.println("******Sorted_Array******");
    		
    		for( int i = 0; i < 5; i++) {
    			Person temp;
    			if(sorted_array[i].age >sorted_array[i+1].age) {
    				temp = sorted_array[i];
    				sorted_array[i] = sorted_array[i+1];
    				sorted_array[i+1] = temp;
    				}
    			}
    		
    		for(int i = 0; i < 5; i++) {
    			System.out.println("Name : " + sorted_array[i].name + " Age : " + sorted_array[1].age);
    		}
    		
    		                
    	}
    	
    }
    but getting an error...no clue why...

    Code:
    Name : Rohan Age : 19
    Name : Ankur Age : 20
    Name : Giri Age : 40
    Name : Aaditya Age : 80
    Name : Suhail Age : 30
    ******Sorted_Array******
    Exception in thread "main" java.lang.NullPointerException
    	at HelloWorldPerson.main(HelloWorldPerson.java:21)
    please help...
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    Originally Posted by datus
    Code:
    			if(sorted_array[i].age >sorted_array[i+1].age) {
    What is the value of "i + 1" when going through the loop (hint - what about the last time through)? Is there a value set in the array at that index?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0
    ok i got it ...silly at my part...

    buy now facing another problem ....array is still not sorted..for some value sets of age....for others it shows correct response...

    Code:
    public class HelloWorldPerson {
    	public static void main (String [] args) {
    		String name[] = { "Rohan", "Ankur", "Giri", "Aaditya", "Suhail"};
    		int age[] = {19, 100, 40, 80, 50};
    		
    		Person [] persons = new Person [10];
    		Person [] sorted_array = new Person [10];
    		
    		for(int i = 0; i< 5; i++) {
    			persons[i] = new Person(name[i], age[i]);
    			System.out.println("Name : " + persons[i].name + " Age : " + persons[i].age);
    		}
    		
    		sorted_array = persons.clone();
    		
    		System.out.println("******Sorted_Array******");
    		
    		for( int i = 0; i < 4; i++) {
    			Person temp;
    			if(sorted_array[i].age >sorted_array[i+1].age) {
    				temp = sorted_array[i];
    				sorted_array[i] = sorted_array[i+1];
    				sorted_array[i+1] = temp;
    				}
    			}
    		
    		for(int i = 0; i < 5; i++) {
    			System.out.println("Name : " + sorted_array[i].name + " Age : " + sorted_array[i].age);
    		}
    		
    		                
    	}
    	
    }
    Code:
    Name : Rohan Age : 19
    Name : Ankur Age : 100
    Name : Giri Age : 40
    Name : Aaditya Age : 80
    Name : Suhail Age : 50
    ******Sorted_Array******
    Name : Rohan Age : 19
    Name : Giri Age : 40
    Name : Aaditya Age : 80
    Name : Suhail Age : 50
    Name : Ankur Age : 100
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0
    uh ...got it..

    working for new code....happy now

    Code:
    System.out.println("******Sorted_Array******");
    		
    		for(int j =0; j<5; j++) {
    		for( int i = 0; i < 4; i++) {
    			Person temp;
    			if(sorted_array[i].age >sorted_array[i+1].age) {
    				temp = sorted_array[i];
    				sorted_array[i] = sorted_array[i+1];
    				sorted_array[i+1] = temp;
    				}
    			}

IMN logo majestic logo threadwatch logo seochat tools logo