File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Lottery GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Lottery GUI" Watch "Lottery GUI" New topic
Author

Lottery GUI

A Jackson
Greenhorn

Joined: Apr 17, 2012
Posts: 6
Hi all,
I've been searching for help with this program, and have found lots of lottery applications, but nothing quite like what I need. I feel like I'm missing something, but I have looked at this so much over the last few days that I can't figured out where to go from here.

What I need to do is write a lottery class that simulates a lottery. The class should have an array of five integers named lotteryNumbers. The constructor should use the Random class to generate a random number in the range of 0 - 9 for each element in the array. The class should also have a method that accepts an array of five integers that represent a person's lottery picks (userPicks). The method is to compare the corresponding elements in the two arrays and return the number or digits that match. The class should be demonstrated in a GUI with JTextFields for the user to enter their picks and JLabels for the winning lottery numbers. The results panel should display the number of digits that match the randomly generated lottery numbers, or a message proclaiming the user a grand prize winner should all the digits match.

What I have is below. Any input would be appreciated.

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Welcome to JavaRanch!

You've shown us a bunch of code, and implied that it doesn't do what you want it to do, but you need to give us some more details. What's is it doing wrong? Are there compile errors? (Post them.) Run time errors? (Post those.) Or is not working in some other way?
A Jackson
Greenhorn

Joined: Apr 17, 2012
Posts: 6
Thanks for the reply, and no, it is not working. I'm using netbeans, and it doesn't show any error in the code (no red squiggly lines). When I run it I get this:

run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at LotteryApplication.buildUserPanel(LotteryApplication.java:82)
at LotteryApplication.<init>(LotteryApplication.java:48)
at LotteryApplication.main(LotteryApplication.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Oh, that's because when you declare an array to have n elements, you are only allowed to use indexes 0 to n-1. Your loop runs from 0 to <= NUMBERS, so on the last time through, you try to assign to userPicks[5] and that blows up.
A Jackson
Greenhorn

Joined: Apr 17, 2012
Posts: 6
That makes sense. I was warned to watch for those off-by-one errors. I changed the <= NUMBERS on lines 79, and 97 to < NUMBERS, hoping that would solve the problem, but it did not. I used a System.out.print on the first line of the buildLotteryPanel method to see if it was making it there, and it printed. I added on line 93. I tried a System.out.print inside the loop in the buildLotteryPanel. It printed five times. Placing the System.out.print at line 103 it printed once, but after line 104 it would not print, so I think it must be hanging up there, i'm just not sure why.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Actually, it should blow up again at 104. By then loop would hold the value 5, so you'd have a bad index again. What are you trying to do at that line?
A Jackson
Greenhorn

Joined: Apr 17, 2012
Posts: 6
Well, what I was trying to do was add the elements of the lotteryNumbers array to the lottery panel, but I see why that was causing a problem, so i've changed the buildLotteryPanel method to look like this



It's making through to the buildResultsPanel method now, but I feel like I need something at line 112 to retrieve my results.
This is what I'm seeing when I run it now:

run:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at LotteryApplication.buildResultsPanel(LotteryApplication.java:111)
at LotteryApplication.<init>(LotteryApplication.java:49)
at LotteryApplication.main(LotteryApplication.java:36)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
The problem appears to be on line 111. What does that say?
A Jackson
Greenhorn

Joined: Apr 17, 2012
Posts: 6
I think the problem is that i'm trying to add results to the panel before they have been calculated, but I'm not sure how to do that, since nothing should appear in the results panel until the calculate button has been clicked.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Well, maybe. Campbell asked you to show us line 111 though. That's where the error is, but it's not clear if that code is from the original listing or not. Show us line 111, and a few lines around it.
A Jackson
Greenhorn

Joined: Apr 17, 2012
Posts: 6
Ok, sorry bout that, this is the buildResultsPanel method.


Line 111 is resultsPanel.add(results);

The CalcButtonListener (lines 151 - 185) from above are the same. As I said, I'm not sure how to add the results to the panel since they are to appear only after the calculate button has been pressed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Read about add, and it tells you all sorts of useful information about your problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lottery GUI
 
Similar Threads
Label in a field or scrollpanel
Arrays Won't Work
Switching between the frames
JLabel appears as a JButton
calling another swing application to run when a button is click from my swing application