Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Array of a Class

  1. #1
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Array of a Class

    Hi, I'm triying to fill an array of class "Months"

    All I'm triying to fill is something like this: "array[position].atribute"
    Every Month must have name and days;

    I get java.lang.NullPointerException , I don't understand why, I tryed using getter and setter but I don't fix it, I don't understand what's wrong, I don't get any compilation errors.

    Thank you.
    import java.util.Scanner;
     
    public class Arrays {
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		Months [] array = new Months[12];
     
    		for(int i=0; i<array.length; i++){
    			System.out.println("Introduzca el nombre del mes:\t"+(i+1));
    			array[1].name = keyboard.nextLine();
    			System.out.println("Introduzca los días que tiene el mes:\t");
    			array[i].days = keyboard.nextInt();
    		}
    		keyboard.close();
    		for(int i=0; i<array.length; i++){
    			System.out.println(array[0].name);
    			System.out.println(array[0].days);
    		}
    	}
    }
    class Months {
    	String name;
    	int days;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array of a Class

    I get java.lang.NullPointerException
    Please copy the full text of the error message and post it here. It has information about where the error happened.

    Where does the code put any Months objects into the array? The array has been defined but no Months objects have been put into it so all its slots have null values.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Array of a Class

    Exception in thread "main" java.lang.NullPointerException
    at Ejercicio03.main(Ejercicio03.java:26)

    The name of the class is really Ejercicio03 and there is more lines but I simplified because if i fix to add a name i will know to add more attributes tt

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array of a Class

    Our posts might have crossed.

    Where does the code put any Months objects into the array? The array has been defined but no Months objects have been put into it so all its slots have null values.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Array of a Class

    Sorry for my noob question, but, are you telling me that I need to instance 12 objects and full my array with these 12 objects? like this:

     
    Months m = new Months();
    Months[] array = new Months[12];
     
    for(int i=0;i<array.length;i++)
    	array[i]=m;

    But how could I loop for do 12 objects with different attributes each??

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array of a Class

    Inside the loop create a new instance with the different attributes before adding it to the array.

    Your loop adds a reference to the same (one) instance of the object to all the slots of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Rexshine (January 20th, 2013)

  8. #7
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Array of a Class

    Thank you very much, now I understand it better, I did this:
    Meses [] array = new Meses[2];
     
    for(int i=0;i<array.length;i++)
    	array[i]= new Meses();
    I still had problems, but the problem is now solved, I'm just blind I was filling the months fine, but all positions of array show me the First position.
    If you check the code I always showing array[0] (12 times -.-)

    Now works great.

    Thank you again.

Similar Threads

  1. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  2. [SOLVED] Storing instances of a Sub Class in it's Super Class (Which is an array)
    By Hiroto in forum Object Oriented Programming
    Replies: 5
    Last Post: November 6th, 2011, 10:36 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM
  5. HELP. Passing Array to class.
    By videodrome in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 13th, 2010, 04:35 AM