java - ArrayList add null pointer -
java - ArrayList add null pointer -
the next class keeps giving me null pointer when seek phone call addplayer method , have no thought heck i'm doing wrong. : maintain in mind simple stuff...supposedly... i'm learning java.
import java.util.*; public class team { private string teamname; private arraylist<player> players; private int numberofplayers; public team(string teamname) { this.teamname = teamname; } public string getname() { homecoming this.teamname; } public void addplayer(player player) { this.players.add(player); } public void printplayers() { (player player : this.players) { system.out.println(player); } } }
here player class :
public class player { private string name; private int goals; public player(string name) { this.name = name; } public player(string name, int goals) { this.name = name; this.goals = goals; } @override public string tostring() { homecoming this.getname() + ", goals " + this.goals(); } public string getname () { homecoming this.name; } public int goals() { homecoming this.goals; } }
this problem:
private arraylist<player> players;
is not initialized, never create new 1 when instantiate class.
add next constructor:
public team(string teamname) { this.teamname = teamname; this.players = new arraylist<player>(); }
java arraylist
Comments
Post a Comment