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

Thread: Issue with initialising Array

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Location
    Leeds, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Issue with initialising Array

    I've created an array of a self defined object 'Team'... but when I try to set a property using the array I get a NullPointerException.. Research shows this is because the Array needs to be initialised though I can't see what more I need to do.

    I am using a Method of my new object but that doesn't seem to be the problem - as you will see I am declaring another instance of my object and I can use the Method fine on that 'myTeam' . I get the exception from the line where I am using the Method on Array (index 3)

     
           team[] teams = new team[40];
            team myTeam = new team();
     
            try {
                myTeam.setName("WBA");
                teams[3].setName("QPR");
            } catch (
                       Exception e) {
            System.out.println("Error : " + e);
            e.printStackTrace();
        }
     
     
    public class team {
     
        private String teamName;
     
     
     public void setName(String name) {
         teamName = name;
     
       }
     
    }

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

    Default Re: Issue with initialising Array

    I get a NullPointerException
    Declaring an array of objects creates the structure with slots for instances of the class, but does NOT create the instances which means all the slots in the array have null values. You need to create instances of the class and assign them to the slots in the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Location
    Leeds, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issue with initialising Array

    Thanks

    I interpreted your comment as a need for code like this

                 for(i=0;i<40;i++)
                teams[i] = new team();

    This is what I have inserted and I'm not getting the error now; Is this the recognised way of doing this?

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

    Default Re: Issue with initialising Array

    Yes, that is a way to fill in an array, except use the array's length attribute not a hardcoded value like 40.

    My question about the code is why is it using an array instead of some Java collection like a List or Map?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2020
    Location
    Leeds, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issue with initialising Array

    Probably because I'm a recent immigrant to Javaland from other languages, so not yet practiced in reaching for other constructs

Similar Threads

  1. Need knowledge on initialising arrays of various types
    By crisdeodates in forum Java Theory & Questions
    Replies: 4
    Last Post: September 18th, 2014, 12:17 PM
  2. Issue with calling array
    By javaStooge in forum What's Wrong With My Code?
    Replies: 16
    Last Post: February 15th, 2014, 05:15 PM
  3. [SOLVED] Array Issue
    By Blasfemmy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2013, 10:26 PM
  4. [SOLVED] Issue when returning an array
    By Broxxar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2012, 10:19 PM
  5. Array issue
    By Mini83 in forum Collections and Generics
    Replies: 5
    Last Post: August 18th, 2011, 09:18 AM

Tags for this Thread