1 Replies - 1012 Views - Last Post: 18 April 2012 - 10:28 PM Rate Topic: -----

#1 ajackson25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-April 12

I am trying to add the results of a calculation to a GUI panel.

Posted 18 April 2012 - 08:07 PM

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.

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.

I have the code all written, and netbeans doesn't show any errors, but i'm having trouble in the buildResults method. 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.

The code as it is now is below. Any help would be greatly appreciated.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class LotteryApplication extends JFrame
   private JPanel instructionPanel;     
   private JPanel lotteryPanel; 
   private JPanel userPanel;
   private JPanel resultsPanel;
   private JPanel buttonPanel;   
   private JLabel instruction;
   private JTextField userPicks[];
   private JLabel lotteryNumbers[];
   private JLabel results;
   private JButton calcBtn;
   private JButton exitBtn;
   private JButton clearBtn;
   Random randomNumbers = new Random();
        final int NUMBERS = 5;
        int loop;
        final int UPPER_END = 10;
        final int SIZE = 3;
        int[] counter;
        final int TWO_DIGITS = 10;
        boolean arraysEqual = true;
        int matches = 0;
    public static void main (String[] args)
        LotteryApplication lotteryApp = new LotteryApplication();
        public LotteryApplication()
        setTitle("Lottery Application");
        setLayout(new BorderLayout());
        add(instructionPanel, BorderLayout.NORTH);
      	add(userPanel, BorderLayout.WEST);
        add(lotteryPanel, BorderLayout.CENTER);
      	add(resultsPanel, BorderLayout.EAST);
      	add(buttonPanel, BorderLayout.SOUTH);
        private void buildInstructionPanel()
       	instructionPanel = new JPanel();
      	instruction = new JLabel("Select 5 numbers from 0-9.");
        private void buildUserPanel()
      	userPanel = new JPanel();
        userPanel.setLayout(new GridLayout(5, 1));
      	userPanel.setBorder(BorderFactory.createTitledBorder("User Picks"));
        userPicks = new JTextField[NUMBERS];
        for (loop = 0; loop < NUMBERS; loop ++)
            userPicks[loop] = new JTextField(SIZE);
        private void buildLotteryPanel()
      	lotteryPanel = new JPanel();
      	lotteryPanel.setBorder(BorderFactory.createTitledBorder("Lottery Numbers"));
        lotteryNumbers = new JLabel[NUMBERS];
        counter = new int[UPPER_END];
        for (loop = 0; loop < NUMBERS; loop ++)
            lotteryNumbers[loop] = new JLabel("" + randomNumbers.nextInt
        private void buildResultsPanel()
        resultsPanel = new JPanel();

        private void buildButtonPanel()

        buttonPanel = new JPanel();
    	calcBtn = new JButton("Calculate");
        clearBtn = new JButton("Clear");
      	exitBtn = new JButton("Exit");


    	calcBtn.addActionListener(new CalcButtonListener());
        clearBtn.addActionListener(new ClearButtonListener());
      	exitBtn.addActionListener(new ExitButtonListener());

        private class ExitButtonListener implements ActionListener
      	public void actionPerformed(ActionEvent e)
   	private class ClearButtonListener implements ActionListener
        public void actionPerformed(ActionEvent e)
        private class CalcButtonListener implements ActionListener
        public void actionPerformed(ActionEvent e)
          while (arraysEqual && loop < lotteryNumbers.length)
            if (lotteryNumbers[loop].equals (userPicks[loop].getText()))
                arraysEqual = true;
                matches ++;
                loop ++;
                arraysEqual = false;
                loop ++;
        if (matches <= NUMBERS)
            results = new JLabel("You matched " + matches + " numbers.");
            results = new JLabel("You are a Grand Prize Winner!");

If it helps, this is what I get now when I run it.

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: 1 second)

Is This A Good Question/Topic? 0
  • +

Replies To: I am trying to add the results of a calculation to a GUI panel.

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: I am trying to add the results of a calculation to a GUI panel.

Posted 18 April 2012 - 10:28 PM

You never initialize results, and you cannot add null to the container

You could make it visible/invisible with the setVisible(true/false) method, but wouldn't it be better to always show the label, but then only set the content of it when the calculation button is pressed?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1