0

I am making a program that makes a randomized lotto ticket and compares it to a user's set of numbers. My problem is I cannot locate the error of NullPointerException. I also feel like my method for comparing numbers is sub-optimal but I can't think of better way to write it.

package lotteryapplication;

import java.util.Random;
import java.util.Scanner;

public class LotteryApplication {

    public static int lotteryNumbers[];
    public static int usersNumbers[] = new int[5];

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println(
                " This is the lottery. Numbers in my lottery range from 1 through 60.");
        System.out.println(" See if you can win\n");
        getUserPicks();
        getRandomNumbers();
        checkLotteryMatch();
    }

    public static void getUserPicks() {
        Scanner keyboard = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.print(String.format(
                    "Enter a Number from 1 through 60 for spot :" + (i + 1))
                    + "\n");
            usersNumbers[i] = keyboard.nextInt();
            keyboard.nextLine();
        }
    }

    public static void getRandomNumbers() {
        int lotteryNumbers[] = new int[5];
        Random r = new Random();
        for (int i = 0; i < 5; i++) {
            lotteryNumbers[i] = r.nextInt(60) + 1;
        }
    }

    public static void checkLotteryMatch() {
        int matchedNums = 0;
        if (usersNumbers.length == lotteryNumbers.length) {

            for (int i = 0; i < lotteryNumbers.length; i++) {
                if (usersNumbers[0] == lotteryNumbers[i]) {
                    matchedNums++;
                }
            }
            for (int i = 0; i < lotteryNumbers.length; i++) {
                if (usersNumbers[1] == lotteryNumbers[i]) {
                    matchedNums++;
                }
            }
            for (int i = 0; i < lotteryNumbers.length; i++) {
                if (usersNumbers[2] == lotteryNumbers[i]) {
                    matchedNums++;
                }
            }
            for (int i = 0; i < lotteryNumbers.length; i++) {
                if (usersNumbers[3] == lotteryNumbers[i]) {
                    matchedNums++;
                }
            }
            for (int i = 0; i < lotteryNumbers.length; i++) {
                if (usersNumbers[4] == lotteryNumbers[i]) {
                    matchedNums++;
                }
            }
            for (int i = 0; i < lotteryNumbers.length; i++) {
                if (usersNumbers[5] == lotteryNumbers[i]) {
                    matchedNums++;
                }
            }
        }
        if (matchedNums == 1) {
            System.out.println("You only got one match sorry you win nothing.");
        }
        if (matchedNums == 2) {
            System.out.println("You only got two matched sorry you win nothing");
        }
        if (matchedNums == 3) {
            System.out.println(
                    "you will recieve a free Lottery ticket as the prize");
        }
        if (matchedNums == 4) {
            System.out.println("You will recieve a $2,000 prize");
        }
        if (matchedNums == 5) {
            System.out.println("You will recieve a 500,000 prize");
        }
        if (matchedNums == 6) {
            System.out.println("You will recieve a grand prize of $1,000,000");
        }
    }
}
Bobulous
  • 12,967
  • 4
  • 37
  • 68

3 Answers3

1

The NullPointerException is in checkLotteryMatch due to the lotteryNumbers[] array not having been initialized yet.

I modified your program to use classes from the java.util Collections package.

package lotteryapplication;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class LotteryApplication {
    private static final int CAPACITY = 5;
    private Set<Integer> lotteryNumbers = new HashSet<>(CAPACITY);
    private List<Integer> userPicks = new ArrayList<>(CAPACITY);

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        new LotteryApplication().run();
    }

    private void run() {
        System.out.println(" This is the lottery. Numbers in my lottery range from 1 through 60.");
        System.out.println(" See if you can win\n");
        getUserPicks();
        getRandomNumbers();
        checkLotteryMatch();
    }

    public void getUserPicks() {
        try (Scanner keyboard = new Scanner(System.in)) {
            for (int i = 0; i < 5; i++) {
                System.out.print(String.format("Enter a Number from 1 through 60 for spot :" + (i + 1)) + "\n");
                userPicks.add(keyboard.nextInt());
                keyboard.nextLine();
            }
        }
    }

    public void getRandomNumbers() {
        Random r = new Random();
        for (int i = 0; i < 5; i++)
            lotteryNumbers.add(r.nextInt(60) + 1);
    }

    public void checkLotteryMatch() {
        int matchedNums = 0;

        if (userPicks.size() == lotteryNumbers.size()) {
            for (Integer lottery : lotteryNumbers) {
                if (userPicks.contains(lottery)) {
                    matchedNums++;
                }
            }
        }

        switch (matchedNums) {
        case 0:
             System.out.println("Better luck next time.");
             break;

        case 1:
            System.out.println("You only got one match sorry you win nothing.");
            break;

        case 2:
            System.out.println("You only got two matched sorry you win nothing");
            break;

        case 3:
            System.out.println("you will recieve a free Lottery ticket as the prize");
            break;

        case 4:
            System.out.println("You will recieve a $2,000 prize");
            break;

        case 5:
            System.out.println("You will recieve a 500,000 prize");
            break;

        case 6:
            System.out.println("You will recieve a grand prize of $1,000,000");
            break;
        }
    }
}
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
0

Quite simply this is a typo. In your getRandomNumbers() method you have this on the first line:

int lotteryNumbers[] = new int[5];

Because you've put int at the start of the line, this means the method is declaring a new local method variable, so the method is working on this local variable and completely ignoring the class field that you've declared at the start of the class. So in the other methods, when they refer to lotteryNumbers, they are referring to the class field which has never been initialised with any value.

To fix the NullPointerException exception you probably just need to remove the keyword int from this line of code, so that the method no longer creates a local variable and instead works with the class field.

Bobulous
  • 12,967
  • 4
  • 37
  • 68
-1

It would be really helpfull if you just commented your output of the NullpointerException, because this always tells it where it's been triggered. For your next problem, checking numbers there are some solutions.

  1. You can use a Map

    Map<Integer, String> map = new HashMap<>(); //Or any other map you prefer
    map.put(1, "....");
    .
    .
    .
    
    if(map.contains(matchedNums)) System.out.println(map.get(matchedNums); 
    else DOSTUFF //maybe an Exception
    
  2. BUT IN YOUR CASE i would rather use an array, since it's a little faster and doesn't give that much overhead as a map. Considering your small programm this shouldn't be a problem but i prefer to think ahead.

    String[] messages = new String[]{"1....", "2....", ...};
    if(matchedNums < messages.length) System.out.println(messages[matchedNums]); 
    else DOSTUFF //maybe an Exception
    
  3. If you are a Function fanboy like me you an also do something like this.

    String[] messages = new String[]{"1....", "2....", ...};
    Optional.ofNullable(matchedNums).map(i -> messages[i]).ifPresent(System.out::println);
    

There are many options, choose as you like and as it fits you best.

Levent Dag
  • 162
  • 8