Results 1 to 19 of 19
  1. #1
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default why does it say "null"?!

    hello! i was wondering why it says "null" whenever the value isnt 2-10, because when it hits for example 11, id like it to say "knekt"!
    the names and stuff are in swedish i hope it doesnt bother ya too much, ill start programming in english *promise* :D

    public class Kort {

    private int kast;
    private static int valör=13;
    private static int färg =4;
    private String KORT;

    public void blandaKort(){

    kast=(int)(Math.random()*valör);

    if (kast==1){
    String KORT=Integer.toString(kast);
    KORT="ESS";
    }

    else if (kast==2){KORT="2";}
    else if (kast==3){KORT="3";}
    else if (kast==3){KORT="3";}
    else if (kast==4){KORT="4";}
    else if (kast==5){KORT="5";}
    else if (kast==6){KORT="6";}
    else if (kast==7){KORT="7";}
    else if (kast==8){KORT="8";}
    else if (kast==9){KORT="9";}
    else if (kast==10){KORT="10";}


    else if (kast==11){
    String KORT=Integer.toString(kast);
    KORT="knekt";
    }

    else if (kast==12){
    String KORT=Integer.toString(kast);
    KORT="DAM";
    }

    else if (kast==13){
    String KORT=Integer.toString(kast);
    KORT="KUNG";
    }
    }

    public void skrivUt(){


    System.out.println(KORT);


    }


    public class KortProgram {

    public static void main (String[] args){

    Kort spel = new Kort();
    spel.blandaKort();
    spel.skrivUt();

    }

    }


    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Where does it say "null"?

    A problem you have is that you define new variables: String KORT over and over and over inside of {} and give them values that go out of scope and disappear when the execution exits the {}s. Remove "String" from "String KORT" to use the one variable defined at the beginning of the code.

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

    Default

    While we're at it: use enums for your playing cards. If I'm not mistaken the Tutorials even use playing card examples in their text ...

    kind regards,

    Jos

  4. #4
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    im sorry, i dont even know what enums mean :S

  5. #5
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    Hey u!
    As Norm said :
    A problem you have is that you define new variables: String KORT over and over and over inside of {} and give them values that go out of scope and disappear when the execution exits the {}s. Remove "String" from "String KORT" to use the one variable defined at the beginning of the code.


    I copied ur code and run it without all those declarations inside the if else code and it worked!!!!!!!
    Try to do this!!!!!

    Another comment: check what static does when u use it for a variable ;) i tell u this cause when i was newbie i did exactly the same mistakes as u ;)

  6. #6
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    i very very appreciate ur help!

    i modified my code and put "färg", which is the same principle as the "kort"... it means color resp. card in english.. again, sorry for the swedish coding :D
    anyway, it seems to have the same problem, it creates a "null" value when i try to print it out and ive almost copy-pasted it from "kort", just changed the name to "färg" :eek:


    public class Kort {

    private int kast, kast2;
    private static int valör=13;
    private static int färg =4;
    private String KORT;
    private String FÄRG;

    public void blandafärg(){

    kast=(int)(Math.random()*valör)+1;

    if (kast==1){
    KORT=Integer.toString(kast);
    KORT="ESS";
    }

    else if (kast==2){KORT="2";}
    else if (kast==3){KORT="3";}
    else if (kast==3){KORT="3";}
    else if (kast==4){KORT="4";}
    else if (kast==5){KORT="5";}
    else if (kast==6){KORT="6";}
    else if (kast==7){KORT="7";}
    else if (kast==8){KORT="8";}
    else if (kast==9){KORT="9";}
    else if (kast==10){KORT="10";}


    else if (kast==11){
    KORT=Integer.toString(kast);
    KORT="knekt";
    }

    else if (kast==12){
    KORT=Integer.toString(kast);
    KORT="DAM";
    }

    else if (kast==13){
    KORT=Integer.toString(kast);
    KORT="KUNG";
    }
    }

    public void blandaFärg(){
    kast2=(int)(Math.random()*färg)+1;

    if (kast2==1){
    FÄRG=Integer.toString(kast2);
    FÄRG="Hjärter";
    }
    else if (kast2==2){
    FÄRG=Integer.toString(kast2);
    FÄRG="Spader";
    }
    else if (kast2==3){
    FÄRG=Integer.toString(kast2);
    FÄRG="Klöver";
    }
    else if(kast2==4){
    FÄRG=Integer.toString(kast2);
    FÄRG="Ruter";

    }
    }






    public void skrivUt(){


    System.out.println(FÄRG+" "+KORT);


    }

    }





    public class KortProgram {

    public static void main (String[] args){

    Kort spel = new Kort();
    spel.blandafärg();
    spel.skrivUt();

    }

    }

  7. #7
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    one questions, does it returns both null?!

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    it creates a "null" value when i try to print it out
    Where does it "create a null" and where do you print it out?
    What variable are you talking about?

    A comment on your program:
    When you have a list of chained together if/else if statements, add an ending else to handle the condition that none of the preceding tests caught. Print out a message there showing the value and a message about the problem.
    This will help you find bugs in your program.

  9. #9
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    well, the cards ("kort") are printing out just alright, just the "färg" that prints out null

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Add the else at the end of the if/else if chain and you should see what your problem is.

  11. #11
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Where does it "create a null" and where do you print it out?
    What variable are you talking about?

    A comment on your program:
    When you have a list of chained together if/else if statements, add an ending else to handle the condition that none of the preceding tests caught. Print out a message there showing the value and a message about the problem.
    This will help you find bugs in your program.
    public void skrivUt(){

    System.out.println(FÄRG+" "+KORT);


    färg shows a "null" value while KORT is working

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Let me repeat for the third time:
    Add the else at the end of the if/else if chain and you should see what your problem is

  13. #13
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Add the else at the end of the if/else if chain and you should see what your problem is.
    you mean like this:

    public void blandaFärg(){
    kast2=(int)(Math.random()*färg)+1;

    if (kast2==1){
    FÄRG=Integer.toString(kast2);
    FÄRG="Hjärter";
    }
    else if (kast2==2){
    FÄRG=Integer.toString(kast2);
    FÄRG="Spader";
    }
    else if (kast2==3){
    FÄRG=Integer.toString(kast2);
    FÄRG="Klöver";
    }
    else{
    FÄRG=Integer.toString(kast2);
    FÄRG="Ruter";
    }

    }

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    No that wasn't what I said. I said to ADD an 'else', NOT to change the last 'else if' to an 'else'.

    Why do these two statements:
    FÄRG=Integer.toString(kast2);
    FÄRG="Ruter";

    The second one replaces the value saved by the first one?
    Do you mean to concatenate the values? Something like this:
    FÄRG=Integer.toString(kast2);
    FÄRG += " Ruter"; // Concatentate to follow number

  15. #15
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    mmmmm......


    u have 2 methods differrent:

    public void blandafärg()

    and

    public void blandFärg()

    u never call the second one, so its normal the fact that your String is null!

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    That shows another place where putting print outs in your code to show execution flow would help you find bugs. When a print out is NOT done, then you know that that code has NOT been executed.

  17. #17
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    That shows another place where putting print outs in your code to show execution flow would help you find bugs. When a print out is NOT done, then you know that that code has NOT been executed.
    Quote Originally Posted by jlmp View Post
    mmmmm......


    u have 2 methods differrent:

    public void blandafärg()

    and

    public void blandFärg()

    u never call the second one, so its normal the fact that your String is null!
    ofcourse! now it works perfectlyD

  18. #18
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    That shows another place where putting print outs in your code to show execution flow would help you find bugs. When a print out is NOT done, then you know that that code has NOT been executed.
    indeed, ill start doing so.. haven't gotten around these "good programming" customs yet, good tip and thanks for the help!

  19. #19
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    I have to admit that in order to find your mistake i had to put System.out.println to your code in order to follow your execution!!!! Norm is absolutelly right about that, printing out is the BEST way (and for me the first and the only way)to find bugs!!!!
    Last edited by jlmp; 09-21-2010 at 07:27 PM.

Similar Threads

  1. How to "allow null" in a very simple script?
    By Mattedatten in forum New To Java
    Replies: 7
    Last Post: 12-04-2010, 11:09 PM
  2. Replies: 2
    Last Post: 03-19-2010, 05:30 PM
  3. Replies: 0
    Last Post: 07-28-2009, 06:45 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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