Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting null when retrieving int from another class

Tags:

java

Basically I made this programm where the computer generates a random card. I made a class called 'rndnumber', this class generates the random number. I then made another class called 'rndsuits', and this generates the random suit. Problem is when I go to my main class, and execute the code, im getting null instead of numbers and suits. Anyone know why? My output is basically: Here is your random card: null of null.

public class maincard { 

public static void main(String[] args){

    System.out.println("Here is your random card");

    rndnumber h = new rndnumber();
    rndsuit a = new rndsuit();


    System.out.println(h.getString() + " of " + a.getStringz());

}

}


public class rndnumber {

private int rndnumber = (int) (Math.random()*13+1);
private String number;

public String getString(){
    return number;
}


public void rnd(){
    switch (rndnumber){
    case 1:
        number = "Ace";
        break;
    case 2:
        number = "2";
        break;
    case 3:
        number = "3";
        break;
    case 4:
        number = "4";
        break;
    case 5:
        number = "5";
        break;
    case 6:
        number = "6";
        break;
    case 7:
        number = "7";
        break;
    case 8:
        number = "8";
        break;
    case 9:
        number = "9";
        break;
    case 10:
        number = "10";
        break;
    case 11:
        number = "Jacks";
        break;
    case 12:
        number = "Queens";
        break;
    case 13:
        number = "Kings";
        break;
    }

}   
}


public class rndsuit {

private int y = (int) (Math.random()*3+1);
private String rndsuit;

public String getStringz(){
return rndsuit;
}



public void suit(){
    switch(y){
    case 1:
        rndsuit = "Spades";
        break;
    case 2:
        rndsuit = "Cloves";
        break;
    case 3:
        rndsuit = "Hearts";
        break;
    case 4:
        rndsuit = "Diamonds";
        break;
    }
}
}
like image 402
Tloz Avatar asked Dec 21 '25 10:12

Tloz


2 Answers

You need to call the methods before getting the values.

h.rnd();
a.suit();
System.out.println(h.getString() + " of " + a.getStringz());

This will fix the null issue.

But there is another one issue waiting in the lounge for you. The above fix will return the same value always, unless you create a new object again and again and access value using that.

You need to move the random generator part in the methods like this for the rnd() and suit() to return new random values always.

public void suit() {
    y = (int) (Math.random() * 3 + 1);
    ...
}

public void rnd() {
    rndnumber = (int) (Math.random() * 13 + 1);
    ...
}
like image 122
Rahul Avatar answered Dec 22 '25 23:12

Rahul


You need to set them before getting.

You are setting values in rnd(), suit() methods, but you are never calling them.

 rndnumber h = new rndnumber();
 h.rnd();

 rndsuit a = new rndsuit();
  a.suit();
 System.out.println(h.getString() + " of " + a.getStringz());

Until unless you call those methods, you end up with getting default value, that is null.

As a side note: Please please follow the java naming conventions.,Class names starts with capital letter.

like image 38
Suresh Atta Avatar answered Dec 23 '25 00:12

Suresh Atta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!