• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Assigning a Class with Multiple Parameters

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic