0

Good day everyone! I'm a newbie at java, especially at OOP. I was tasked to write a program with three classes about a registration of basketball and volleyball team. This is a simple algorithm there is one thing that I can't get through. I have three classes, "Member", "Teams", and "Test" (where my main at). I kept receiving a NullPointerException whenever I run my program. Here is my code.

Team Class

package MyLibs;

/**
 *
 * @author anonymous
 */
public class Team {

    public String name;
    public Member [] members;
    public int memberCnt;
    public int maxMember;
    public int minAge;
    public int maxAge;

    public Team(String name, int maxMember, int minAge, int maxAge) {
        this.name = name;
        this.maxMember = maxMember;
        this.minAge = minAge;
        this.maxAge = maxAge;
    }

    public void addMember(Member  member){
        members[memberCnt] = member;
        memberCnt++;
    }

    public boolean checkQualification(Member member){

        if (member.age < minAge || member.age > maxAge)
            return false;
        else
            return true;

    }



}

Member Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MyLibs;

/**
 *
 * @author anonymous
 */
public class Member {

    public String name;
    public int age;

    public Member(String name, int age) {
        this.name = name;
        this.age = age;
    }




}

Main Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MyApp;

import java.util.Scanner;
import MyLibs.*;

/**
 *
 * @author anonymous
 */
public class Test {
    public static void main(String[] args) {
        String bBall_name;
        int bBall_age;
        String vBall_name;
        int vBall_age;
        boolean res;
        int choice;


        Scanner scan = new Scanner(System.in);

        System.out.println("Select Team");
        System.out.println("-----------------");
        System.out.println("[1] Basketball");
        System.out.println("[2] Volleyball");
        System.out.println("[3] Exit");
        System.out.println("-----------------\n");

        do{
        System.out.print("Enter choice: ");

        choice = scan.nextInt();
        scan.skip("\n");

        switch (choice){

            case 1:

                System.out.println("\nYou choose Basketball!\n");
                System.out.print("Name: ");
                bBall_name = scan.nextLine();
                System.out.print("Age: ");
                bBall_age = scan.nextInt();


                Team basketball = new Team(bBall_name, 5, 18, 30);
                Member bBall_mem = new Member(bBall_name, bBall_age);



                if(basketball.memberCnt == basketball.maxMember){
                    System.out.println("\nBasketball team is no longer accepting applicants.\n");
                    break;
                }

                res = basketball.checkQualification(bBall_mem);

                if(res == true){
                    System.out.println("\nWelcome to the Basketball team!\n");
                    basketball.addMember(bBall_mem);
                }else{
                    System.out.println("\nSorry, you are not qualified!\n");
                    break;


                }



                break;
            case 2:


                System.out.println("\nYou choose Volleyball!\n");
                System.out.print("Name: ");
                vBall_name = scan.nextLine();
                System.out.print("Age: ");
                vBall_age = scan.nextInt();


                Team volleyball = new Team(vBall_name, 3, 14, 20);
                Member vBall_mem = new Member(vBall_name,vBall_age);

                if (volleyball.memberCnt == volleyball.maxMember){
                    System.out.println("Volleyball team is no longer accepting applicants.\n");
                    break;
                }

                res = volleyball.checkQualification(vBall_mem);

                if(res == true){
                    System.out.println("\nWelcome to the Volleyball team!\n");
                    volleyball.addMember(vBall_mem);

                }else{
                    System.out.println("\nSorry, you are not qualified!\n");
                    break;
                }

                break;




        }

    }while(choice != 3);


    }

}

The error occurs at the

basketball.addMember(bBall_mem);

and

volleyball.addMember(vBall_mem);

I don't know why it's always getting that error. I already check and I don'y think it is null. Please go easy on me, this is really my first time.Thank you for everyone who will help me.

PS> I'm also new here :)

pokoyo
  • 19
  • 5
  • 1
    `public Member [] members;`: you don't look to assign a value to this field. Your `addMember` method also make it look like you think arrays will grow as you add elements - they don't; use a `List` instead. – Andy Turner Jun 07 '19 at 12:16
  • I'm sorry but I don't know that this already answered. – pokoyo Jun 07 '19 at 12:17
  • There are two things you should look at learn to fix stuff like that. Look at the stacktrace and what it tells you. Look at the first line which mentions one of your own classes to find the possible place in your code where the exception arises. Then use a debugger and look at the real values of each variable during runtime at that location in your code. Then you would see that `members` isn't an array like you expect it to be (see Andys comment). – Tom Jun 07 '19 at 12:20

1 Answers1

0

You don't initialize your Member array in your Team class.

Just add the below line in your Team class constructor :

this.members = new Member[maxMember];

Alternatively as others suggested, you should better use a List instead of an array for this

nullPointer
  • 4,419
  • 1
  • 15
  • 27