0
public class Main {

    public static void main(String[] args) {
        Lottery nums = new Lottery();

        System.out.print(nums.getLotteryNumbers());
    }
}

class Lottery {
    int[] lotteryNumbers;

    Lottery() {
        Random randomNum = new Random();
        int[] lotteryNumbers = new int[6];

        for (int i = 0; i < lotteryNumbers.length; i++) {
            lotteryNumbers[i] = randomNum.nextInt(10);

        }

    }

    public int[] getLotteryNumbers() {
        return lotteryNumbers;
    }
}

I get a null every time I try to print out my Random array, if you can please point me to what I'm doing wrong I'd really appreciate it thanks!

Yayo
  • 15
  • 1
  • 7
  • `int[] lotteryNumbers = new int[6];` - you declare a new `int[]` with constructor local scope. So you don't actually initialize your class level variable `lotteryNumbers`. So your class variable is still `null` when you call `getLotteryNumbes()`. The line in the constructor should be `lotteryNumbers = new int[6];`. Could be a typo or it could be lack of knowledge. In the second case, check out [What is 'scope' in Java?](https://stackoverflow.com/questions/38177140/what-is-scope-in-java). – maloomeister May 10 '21 at 05:38
  • Also, there is a useful utility method [`Arrays.toString()`](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-int:A-) for printing out the contained values without having to use a loop. – maloomeister May 10 '21 at 05:44
  • thanks for your info I understand the first part you mentioned and now it did change the null! but it prints out random numbers and symbols like [@I and such . is there something I'm still missing? – Yayo May 10 '21 at 18:11
  • Check out the answers on [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4), especially the section on arrays. – maloomeister May 11 '21 at 05:39

2 Answers2

1

There are a few issue with your code.

First lotteryNumbers is being re defined. You should declare it just once as a global variable. Remove your int[] lotteryNumbers = new int[6]; and change it to

class Lottery { 

int[] lotteryNumbers;

Lottery() {
   
    lotteryNumbers = new int[6];

    for (int i = 0; i < lotteryNumbers.length; i++) {
        lotteryNumbers[i] = randomNum.nextInt(10);

    }

}

Then to print your result, add this in your main method

int [] lotteryNumbers = nums.getLotteryNumbers();
     for (int i = 0; i < lotteryNumbers.length; i++) {
          System.out.print(lotteryNumbers[i]);
     }
lyncx
  • 680
  • 4
  • 8
  • thanks for your response . The assignment wants us to use Random though so I'm not sure where would I add that in your example. – Yayo May 10 '21 at 18:10
  • Yes just change that line to random int and it should work. Ive edited the code – lyncx May 10 '21 at 23:01
0

Remove int[] in the line

  • int[] lotteryNumbers = new int[6];

It will create a new array variable inside Lottery() function.

If you dont get it, Learn more about variable scope.

  • yea that helped! but not instead of null I'm getting random symbols along with numbers in the result – Yayo May 10 '21 at 18:15