Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning a Class with Multiple Parameters

 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a class with two types of parameters (String and int). How do take variables and set them as those parameters? My code below...

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not following conventions makes code surprisingly difficult to read. Vairable names should start with a lowercase character. wins not Wins, etc...

this is where the compiler is having issues...


In the future, it's best to show the exact issues that the compiler is having. But in this case it's easy to see that your statement is missing a keyword...
 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I made some changes. Namely, I made a method in the Team class to set the two variables. Now it compiles just fine, but I'm getting a runtime error: "Exception in thread "main" java.lang.NullPointerException" when I input the first team name... What am I doing wrong now?

 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may be using an incorrect syntax? What exactly is it you're trying to do here? Get an Array of teams with their wins, etc?

I would use an ArrayList or LinkedList of "Team"s, set the team's information first, and then add the team to the ArrayList. This way if you want to ever add information to the team and, consequently, the array it doesn't cause trouble later....

Someone with more experience might be able to better help you.
Janeice
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an array that can reference Teams, set to a size, but you don't actually have any Teams.
 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:You have an array that can reference Teams, set to a size, but you don't actually have any Teams.


That's actually what I am trying to do on line 34, is to assign the first element of the first team. Do I need to set all the teams to some default value or something and then assign them? How do I assign them all an initial default value of say: "" and 0?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you don't have a Team to set properties on. You have an array of Team references, but no Teams.
 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:But you don't have a Team to set properties on. You have an array of Team references, but no Teams.


isn't that what the "new" keyword is going on line 62? What if in the Team Constructor method, I put:
. Would that initialize all the teams so I don't get null pointer errors later?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim:

Sorry, you're just creating the array with that new. You need to instantiate each element of an array individually, and no, setting the parameters won't change that.

John.
 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:Jim:

Sorry, you're just creating the array with that new. You need to instantiate each element of an array individually, and no, setting the parameters won't change that.

John.


Thanks, gang. I figured it out. The Constructor method required arguments. Once I removed that requirement, I was able to create a loop to instantiate it just fine...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic