Results 1 to 14 of 14
  1. #1
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default 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);
    }
    }
    i get an output of
    Java Code:
    null null 0.0 null null
    Why do i get this output, and what can i do to fix my error?

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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.

  3. #3
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    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);
    the error is "Cannot make a static reference to the non-static field "...."

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by socboy6579 View Post
    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);
    the error is "Cannot make a static reference to the non-static field "...."
    The solution is not to make the class fields static. The solution is to use local variables in the main method, variables that are declared in that method, not to use the class fields there (unless they're associated with an object).

  5. #5
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    the fields are for the baseball player if that changes anything to make the object run correctly?

  6. #6
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    how can i make this object run properly without changing it to static which makes th eoutput null..

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  8. #8
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    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);
    }
    }

  9. #9
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    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);

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #11
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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);
       }
    I like to think that there are two separate worlds here, the static world that exists in static methods and blocks, and the instance world that exists in non-static methods and blocks. You can use static things in the instance world, but you can't use instance things in the static world unless they're associated with an object.
    Last edited by curmudgeon; 11-16-2010 at 04:09 AM.

  12. #12
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    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?

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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
    	}
    At Error #1, you're ignoring the value passed into the parameter, and you're setting a local variable with a hard-coded String literal, "Micheal".

    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";
    Better still is to simplify your code by just passing the parameter into the class field in one step:

    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

  1. needed a code for this
    By rajivjoshi in forum New To Java
    Replies: 2
    Last Post: 08-01-2010, 01:26 PM
  2. How to use and print the JAVA code from a JSP
    By Ginkan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-23-2010, 03:05 PM
  3. Replies: 5
    Last Post: 05-25-2009, 10:03 PM
  4. Java code to connect printer to print content
    By wendyz in forum Advanced Java
    Replies: 3
    Last Post: 04-30-2009, 07:51 PM
  5. Replies: 11
    Last Post: 04-16-2008, 02:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •