Results 1 to 3 of 3
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default the nullpointer plague

    I ran into the following problem:
    I need to have the action listener before starting the GUI (see createDialogPanel(true))).
    But than the action listener seems to fire an event that causes the null pointer (see below).
    Of course I could envoke the listener after adding the dialog panel to the content panel,
    but than the action listener does not work: A silly paradox!
    Anyway why does the listener fires an event when it gets installed anyway?
    (the listener checks a bunch of checkboxes and sets combo boxes available or not)


    dlistener = new DialogListener();
    Container contentPane = getContentPane();
    contentPane.add(createDialogPanel(true));


    Exception in thread "main" java.lang.NullPointerException
    at roomassigment.CalenderDialogs$DialogListener.actio nPerformed(CalenderDialogs.java:246)
    at javax.swing.JComboBox.fireActionEvent(JComboBox.ja va:1240)
    at javax.swing.JComboBox.setSelectedItem(JComboBox.ja va:567)
    at javax.swing.JComboBox.setSelectedIndex(JComboBox.j ava:603)
    at roomassigment.CalenderDialogs.createSinglePanel(Ca lenderDialogs.java:194)
    at roomassigment.CalenderDialogs.createDialogPanel(Ca lenderDialogs.java:109)
    at roomassigment.CalenderDialogs.<init>(CalenderDialo gs.java:55)
    at roomassigment.CalenderDialogs.main(CalenderDialogs .java:34)
    Java Result: 1


    Java Code:
    private class DialogListener implements ActionListener {
            public void actionPerformed(ActionEvent e)  {
                String cmd = e.getActionCommand();
    
                 for (int i = 0; i < comboref.length ; i++) { //comboref.length
                   if (comboref[i].getboxref().isSelected())  {
                            comboref[i].getcomboA1().setEnabled(true);   
                            comboref[i].getcomboA2().setEnabled(true);
                            comboref[i].getcomboB1().setEnabled(true);
                            comboref[i].getcomboB2().setEnabled(true);
                   }
                   else {
                            comboref[i].getcomboA1().setEnabled(false);  //<- line 246
                            comboref[i].getcomboA2().setEnabled(false);
                            comboref[i].getcomboB1().setEnabled(false);
                            comboref[i].getcomboB2().setEnabled(false);
                            comboref[i].getcomboA1().setSelectedIndex(0);
                            comboref[i].getcomboA2().setSelectedIndex(0);
                            comboref[i].getcomboB1().setSelectedIndex(0);
                            comboref[i].getcomboB2().setSelectedIndex(0);
                   }
                }
                
                if (cmd.equals("COMBO")) {
                //  System.out.println("combo listener!");
                }  
           }
        }
    Last edited by willemjav; 05-11-2013 at 08:39 PM.

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: the nullpointer plague

    this one works, please any comments on the program


    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package roomassigment;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /**
     *
     * @author willem
     */
    public class CalenderDialogs extends JDialog{
        private ImageIcon icon;
        private ActionListener dlistener;
        private JCheckBox selectbox;
        private  JComboBox hourscombo1, hourscombo2, minutescombo1, minutescombo2;
        private JTextField infoField;
        private JTextArea infoarea;
        private JButton okb, chancelb, resetb;
        private ComponentRef[] comboref;
        private int MAX_FIELD = 0, count;
        private static boolean flag = false;
    
        public static void main(String[] args) {
           CalenderDialogs dialog = new CalenderDialogs(flag);
           dialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
           dialog.setBounds(300, 300, 520, 350);
         //  dialog.setBounds(300, 300, 760, 520);
           dialog.setVisible(true);
        }
    
        public CalenderDialogs(boolean fl)       {
            icon = createImageIcon("http://www.java-forums.org/images/lic2.gif");
    
            count = 0;
            if (fl) MAX_FIELD = 7;
            else MAX_FIELD = 10;
            
            dlistener = new DialogListener();
            comboref = new ComponentRef[MAX_FIELD]; // initialize the dynamic
                for (int i = 0; i < comboref.length; i++)  // calenderinfopanel array
                        comboref[i] = new ComponentRef();
      
            Container contentPane = getContentPane();
            contentPane.add(createDialogPanel(fl));
            
    
        }
    
    
        private JPanel createDialogPanel(boolean flag)  { // flag for two panel types
            
            JPanel panel = new JPanel(new BorderLayout());
            JPanel iconpanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 30, 10));
            JPanel buttonpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            JLabel iconlabel = new JLabel(icon);
            iconpanel.add(iconlabel);
    
            panel.setBackground(Color.WHITE);
            iconpanel.setBackground(Color.WHITE);
            buttonpanel.setBackground(Color.WHITE);
    
            infoarea = new JTextArea(3,25);
            infoarea.setPreferredSize(new Dimension(20,20));
            infoarea.setFont(new Font("Serif", Font.ITALIC, 16));
            infoarea.setEditable(false);
            if (flag) {
                infoarea.setText("Set the schedule of the week days\n"
                             + "when active, check the box and\n"
                             + "and enter the duration");
            }
            else  {
                infoarea.setText("Set several vacations and\n"
                             + "class free periods by checking\n"
                             + "the box and typing a short info");
            }
            iconpanel.add(infoarea);
    
            okb = new JButton("OK");
            okb.setActionCommand("OK");
            
            chancelb = new JButton("CHANCEL");
            
            chancelb.setActionCommand("CH");
            resetb = new JButton("RESET");
            resetb.setActionCommand("RS");
            buttonpanel.add(resetb);
            buttonpanel.add(okb);
            buttonpanel.add(chancelb);
            
            okb.addActionListener(dlistener);
            resetb.addActionListener(dlistener);
            chancelb.addActionListener(dlistener);
           
    
         //   iconlabel.setHorizontalTextPosition(SwingConstants.LEADING);
            JPanel comppanel = new JPanel();
            comppanel.setBackground(Color.WHITE);
            comppanel.setLayout(new BoxLayout(comppanel,
                    BoxLayout.LINE_AXIS));
            
            Box bh = Box.createHorizontalBox();
            Box bv = Box.createVerticalBox();
    
            if (flag) {
                bv.add(createSinglePanel("lunes    ", flag));
                bv.add(createSinglePanel("martes   ", flag));
                bv.add(createSinglePanel("miercoles", flag));
                bv.add(createSinglePanel("jueves   ", flag));
                bv.add(createSinglePanel("viernes  ", flag));
                bv.add(createSinglePanel("sabado   ", flag));
                bv.add(createSinglePanel("domingo  ", flag));
            }
            else  {
                bv.add(createSinglePanel("1 ", flag));
                bv.add(createSinglePanel("2 ", flag));
                bv.add(createSinglePanel("3 ", flag));
                bv.add(createSinglePanel("4 ", flag));
                bv.add(createSinglePanel("5 ", flag));
                bv.add(createSinglePanel("6 ", flag));
                bv.add(createSinglePanel("7 ", flag));
                bv.add(createSinglePanel("8 ", flag));
                bv.add(createSinglePanel("9 ", flag));
                bv.add(createSinglePanel("10", flag));
            }
    
            comppanel.add(bv);
            
            panel.add(iconpanel, BorderLayout.NORTH);
            panel.add(comppanel, BorderLayout.CENTER);
            panel.add(buttonpanel, BorderLayout.SOUTH);
            return panel;
        }
    
    
         private Box createSinglePanel(String label, boolean flag)  {
            String[] list1;
            String[] list2;
            String[] hourlist = {"7", "8", "9", "10", "11", "12", "13",
                                "14", "15", "16", "17", "18", "19", "20",
                                  "21", "22", "23"};
            String[] minlist = {"00", "30"};
            
            String[] monthlist = {"enero", "febrero", "marzo", "abril", "mayo", 
                                   "junio", "julio", "augusto", "septembro", 
                                   "octobre", "noviembre", "deciembre"};
            
            String[] daylist = {"1", "2", "3", "4", "5", "6", "7", "8", "9", 
                                "10", "11", "12", "13","14", "15", "16", "17", 
                                "18", "19", "20", "21", "22", "23", "24", "25",
                                "26", "27", "28", "29", "30", "31"};
    
            if (flag)  {
                list1 = hourlist;
                list2 = minlist;
    
            }
            else  {
                list1 = monthlist;
                list2 = daylist;
            }
    
            JLabel lb = new JLabel(" until ");
            Box bx = Box.createHorizontalBox();
    
            selectbox = new JCheckBox(label);
            selectbox.setActionCommand("SELECT");
            selectbox.setSelected(false);
            selectbox.addActionListener(dlistener);
            comboref[count].setboxref(selectbox);
           
            hourscombo1 = new JComboBox(list1);
            minutescombo1 = new JComboBox(list2);
            hourscombo2 = new JComboBox(list1);
            minutescombo2 = new JComboBox(list2);
            lb.setLabelFor(hourscombo2);
    
            hourscombo1.setSelectedIndex(0);
            minutescombo1.setSelectedIndex(0);
            hourscombo2.setSelectedIndex(0);
            minutescombo2.setSelectedIndex(0);
           
            comboref[count].setcomboA1(hourscombo1);
            comboref[count].setcomboA2(minutescombo1);
            comboref[count].setcomboB1(hourscombo2);
            comboref[count].setcomboB2(minutescombo2);
            hourscombo1.setEnabled(false);
            hourscombo2.setEnabled(false);
            minutescombo1.setEnabled(false);
            minutescombo2.setEnabled(false);
    
    
            infoField = null;
            if (!flag)  {
                    infoField = new JTextField(23);
                    infoField.setMaximumSize(infoField.getPreferredSize());
                    infoField.setText("info of vacation etc");
                    infoField.setEnabled(false);
                    comboref[count].settextfield(infoField);
            }
    
    
            bx.add(selectbox);
            if (!flag)
            bx.add(infoField);
            bx.add(Box.createHorizontalGlue());
            bx.add(hourscombo1);
            bx.add(minutescombo1);
            bx.add(lb);
            bx.add(hourscombo2);
            bx.add(minutescombo2);
            count++;
            return bx;
        }
    
         private class DialogListener implements ActionListener {
            public void actionPerformed(ActionEvent e)  {
                String cmd = e.getActionCommand();
             if (cmd.equals("SELECT")) {
                 for (int i = 0; i < comboref.length ; i++) { //comboref.length
                        if (comboref[i].getboxref().isSelected())  {
    
                            comboref[i].getcomboA1().setEnabled(true);
                            comboref[i].getcomboA2().setEnabled(true);
                            comboref[i].getcomboB1().setEnabled(true);
                            comboref[i].getcomboB2().setEnabled(true);
                            if (!flag)
                            comboref[i].gettextfield().setEnabled(true);
                        }
                        else {
                            comboref[i].getcomboA1().setEnabled(false);
                            comboref[i].getcomboA2().setEnabled(false);
                            comboref[i].getcomboB1().setEnabled(false);
                            comboref[i].getcomboB2().setEnabled(false);
                            comboref[i].getcomboA1().setSelectedIndex(0);
                            comboref[i].getcomboA2().setSelectedIndex(0);
                            comboref[i].getcomboB1().setSelectedIndex(0);
                            comboref[i].getcomboB2().setSelectedIndex(0);
                            if (!flag)
                            comboref[i].gettextfield().setEnabled(false);
                        }
                }
           }
                
                if (cmd.equals("OK")) {
                  System.out.println("OK");
                }
                if (cmd.equals("CH")) {
                  System.out.println("CHANCEL");
                }
                if (cmd.equals("RS")) {
                    for (int i = 0; i < comboref.length ; i++) { //comboref.length
    
                            comboref[i].getboxref().setSelected(false);
                            comboref[i].getcomboA1().setEnabled(false);
                            comboref[i].getcomboA2().setEnabled(false);
                            comboref[i].getcomboB1().setEnabled(false);
                            comboref[i].getcomboB2().setEnabled(false);
                            comboref[i].getcomboA1().setSelectedIndex(0);
                            comboref[i].getcomboA2().setSelectedIndex(0);
                            comboref[i].getcomboB1().setSelectedIndex(0);
                            comboref[i].getcomboB2().setSelectedIndex(0);
                       if (!flag)  {
                           comboref[i].gettextfield().setText("info of vacation etc");
                           comboref[i].gettextfield().setEnabled(false);
                       }
    
                   }
                }
           }
        }
    
    
        protected static ImageIcon createImageIcon(String path) { // loads the icons
            java.net.URL imgURL = StudentMonitor.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
       }
    
       class ComponentRef {
           private JCheckBox boxref;
           private JComboBox comborefA1, comborefA2, comborefB1, comborefB2;
           private JTextField textfieldref;
    
           public void setboxref(JCheckBox br)  {
                    boxref = br;
           }
           public JCheckBox getboxref()  {
                    return boxref;
           }
    
           public void setcomboA1(JComboBox cb)  {
                   comborefA1 = cb;
           }
           public JComboBox getcomboA1()  {
                    return comborefA1;
           }
    
           public void setcomboA2(JComboBox cb)  {
                   comborefA2 = cb;
           }
           public JComboBox getcomboA2()  {
                    return comborefA2;
           }
    
           public void setcomboB1(JComboBox cb)  {
                   comborefB1 = cb;
           }
           public JComboBox getcomboB1()  {
                    return comborefB1;
           }
    
           public void setcomboB2(JComboBox cb)  {
                   comborefB2 = cb;
           }
           public JComboBox getcomboB2()  {
                    return comborefB2;
           }
    
           public void settextfield(JTextField tf)  {
                   textfieldref = tf;
           }
           public JTextField gettextfield()  {
                    return textfieldref;
           }
    
       }
    
    }
    Last edited by willemjav; 05-12-2013 at 02:42 AM.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: the nullpointer plague

    sorry for the english/ spanish spelling, not focussed on that right now!

Similar Threads

  1. nullpointer exception
    By empire in forum Eclipse
    Replies: 3
    Last Post: 06-12-2013, 03:31 AM
  2. why this pgm is getting NUllPointer.
    By javastuden in forum New To Java
    Replies: 2
    Last Post: 02-23-2013, 10:13 AM
  3. NULLPointer Exception
    By phyzix5761 in forum New To Java
    Replies: 7
    Last Post: 11-08-2012, 04:53 AM
  4. NullPointer help
    By moosick in forum AWT / Swing
    Replies: 1
    Last Post: 04-02-2011, 02:35 AM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 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
  •