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

    Default reference nulpointer

    I'd like to create various checkboxes and need a reference of each checkbox outside the method block.
    So I thought this might work. But no, the reference gives (of course) a nullpointer!
    How to get around this in a elegant manner (I could have an extra string parameter of course...)

    Java Code:
    public void CreateCheckBoxes2(JCheckBox cb, boolean b)  {
            cb = new JCheckBox(cb.toString());
            cb.setSelected(b);
            upperPanel.add(cb);
        }
    The fun is, of course, to have the object name of the checkbox also as a string identifier, right!
    So the question is: how to convert an object reference name into a string (the object is not created yet)?
    Last edited by willemjav; 03-09-2013 at 12:39 AM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,502
    Rep Power
    5

    Default Re: reference nulpointer

    You can't return a reference via the parameter list (at least not the way you are trying it). Return a JCheckBox instance.
    How about this?


    Java Code:
    public JCheckBox CreateCheckBoxes2(String cbname,  boolean b)  {
            JCheckBox cb = new JCheckBox(cbname);
            cb.setSelected(b);
            upperPanel.add(cb);
            return cb;
        }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: reference nulpointer

    Yes, Jim I know you can do that, return a created object through a method.
    I was trying out the sintaxis of java and first wrote a method that would create a series of JButtons. I lost the object reference of each Button, but sense the ActionListener object of each button was doing its job I did not care, right!

    Then I wanted to do the same when creating Checkboxes and the method worked fine also for the checkboxes. But when I use the isSelected() method I needed the reference to each checkbox object, which I did not have, so what now:

    Java Code:
     public void CreateButton(final String b)  {
            JButton button = new JButton(b);
            lowerPanel.add(button);
            button.addActionListener(new
              ActionListener()
              {
                 public void actionPerformed(ActionEvent e)
                 {
                    System.out.println(" button pressed " + b);  
                 }
              });
    
        }
    Here I did the same concerning radiobuttons (loosing the button reference):

    Java Code:
    public void CreateRadioButton(final String RB,  
                boolean b, ButtonGroup gr)  {
         //   ButtonGroup group = new ButtonGroup();
            final JRadioButton radiob = new JRadioButton(RB, b);
            gr.add(radiob);
            upperPanel.add(radiob);
            radiob.addActionListener(new
              ActionListener()
              {
                 public void actionPerformed(ActionEvent e)
                 {
                    System.out.print(" radiobutton " + RB + "  ");
                    if (radiob.isSelected()) System.out.println(" radiobutton true ");
                    else System.out.println(" radiobutton false ");
    
                 }
              });
    
        }
    I noticed that the buttongroup should be created outside the method in order to work
    Last edited by willemjav; 03-09-2013 at 09:43 AM.

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

    Default Re: reference nulpointer

    Thanks Jim here is what I did:

    Java Code:
     public JCheckBox CreateCheckBoxes(final String cb, boolean b)  {
            final JCheckBox cbox = new JCheckBox(cb);
            cbox.setSelected(b);
            upperPanel.add(cbox);
    
            cbox.addActionListener(new
              ActionListener()
              {
                 public void actionPerformed(ActionEvent e)
                 {
                    System.out.print(" checkbox " + cb + "  ");
                    if (cbox.isSelected()) System.out.println(" checkbox true ");
                    else System.out.println(" checkbox false ");
    
                 }
              });
              return cbox;
        }
    The return value is assigned to a field and can be read out by field.isSelected().
    great thanks

    willem

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

    Default Re: reference nulpointer

    In conclusion:
    In case of the Buttons, I am only interested in the ActionListener that fires when pressed
    In case of the Checkboxes I have stored each box in a JCheckBox field and use cbox.isSelected()
    And concerning the RadioButtons I have just one field that holds the one selected.

Similar Threads

  1. Reference !
    By Jack_Tauson_Sr in forum New To Java
    Replies: 4
    Last Post: 12-26-2012, 03:37 PM
  2. What to do with object reference?
    By kyle_maddisson in forum New To Java
    Replies: 6
    Last Post: 11-04-2011, 05:58 AM
  3. Don't pass by reference
    By Lyven in forum Advanced Java
    Replies: 6
    Last Post: 11-16-2009, 06:06 PM
  4. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 03:45 PM
  5. Pass-by-reference
    By popjava in forum New To Java
    Replies: 1
    Last Post: 10-19-2009, 03:45 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
  •