Results 1 to 14 of 14
Thread: Code Print Error! Help needed
- 11-16-2010, 02:26 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
Code Print Error! Help needed
When i run this code:
Java Code:public class baseballPlayer { private static String FirstName; private static String LastName; private static double average; private static String position; private static String Team; public baseballPlayer(String FirstName, String LastName, double average, String position, String Team){ String FirstN= "Michael"; String LastN= "Hall"; double Batting = 0.300; String pos="Second Base"; String TeamName="Angels"; FirstName= FirstN; LastName= LastN; average= Batting; position= pos; Team= TeamName; } public static void main(String[] args){ baseballPlayer mHall=new baseballPlayer(FirstName, LastName, average, position, Team); System.out.println(mHall.FirstName + " " + mHall.LastName + " " + mHall.average + " " + mHall.position + " " + mHall.Team); } }
Java Code:null null 0.0 null null
- 11-16-2010, 02:29 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
None of those variables / fields should be static. None.
And don't re-declare your variables in the constructor. Instead use the class fields as that's what they're for.
In other words don't do this:
Java Code:class Foo { static int bar; public Foo(int bar) { int bar = bar; //the "bar declared in the constructor is only visible in the constructor }
Instead do this:
Java Code:class Foo { int bar; // not static public Foo(int bar) { bar = bar; // bar is not re-declared }
Last edited by curmudgeon; 11-16-2010 at 02:32 AM.
- 11-16-2010, 02:33 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
when i change the fields to non-static
i get an error on this line
Java Code:baseballPlayer mHall=new baseballPlayer(FirstName, LastName, average, position, Team);
- 11-16-2010, 02:36 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
- 11-16-2010, 02:41 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
the fields are for the baseball player if that changes anything to make the object run correctly?
- 11-16-2010, 03:44 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
how can i make this object run properly without changing it to static which makes th eoutput null..
- 11-16-2010, 03:50 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
Could you post the code you are using now? ANd any compiler messages if you don't understand them.
Basically the code should look like:
Java Code:public class MyThing { private String name; public MyThing(String name) { this.name = name; } public static void main(String[] args) { String testName = "Michael"; MyThing thing = new MyThing(testName); System.out.println(thing.name); } }
Some things to notice here:
* The class starts with a capital letter (as does the constructor), methods and variables start with a lower case letter and use camelCase.
* In the constructor "this" is used so that the name argument is assigned to the name instance variable. Or use two different variable names, but in that case you have to keep which goes with what straight in your own mind.
* The actual data (the name in this case) is supplied by the "client" (the main() method). The MyThing class constructor doesn't take it upon itself to name itself "Michael".
* Only main() is static.
Maybe there are other things whose significance is not clear (ie you say to yourself "I wouldn't do that. Why is he doing that?"). If so, ask.
- 11-16-2010, 03:52 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
this is the code that i am using now
Java Code:public class baseballPlayer { private String FirstName; private String LastName; private double average; private String position; private String Team; public baseballPlayer(String FirstName, String LastName, double average, String position, String Team){ String FirstN= "Michael"; String LastN= "Hall"; double Batting = 0.300; String pos="Second Base"; String TeamName="Angels"; FirstName= FirstN; LastName= LastN; average= Batting; position= pos; Team= TeamName; } public static void main(String[] args){ baseballPlayer mHall=new baseballPlayer(FirstName, LastName, average, position, Team); System.out.println(mHall.FirstName + " " + mHall.LastName + " " + mHall.average + " " + mHall.position + " " + mHall.Team); } }
- 11-16-2010, 03:53 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
error message is
"Cannot make a static reference to the non-static field "...."
at the line
Java Code:baseballPlayer mHall=new baseballPlayer(FirstName, LastName, average, position, Team);
- 11-16-2010, 04:01 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
You are referencing FirstName (for example) and the compiler has a problem with that. It knows that the baseballPlayer class has a FirstName instance variable. (In plain terms it knows that a baseball player has a name.) But when it hits this line the compiler's message is saying, in effect, "Whose first name?!?!?!".
You can only talk about FirstName once you have a baseball player. So mHall.FirstName would make sense as it means "mHall's first name". {*}
But that's no good for the constructor because you don't have a baseball player at that point. (and hence no first name to be referenced.) Have a look at my point about the client being the one to supply the data (first name and the rest). And the overall structure of the code. The code I posted ought to compile and run - I haven't tested it, though - and output what you would expect.
{*} Edit: Later when you write methods you will use the simple FirstName form. But in that context it means "my first name". What the compiler means by a "static context" is a context where there is no "my".Last edited by pbrockway2; 11-16-2010 at 04:05 AM.
- 11-16-2010, 04:02 AM #11
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
Don't use the class fields to fill the parameters. Use literals, such as:
Java Code:public static void main(String[] args) { baseballPlayer mHall = new baseballPlayer("Mickey", "Mantle", 0.258, "FB", "Yankees"); System.out.println(mHall.FirstName + " " + mHall.LastName + " " + mHall.average + " " + mHall.position + " " + mHall.Team); }
Last edited by curmudgeon; 11-16-2010 at 04:09 AM.
- 11-16-2010, 04:22 AM #12
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
when i run this code
Java Code:public class baseballPlayer { private String FirstName; private String LastName; private double average; private String position; private String Team; public baseballPlayer(String FirstName, String LastName, double average, String position, String Team){ String FirstN= "Michael"; String LastN= "Hall"; double Batting = 0.300; String pos="Second Base"; String TeamName="Angels"; FirstName= FirstN; LastName= LastN; average= Batting; position= pos; Team= TeamName; } public static void main(String[] args){ baseballPlayer mHall=new baseballPlayer("Michael", "Hall", 0.350, "First Base", "Angels"); System.out.println(mHall.FirstName + " " + mHall.LastName + " " + mHall.average + " " + mHall.position + " " + mHall.Team); } }
i get "null null 0.0 null null"
Why?
- 11-16-2010, 04:34 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
Because in your constructor you say:
Java Code:FirstName= FirstN;
instead of
Java Code:this.FirstName = FirstName;
I have suggested - a couple of times - that the constructor should assign the instance variables using the data passed to it and should not be making up its own data (FirstN and the rest). Those variables should simply be deleted. It's up to you of course.
- 11-16-2010, 04:35 AM #14
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
You've get several things wrong in your code above. For one, you're ignoring the parameters passed. For example, just looking at one field, first name,
Java Code:public baseballPlayer(String FirstName){ String FirstN= "Michael"; // Error #1 FirstName= FirstN; // Error #2 }
Then at Error #2 you're setting the parameter, not the class field to the hard-coded value. Since the parameter FirstName and the class field FirstName are spelled exactly the same, the compiler has to choose which one you mean when you use that name in the constructor, and by default it uses the parameter. To use the class field, you have to prefix it with this, i.e,
Java Code:this.FirstName = "spam";
Java Code:public baseballPlayer(String FirstName){ this.FirstName = FirstName; }
Now try to do the same with the rest of the parameters. You'll also want to avoid capitalizing the first letters of fields.
Luck!
Similar Threads
-
needed a code for this
By rajivjoshi in forum New To JavaReplies: 2Last Post: 08-01-2010, 01:26 PM -
How to use and print the JAVA code from a JSP
By Ginkan in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 02-23-2010, 03:05 PM -
ArrayLists do not print to the terminal window, why? long code inside
By caps_lock in forum New To JavaReplies: 5Last Post: 05-25-2009, 10:03 PM -
Java code to connect printer to print content
By wendyz in forum Advanced JavaReplies: 3Last Post: 04-30-2009, 07:51 PM -
How to Print out adiamond pattern I try this code but
By masaka in forum New To JavaReplies: 11Last Post: 04-16-2008, 02:52 PM
Bookmarks