Results 1 to 12 of 12
Thread: error in handler classes
- 04-06-2012, 05:40 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
error in handler classes
hi,
i am writing a code for a dicepoker game and am having a bit off trouble. I have finished writing the code, but for some reason, my handler methods don't seem to be registering.
here is my code
The error message that comes up is:Java Code:import javax.swing.*;import java.awt.Dimension; import java.awt.event.*; import java.util.Random; public class DicePoker extends JFrame { int maxDice = 7; int numDice = 5; private int selected = -1; private int count = 4; private int i; ImageIcon[] dice = new ImageIcon[maxDice]; private int [] valueDie = new int [5]; private final int WINDOW_WIDTH = 400; private final int WINDOW_HEIGHT = 130; private Random random = new Random(System.nanoTime()); private JPanel panel; private JLabel lblRemainingDraws = new JLabel("Press New to Begin", JLabel.CENTER); private JButton btnDeal; private JButton btnNew; private JLabel[] Die = new JLabel[numDice]; private JCheckBox[] Checkboxes = new JCheckBox[numDice]; private ChkBoxHandler chkhand = new ChkBoxHandler(); public DicePoker() { setTitle("Dice Poker"); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setLocation(350,350); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buildPanel(); add(panel); setVisible(true); private void buildPanel { for (int i = 0; i < maxDice; i++) { dice[i] = new ImageIcon("die" + i + ".jpg"); } for (i=0; i < numDice; i++) { Die[i] = new JLabel(dice[i+1]); panel.add(Die[i]); Checkboxes[i] = new JCheckBox(); Checkboxes[i].addActionListener(chkhand); panel.add(Checkboxes[i]); Checkboxes[i].setEnabled (false); } panel.add(lblRemainingDraws); lblRemainingDraws.setPreferredSize(new Dimension(400,30)); btnDeal.addActionListener(new DealBtnHandler()); btnDeal.setEnabled(false); panel.add(btnDeal); btnNew.addActionListener(new NewBtnHandler()); panel.add(btnNew); } private class DealBtnHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if (count==0) { for (int i = 0; i < numDice; i++) { valueDie[i] = random.nextInt(5) + 1; Die[i].setIcon(dice[valueDie[i]]); } for (i = 0; i < numDice; i++) { Checkboxes[i].setEnabled(true); btnDeal.setText("Draw"); } } else { for (int i = 0; i<numDice;i++) { if (Checkboxes[i].isSelected()==true) { valueDie[i]=random.nextInt(5) + 1; Die[i].setIcon(dice[valueDie[i]]); Checkboxes[i].isSelected(false); } if (count == 0) { btnDeal.setEnabled(false); btnDeal.setText("Deal"); btnNew.setEnabled(true); lblRemainingDraws.setText("Draws Left = " + count); } } } } } private class NewBtnHandler implements ActionListener { public void actionPerformed(ActionEvent e) { btnDeal.setEnabled(true); btnDeal.setText("Deal"); btnNew.setEnabled(false); for (i=0; i<numDice; i++) { Checkboxes[i].setSelected(false); count = 4; lblRemainingDraws.setText("Press Deal to Roll"); } } } private class chkhand implements ActionListener { public void actionPerformed (ActionEvent e) { int i = 0; for(;i<numDice; i++) { if (e.getSource()==Checkboxes[i]) { selected = i; break; } } if (Checkboxes[i].isSelected()==true) { Die[i].setIcon(dice[0]); } else { Die[i].setIcon(dice[valueDie[i]]); } } } } public static void main (String[] args) { new DicePoker(); } }
Exception in thread "main" java.lang.Error: Unresolved compilation problem: at DicePoker.main(DicePoker.java:143)
also, chkhand, NewBtnHandler, buildpanel, and DealBtnHandler are all underlined in red? what is the problem?
Thanks
- 04-06-2012, 05:50 PM #2
Re: error in handler classes
You can't write methods inside a constructor. Format your code correctly, paying attentioj to matching the outdents with the indents, and you'll easily see what's wrong.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-06-2012, 05:56 PM #3
Re: error in handler classes
And once you correct that, you'll have a few other things to take care of.
Never write so much code without checking it along the way. Every time you add a few lines, do a compile and get rid of any errors. That way you'll never be swamped.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-06-2012, 06:33 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: error in handler classes
hi,
i have fixed the formatting on my code and nothing is underlined. However, when I run it, it gives me the following error message.
Exception in thread "main" java.lang.NullPointerException
at DicePoker.buildPanel(DicePoker.java:49)
at DicePoker.<init>(DicePoker.java:35)
at DicePoker.main(DicePoker.java:144)
Java Code:import javax.swing.*; import java.awt.Dimension; import java.awt.event.*; import java.util.Random; public class DicePoker extends JFrame { int maxDice = 7; int numDice = 5; private int selected = -1; private int count = 4; private int i; private ImageIcon[] dice = new ImageIcon[maxDice]; private int [] valueDie = new int [5]; private final int WINDOW_WIDTH = 400; private final int WINDOW_HEIGHT = 130; private Random random = new Random(System.nanoTime()); private JPanel panel; private JLabel lblRemainingDraws = new JLabel("Press New to Begin", JLabel.CENTER); private JButton btnDeal; private JButton btnNew; private JLabel[] Die = new JLabel[numDice]; private JCheckBox[] Checkboxes = new JCheckBox[numDice]; private CheckBoxHandler chkhand = new CheckBoxHandler(); public DicePoker() { setTitle("Dice Poker"); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setLocation(350,350); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buildPanel(); add(panel); setVisible(true); } private void buildPanel() { for (int i = 0; i < maxDice; i++) { dice[i] = new ImageIcon("die" + i + ".jpg"); } for (i=0; i < numDice; i++) { Die[i] = new JLabel(dice[i+1]); panel.add(Die[i]); Checkboxes[i] = new JCheckBox(); Checkboxes[i].addActionListener(chkhand); panel.add(Checkboxes[i]); Checkboxes[i].setEnabled (false); } panel.add(lblRemainingDraws); lblRemainingDraws.setPreferredSize(new Dimension(400,30)); btnDeal.addActionListener(new DealBtnHandler()); btnDeal.setEnabled(false); panel.add(btnDeal); btnNew.addActionListener(new NewBtnHandler()); panel.add(btnNew); } private class DealBtnHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if (count==0) { for (int i = 0; i < numDice; i++) { valueDie[i] = random.nextInt(5) + 1; Die[i].setIcon(dice[valueDie[i]]); } for (i = 0; i < numDice; i++) { Checkboxes[i].setEnabled(true); btnDeal.setText("Draw"); } } else { for (int i = 0; i<numDice;i++) { if (Checkboxes[i].isSelected()==true) { valueDie[i]=random.nextInt(5) + 1; Die[i].setIcon(dice[valueDie[i]]); Checkboxes[i].setSelected(false); } if (count == 0) { btnDeal.setEnabled(false); btnDeal.setText("Deal"); btnNew.setEnabled(true); lblRemainingDraws.setText("Draws Left = " + count); } } } } } private class NewBtnHandler implements ActionListener { public void actionPerformed(ActionEvent e) { btnDeal.setEnabled(true); btnDeal.setText("Deal"); btnNew.setEnabled(false); for (i=0; i<numDice; i++) { Checkboxes[i].setSelected(false); count = 4; lblRemainingDraws.setText("Press Deal to Roll"); } } } private class CheckBoxHandler implements ActionListener { public void actionPerformed (ActionEvent e) { int i = 0; for(;i<numDice; i++) { if (e.getSource()==Checkboxes[i]) { selected = i; break; } } if (Checkboxes[i].isSelected()==true) { Die[i].setIcon(dice[0]); } else { Die[i].setIcon(dice[valueDie[i]]); } } } public static void main (String[] args) { new DicePoker(); } }
- 04-06-2012, 07:04 PM #5
- 04-06-2012, 07:09 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: error in handler classes
panel.add(Die[i]);
but what is wrong with that?
- 04-06-2012, 07:22 PM #7
Re: error in handler classes
And where did you initialize the variable panel?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-06-2012, 07:25 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: error in handler classes
on line 19
- 04-06-2012, 07:34 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 04-06-2012, 07:44 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: error in handler classes
ohh okay thanks. i have a few other logic errors with my code, so i'll just see if I can get it to run.
- 04-06-2012, 09:49 PM #11
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: error in handler classes
okay here, im all done and the program runs. However, I'm running into a slight error in it. What is supposed to happen is that it randomizes all the dice, which it does. Then, the die where the checkbox is selected should be randomized again when Draw is clicked. However, for me, when Draw is clicked, all the dice are randomized again.
This is my ActionListener for the DealButton (which changes to Draw after the first time it is clicked). Also, in the beginning, the "count--;" appears to be in some sort of loop, since the count automatically decreases each time the Draw button is clicked, which is what is supposed to happen. However, its NOT in a loop. ???????Java Code:private class DealBtnHandler implements ActionListener { public void actionPerformed(ActionEvent e) { count--; if (count!=0) { for (int i = 0; i < numDice; i++) { valueDie[i] = random.nextInt(5) + 1; Die[i].setIcon(dice[valueDie[i]]); } for (i = 0; i < numDice; i++) { Checkboxes[i].setEnabled(true); btnDeal.setText("Draw"); } for (int i = 0; i<numDice;i++) { if (Checkboxes[i].isSelected()==true) { valueDie[i]=random.nextInt(5) + 1; Die[i].setIcon(dice[valueDie[i]]); Checkboxes[i].setSelected(false); } } lblRemainingDraws.setText("Draws Left = " + count); } if (count == 0) { btnDeal.setEnabled(false); btnDeal.setText("Deal"); btnNew.setEnabled(true); lblRemainingDraws.setText("GAME OVER"); } } }
- 04-09-2012, 03:51 AM #12
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Multiple classes error
By nat45928 in forum New To JavaReplies: 0Last Post: 05-09-2011, 02:10 AM -
<No main classes found> Error
By hersman in forum NetBeansReplies: 2Last Post: 04-23-2011, 01:08 AM -
Classes, compilation error
By l flipboi l in forum New To JavaReplies: 2Last Post: 02-11-2011, 07:38 PM -
Master Handler Java error
By Dradden in forum New To JavaReplies: 4Last Post: 09-12-2010, 07:06 PM -
Help with Handler
By baltimore in forum AWT / SwingReplies: 1Last Post: 08-04-2007, 09:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks