• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Roster creation using string builder

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I created my roster class and I think I have done everything right. However now I am trying to test that by creating a roster in my player class tester.
Here is my Roster class:

And My tester class:


So I am having trouble assigning the players and the team name the way they are supposed to be.  I can't do it like I did for the players where its BaseballPlayer(player information) because I don't have a default constructor. I added one just to see what would happen but I came to realize it's only going to accept one player and I technically would need it to accept and infinite amount of players. I am stumped
 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Montgomery wrote:So I am having trouble assigning the players and the team name the way they are supposed to be.  I can't do it like I did for the players where its BaseballPlayer(player information) because I don't have a default constructor. I added one just to see what would happen but I came to realize it's only going to accept one player and I technically would need it to accept and infinite amount of players. I am stumped


I'm sorry, but that's a terrible description of your issue. You are assuming I know things, like how you used a 'default' constructor for BaseballPlayer, when that information is not provided. You have a list of Players, you can add as many as you want, what's the issue with this? "It's only going to accept one player", what's "it"? We'll need a lot more context, code, and more description of where you are stuck.
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest a single constructor that takes a teamName as an argument. You can't have a team that doesn't have a name.
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I would suggest a single constructor that takes a teamName as an argument. You can't have a team that doesn't have a name.


Okay and what about the players? Should the constructor take the players as an argument or maybe even the arrayList?
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Nathan Montgomery wrote:So I am having trouble assigning the players and the team name the way they are supposed to be.  I can't do it like I did for the players where its BaseballPlayer(player information) because I don't have a default constructor. I added one just to see what would happen but I came to realize it's only going to accept one player and I technically would need it to accept and infinite amount of players. I am stumped


I'm sorry, but that's a terrible description of your issue. You are assuming I know things, like how you used a 'default' constructor for BaseballPlayer, when that information is not provided. You have a list of Players, you can add as many as you want, what's the issue with this? "It's only going to accept one player", what's "it"? We'll need a lot more context, code, and more description of where you are stuck.


My apologies. "It" is the default constructor I made and removed from my program:


So when I tried that ^ I was only able to add one player to the roster like: Roster r1 = new Roster("Warriors", p1);
but even when I tried that I still received errors as such:
Exception in thread "main" java.lang.NullPointerException
at Roster.displayRoster(Roster.java:31)
at PlayerTester.main(PlayerTester.java:17)
My idea was that the constructor would take in the team name and each player as an argument
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have formatPlayerInformation() instead of just using toString() ?
Same question for displayRoster().
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Why have formatPlayerInformation() instead of just using toString() ?
Same question for displayRoster().


Just the instructions I was given by my instructor. I agree I could use those, but using those methods is apart of instructions/requirements
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Montgomery wrote:

Carey Brown wrote:I would suggest a single constructor that takes a teamName as an argument. You can't have a team that doesn't have a name.


Okay and what about the players? Should the constructor take the players as an argument or maybe even the arrayList?

You have an addToTeam() to add Players to the team; as many as you want by calling it as many times as you want. Personally I would have just called it add(Player) because the argument type shows you what you're adding, but I don't know what your requirements are. If I were trying to be more complete I might also have a method addAll( Collection<Player> ) where you could pass in a Collection of Players and have them all added to the team.
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:


So when I did that I received errors and that's why I am stuck.

Exception in thread "main" java.lang.NullPointerException
at Roster.addToTeam(Roster.java:25)
at PlayerTester.main(PlayerTester.java:14)

In theory I thought it would work, adding each player to the "Roster" and then setting the team name and then printing out the roster, but I receive that error above.
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please re-post the current state of your code so that we may match up the line numbers from the error message.
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does BasketballPlayer extend Player?
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Does BasketballPlayer extend Player?


Yes it does.

Roster class:


And tester class:

 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Does BasketballPlayer extend Player?


Here is my player class

 
Saloon Keeper
Posts: 5177
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your Roster class, is 'teamList'  always initialized?
 
Sheriff
Posts: 17351
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem reported is NullPointerException and the line is

The only thing here that can cause an NPE is teamList being null. So make sure teamList isn't null when you call this method.
 
Junilu Lacar
Sheriff
Posts: 17351
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your other fatal mistake is adding Lebron to the Warriors. That is sure to cause many exceptions in multiple cities.  
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The problem reported is NullPointerException and the line is

The only thing here that can cause an NPE is teamList being null. So make sure teamList isn't null when you call this method.


How do you suggest I do that? Because when I call the method I am adding players so that it won't be null, but are you saying it needs to be not null before I add players?
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Montgomery wrote:

Junilu Lacar wrote:The problem reported is NullPointerException and the line is

The only thing here that can cause an NPE is teamList being null. So make sure teamList isn't null when you call this method.


How do you suggest I do that? Because when I call the method I am adding players so that it won't be null, but are you saying it needs to be not null before I add players?


Are you initializing teamList anywhere?
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Nathan Montgomery wrote:

Junilu Lacar wrote:The problem reported is NullPointerException and the line is

The only thing here that can cause an NPE is teamList being null. So make sure teamList isn't null when you call this method.


How do you suggest I do that? Because when I call the method I am adding players so that it won't be null, but are you saying it needs to be not null before I add players?


Are you initializing teamList anywhere?


In my roster class yes and my tester class when I attempt to add information to it

Heres part of my roster class:

 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only place you set teamList is in setTeamList() which is never called. In most cases this would never be called, so how then do you initialize teamList without calling this method?
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how this fits into your requirements but I'm uncomfortable with these two methods because they allow callers to have direct access to private fields of the class.

IF you really need a getTeamList() method, it should return a copy of the private list so that the private list cannot be modified inadvertently.

In a similar fashion setTeamList() will clobber the contents of the current private teamList. I would have preferred seeing methods like addAllToTeam(Collection<Player>) and perhaps a clearTeamList() method. These might achieve a similar end but give some more control of the implementation back to the class.

You might decide that neither of these are necessary for your purposes.
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:The only place you set teamList is in setTeamList() which is never called. In most cases this would never be called, so how then do you initialize teamList without calling this method?


So should I call the method within the addToTeam method, or what?
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. You should probably never call that method, and I would feel a whole lot better if you removed it entirely (but, hey, that's just me).

You should initialize it on the same line where you declare it in the class header.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic