3

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;
    }
}
}
1
  • You really should use enums for such kids of data. Commented Oct 11, 2013 at 10:23

5 Answers 5

6

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);
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

thanks for the help, got it fixed. But I have one more question, doesn't every method in the project execute when you compile it? Or does it only execute when you only call it from the main class?
Methods only get executed when called explicitly, constructors get called when a class is instantiated.
Only the main() will automatically execute. Constructors will be called when you instantiate and other methods(static or non-static) will be executed only when invoked explicitly by you.
No No. You have to call them manually when you need them to do specific task
0

You are instantiating new objects but the constructor is not assigning random numbers to the variables inside the classes.

rndnumber h = new rndnumber();

is creating a new object. But at no point is the String number set to anything inside that class.

private String number;

Later you return that string, which is still null.

public String getString(){
    return number;
}

Comments

0

You never initialize the String fields rndsuit and rndnumber so they are null.

Consider adding a constructor to both classes that will initialize the int as well as the String fields.

For example

public class RandomSuit  {
    // theses should never change so make them final
    private final int y;
    private final String rndsuit;

    public RandomSuit() {
       this.y = (int) (Math.random()*3+1);
       this.rndsuit = rnd(y);
    }
    // find the String representation of the random number 
    private static String rnd(rndnumber){
      switch (rndnumber){
      case 1:
          return "Spades";
      case 2:
          return "Cloves";
       ...
       }
    }
    //override Object's toString instead of using getString
    @Override
    public String toString(){
       return rndsuit;
    }
}

Comments

0

You can solve this by adding constructors to the rndnumber and rndsuit classes as folows.

Add following to the rndnumber class :

public rndnumber() {
    rnd();
}

Add this to the rndsuit class :

public rndsuit() {
    suit();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.