aspose file tools*
The moose likes Beginning Java and the fly likes Assigning a Class with Multiple Parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning a Class with Multiple Parameters" Watch "Assigning a Class with Multiple Parameters" New topic
Author

Assigning a Class with Multiple Parameters

Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
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

Joined: Jan 10, 2002
Posts: 60071
    
  65

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...


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
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

Joined: Sep 14, 2009
Posts: 1614
    
  10

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


When you do things right, people won't be sure you've done anything at all.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You have an array that can reference Teams, set to a size, but you don't actually have any Teams.
Jim Hester
Ranch Hand

Joined: Sep 19, 2009
Posts: 36
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 19, 2009
Posts: 36
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

Joined: Mar 09, 2009
Posts: 600
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

Joined: Sep 19, 2009
Posts: 36
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning a Class with Multiple Parameters
 
Similar Threads
Trouble Repeating Method
i want some help in writing the code for the below description
encapsulating of array
I hate NullPointerExceptions!
Screen Scrapping Problem!