Results 1 to 18 of 18
  1. #1
    Julian is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default Keeps returning null....

    This is my source code:
    Java Code:
    import java.util.Random;
    
    public class race{
    
    	static float speed;
    	static String win;
    	
    static void wintest(){
    Random ran=new Random();
    race Julian=new race();
    race Sam=new race();
    	
    	Sam.speed=ran.nextFloat();
    	Julian.speed=ran.nextFloat();
    	if(Julian.speed>Sam.speed)win="Julian wins the race!!!";
    	if(Sam.speed>Julian.speed)win="Sam wins the race!!!";
    	System.out.println(win);}
    	
    public static void main(String[] args){
    wintest();
    wintest();
    wintest();
    }}
    This is my result
    Java Code:
    null
    null
    null
    I'm confused...
    Last edited by DarrylBurke; 05-27-2011 at 09:53 AM. Reason: Removed SIZE tags, added CODE tags, removed blank lines

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Add some more printlns to show what the values are. When you print out data, be sure to add a label so you know what is being printed:
    System.out.println("data=" + data);

    Replace data with the variables you are using.

    A comment on your coding style:
    Don't bury } at the end of a statement. Put it in vertical column with the starting { or with the beginning of the code on the line with the {

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    This post Deleted.
    Last edited by Norm; 05-27-2011 at 12:45 AM. Reason: Duplicate entry

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Don't have your instance variable speed as static. That's what is causing the problem.

    Plus, you don't really need to have 2 if statements for the same condition. You can use if/else instead. Like, this code of yours,

    Java Code:
    if(Julian.speed>Sam.speed)win="Julian wins the race!!!";
    if(Sam.speed>Julian.speed)win="Sam wins the race!!!";
    Can be better written as,
    Java Code:
    if (Julian.speed > Sam.speed) {
         win = "Julian wins the race!!!";
    } else {
         win = "Sam wins the race!!!";
    }
    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by goldest View Post
    you don't really need to have 2 if statements for the same condition. You can use if/else instead. Like, this code of yours,
    <snip>
    Can be better written as,
    <snip>
    If it's a tie, why should Sam win?

    That code reminded me of a signboard near an unmanned rail crossing where I grew up:
    Don't race the train. If it's a tie, you lose.
    db

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Quote Originally Posted by DarrylBurke View Post
    If it's a tie, why should Sam win?
    OOPS, Yes. In case of a tie things would freak out. But even the OPs 2 if statements will also not do much and will return a null.

    In that case either the win needs to have a default value or a if/else-if/else can be used like,

    Java Code:
    if (Julian.speed > Sam.speed) {
    	win = "Julian wins the race!!!";
    } else if (Sam.speed > Julian.speed) {
    	win = "Sam wins the race!!!";
    } else {
    	win = "Race tied with same timings. No one won.";
    }
    Thanks for pointing that Darryl.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    Julian is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    hey goldest thx but it keeps returning Race tied with same timings. No one won. Its obviously all in the else statement...i think Julian.speed or Sam.speed is weird and stuff...im not sure not that familiar w/java

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Try to print Julian.speed and Sam.speed before if statement so we could determine who should really won or if it is a tie.
    Java Code:
    System.out.println("Julian's speed = " + Julian.speed);
    System.out.println("Sam's speed = "+ Sam.speed);
    if (Julian.speed > Sam.speed) {
    	win = "Julian wins the race!!!";
    } else if (Sam.speed > Julian.speed) {
    	win = "Sam wins the race!!!";
    } else {
    	win = "Race tied with same timings. No one won.";

  9. #9
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    Speed is a static variable and every static variable could be only created once.

    Although you declare two objects Julian and Sam, Sam.speed and Julian.speed is pointing to the same variable.

    See your code line Julian.speed=ran.nextFloat(). It actually means both Julian.speed and Sam.speed are set to ran.nextFloat().

    Julian.speed and Sam.speed will always be the same and they do not meet the two if conditions.

    Therefore, the win String object is using default class variable = null as you see in the output.
    Last edited by jing-yi; 05-28-2011 at 04:40 PM.

  10. #10
    Julian is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    okay...so i changed my code and here it is:
    Java Code:
    import java.util.Random;
    
    public class race{
    
    	static float speed;
    	static String win;
    	
    static void wintest(){
    Random ran=new Random();
    race Julian=new race();
    race Sam=new race();
    	
    	Sam.speed=(ran.nextFloat())*10;
    	Julian.speed=ran.nextFloat()*10;
    		System.out.println("Julian was running "+Julian.speed+" mph.");
    		System.out.println("Sam was running "+Sam.speed+" mph.");
    	if (Julian.speed > Sam.speed) {
    	win = "Julian wins the race!!!";
    	} else if (Sam.speed > Julian.speed) {
    	win = "Sam wins the race!!!";
    	} else {
    	win = "Race tied with same timings. No one won.";
    	}
    		System.out.println(win);
    	}
    	
    public static void main(String[] args){
    wintest();
    wintest();
    wintest();
    }}
    and heres my outcome:

    Java Code:
    Julian was running 3.9576263 mph.
    Sam was running 3.9576263 mph.
    Race tied with same timings. No one won.
    Julian was running 3.644009 mph.
    Sam was running 3.644009 mph.
    Race tied with same timings. No one won.
    Julian was running 8.811414 mph.
    Sam was running 8.811414 mph.
    Race tied with same timings. No one won.
    Last edited by Julian; 05-28-2011 at 04:32 PM.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't make variables 'speed' and 'win' static for reasons already explained.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Julian is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    I finally got it to work making Julian use spee instead instead of redoing my variables and keeping them static (too lazY) but thatnks guys

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Julian View Post
    I finally got it to work making Julian use spee instead instead of redoing my variables and keeping them static (too lszY) but thatnks guys
    That is an extremely silly hack; why don't you do as was suggested?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Julian is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    I tried not making it static but it said it cant be froma static reference and didnt know what to declare it

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Julian View Post
    I tried not making it static but it said it cant be froma static reference and didnt know what to declare it
    Post your code again (without making those variables static) and show us the line where the compiler started whining.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Julian is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Okay this is the code:
    import java.util.Random;

    public class race{

    float speed;
    String win;


    static void wintest(){
    Random ran=new Random();
    race Julian=new race();
    race Sam=new race();

    Sam.speed=(ran.nextFloat())*10;
    Julian.speed=ran.nextFloat()*10;
    System.out.println("\n\nJulian was running "+Julian.speed+" mph.");
    System.out.println("\nSam was running "+Sam.speed+" mph.");
    if (Julian.speed > Sam.speed) {
    win = "\nJulian wins the race!!!";
    } else if (Sam.speed > Julian.speed) {
    win = "\nSam wins the race!!!";
    } else {
    win = "\nRace tied with same timings. No one won.";
    }
    System.out.println(win);
    }

    public static void main(String[] args){
    wintest();
    wintest();
    wintest();
    wintest();
    wintest();
    }}
    and heres the outcome:

    race.java:19: non-static variable win cannot be referenced from a static context
    win = "\nJulian wins the race!!!";
    ^
    race.java:21: non-static variable win cannot be referenced from a static context
    win = "\nSam wins the race!!!";
    ^
    race.java:23: non-static variable win cannot be referenced from a static context
    win = "\nRace tied with same timings. No one won.";
    ^
    race.java:25: non-static variable win cannot be referenced from a static context
    System.out.println(win);
    ^
    4 errors

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please don't use [quote] [/quote] tags when you should be using [code] [/code] tags.

    In your code above, the win variable shouldn't even be a variable of the class, but rather a local variable since you're only using it locally (with code tags, note the difference in how the code looks in the forum):

    Java Code:
    import java.util.Random;
    
    public class race {
    
       private float speed;
    
       // String win;
    
       static void wintest() {
          Random ran = new Random();
          race Julian = new race();
          race Sam = new race();
    
          Sam.speed = (ran.nextFloat()) * 10;
          Julian.speed = ran.nextFloat() * 10;
          System.out.println("\n\nJulian was running " + Julian.speed + " mph.");
          System.out.println("\nSam was running " + Sam.speed + " mph.");
          String win = "";
          if (Julian.speed > Sam.speed) {
             win = "\nJulian wins the race!!!";
          } else if (Sam.speed > Julian.speed) {
             win = "\nSam wins the race!!!";
          } else {
             win = "\nRace tied with same timings. No one won.";
          }
          System.out.println(win);
       }
    
       public static void main(String[] args) {
          wintest();
          wintest();
          wintest();
          wintest();
          wintest();
       }
    }

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    What Fubarable said.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 10-02-2010, 10:30 PM
  2. Replies: 1
    Last Post: 10-28-2009, 03:39 AM
  3. Replies: 0
    Last Post: 07-28-2009, 07:45 PM
  4. Keeps returning null
    By ribbs2521 in forum New To Java
    Replies: 7
    Last Post: 02-23-2009, 03:25 AM
  5. String returning null value
    By impact in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 08:49 AM

Tags for this Thread

Posting Permissions

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