Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Actionlistener problem

    Hey everyone.

    I'm working on a project with JFrame, and I've got a problem I can't solve.

    The basic idea is, I'm writing a gui for another class. The Gui needs to be able to add information to the background class, and be able to change that information. Mostly, I'm using JComboBox for that.

    Now, in one area, I have a JCombobox (wBox) which is an ordered list of numbers, from 1 to 18. The user selects one of those numbers, and then it creates a new JComboBox, (jBo) which goes from 0- wBox. So if the user selects '4' for wBox, jBo will be a box with selections '0' '1' '2' '3' '4'.
    (It also stores the chosen value of wBox and jBo in the background class, does some calculations off them, and runs the autoCalc() method which changes certain JLabels accordingly so the user can see them.)

    In theory, at least.

    In practice, it doesnt work properly.

    Code:
    String[] wString = new String [] {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"};
    JComboBox wBox = new JComboBox(wString);
    JComboBox jBo;
    
    public void frame()
    {
    
    String[] jjChoice = new String[] {"0"};
    jBo = new JComboBox(jjChoice); 
    		 
    JFrame f = new JFrame();
    f.setVisible(true);
    f.setSize(400,450);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		 
    JPanel p= new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    
    c.gridx = 1;
    c.gridy = 3;
    p.add(wMP, c);
    
    
    
    wBox .addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
     {
     autoCalc();
    			 
     String s = wBox.getSelectedItem().toString();
     workS.setW(s);
    
    jBo.removeAllItems();
    for(int i = 0; i <= workS.wChoice; i++)
     {
     String temp = ("" + i);
     jBo.addItem(temp);
     }
     autoCalc();
     }
     });
    
    
    
    jBo.addActionListener(new ActionListener() //TODO This donsnt work!
    {
    public void actionPerformed(ActionEvent e)
    {
    autoCalc();
     String s = "0";
     try{
     s = jBo.getSelectedItem().toString(); 
         }
      catch (ArrayIndexOutOfBoundsException o)
    {
    System.out.println("Array out of bounds");;
    }
    catch (NullPointerException m)
    {
    System.out.println("Null Pointer");
    } 
     workS.setJ(s);				 
     autoCalc();
     }
     });
    
    }
    The problem seems to be that when I change wBox, it throws a Null Pointer Exception. When I change jBo, it doesn't run autoCalc. Or at least, it doesn't show any changes.

    Help!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What does autocalc do? According to this code, it does run autocalc when you select an item in the list.
    The NullPointerException comes from changing the wBox to a different number which runs this block:
    PHP Code:
    jBo.removeAllItems();
    for(
    int i 0<= workS.wChoicei++)
     {
     
    String temp = ("" i);
     
    jBo.addItem(temp);
     } 
    As soon as you have called the removeAllItems() the selected index has now changed in jBo to point at nothing, which is then called with a toString() throwing a nullpointerexception. It is caught, and can be ignored if you want. If there was an item previously selected, it will select the index at 0 if its still available so this will trigger the event twice.


  •  

    Posting Permissions

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