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 6 of 6

Thread: Array with null (same for Arraylist)

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default (Solved) Array with null (same for Arraylist)

    I'm having a problem with calling arrays from a class. I know I should should separate the user interface from the logic so that's what I've tried to do. If you know of a better way please mention that too

    Anyway, when ever I use an array list, it always starts with "null". The only fix I've been able to find is by chopping off the first few characters.

    This is an example of which the output is:

    nullname1
    name2
    name3
    name4
    name5
    student students = new student();
    System.out.println(students.getStudent());

    public class student {
     
    	private String student;
     
    	public String getStudent() {
     
    	String[] students = {"name1","name2","name3","name4","name5"};
     
    		for (int i = 0; i < students.length; i++) {
    			student = student + students[i] + "\n";
    		}
     
    	return student;
    	}
     
     
    }

    Thanks
    Last edited by Alex L; December 5th, 2011 at 12:50 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Array with null (same for Arraylist)

    Write out exactly how your look is working, concentrating on the initial value of the variable student (hint, String is an object which when not instantiated to any value is by default null)

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array with null (same for Arraylist)

    You never initially initialized the student string so Java by default assigns it the value of null. Before your for loop, try setting student to the empty string.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array with null (same for Arraylist)

    Changed

    private String student;

    to

    private String student = "";

    Is this right or have a just done quick fix and missed the point?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array with null (same for Arraylist)

    That works the first time getStudent() is called, but what happens if someone calls getStudent() again?

    There's really no reason student should be an object field, move it in as a local variable of getStudent().

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array with null (same for Arraylist)

    You're quite right. My solution was poor. I've re-read some principles and am starting again. Thanks for you help

Similar Threads

  1. converting array into arraylist
    By Harry Blargle in forum Collections and Generics
    Replies: 6
    Last Post: October 30th, 2011, 03:38 AM
  2. Array list returns null
    By Scotty in forum Collections and Generics
    Replies: 3
    Last Post: April 14th, 2011, 06:50 AM
  3. Array of strings Null Pointer error
    By FredrichGauss in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 12:43 AM
  4. [SOLVED] Error: Null Exception on Array of classes
    By g000we in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 09:14 AM
  5. How do you set an object in array to null value?
    By Arius in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 03:50 AM