Results 1 to 12 of 12
  1. #1
    corvenzo is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default 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

    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();
        }
    
    
    }
    The error message that comes up is:

    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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    corvenzo is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default 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();
        }
    
    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: error in handler classes

    Quote Originally Posted by corvenzo View Post
    Exception in thread "main" java.lang.NullPointerException
    at DicePoker.buildPanel(DicePoker.java:49)
    So, what's on that line?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    corvenzo is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: error in handler classes

    panel.add(Die[i]);

    but what is wrong with that?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: error in handler classes

    And where did you initialize the variable panel?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    corvenzo is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: error in handler classes

    on line 19

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default Re: error in handler classes

    Quote Originally Posted by corvenzo View Post
    on line 19
    You only declared it there.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    corvenzo is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default 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.

  11. #11
    corvenzo is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default 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.

    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");
                }          
                
            }
        }
    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. ???????

  12. #12
    corvenzo is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: error in handler classes

    Hi, do you guys understand what I'm asking?

Similar Threads

  1. Multiple classes error
    By nat45928 in forum New To Java
    Replies: 0
    Last Post: 05-09-2011, 03:10 AM
  2. <No main classes found> Error
    By hersman in forum NetBeans
    Replies: 2
    Last Post: 04-23-2011, 02:08 AM
  3. Classes, compilation error
    By l flipboi l in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 08:38 PM
  4. Master Handler Java error
    By Dradden in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 08:06 PM
  5. Help with Handler
    By baltimore in forum AWT / Swing
    Replies: 1
    Last Post: 08-04-2007, 10:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •