Announcement Announcement Module
Collapse
No announcement yet.
NPE when a combo box 'reload' another combo box. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NPE when a combo box 'reload' another combo box.

    Hello,

    I have a form with two dependents combo boxes (Provinces and Cities). When the province is changed, the cities combo should be reloaded with cities of selected province. When I change the province a NPE happens in:
    Code:
    WARN  [AWT-EventQueue-0] - Null pointer exception occured setting value
    java.lang.NullPointerException
    	at org.springframework.richclient.list.ComboBoxAutoCompletion.startsWithIgnoreCase(ComboBoxAutoCompletion.java:344)
    	at org.springframework.richclient.list.ComboBoxAutoCompletion.lookupItem(ComboBoxAutoCompletion.java:272)
    	at org.springframework.richclient.list.ComboBoxAutoCompletion.insertString(ComboBoxAutoCompletion.java:224)
    	at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:668)
    	at javax.swing.text.JTextComponent.setText(JTextComponent.java:1403)
    	at javax.swing.plaf.basic.BasicComboBoxEditor.setItem(BasicComboBoxEditor.java:48)
    	at org.springframework.richclient.list.ComboBoxAutoCompletionInterceptorFactory$ComboBoxAutoCompletionInterceptor$BeanPropertyValueComboBoxEditor.setItem(ComboBoxAutoCompletionInterceptorFactory.java:127)
    	at javax.swing.JComboBox.configureEditor(JComboBox.java:1340)
    	at javax.swing.plaf.basic.BasicComboBoxUI$ListDataHandler.contentsChanged(BasicComboBoxUI.java:537)
    	at javax.swing.plaf.basic.BasicComboBoxUI$ListDataHandler.intervalRemoved(BasicComboBoxUI.java:551)
    	at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:160)
    	at org.springframework.richclient.list.ListListModel.clear(ListListModel.java:156)
    	at org.springframework.richclient.list.ComboBoxListModel.clear(ComboBoxListModel.java:113)
    	at org.springframework.richclient.list.DynamicComboBoxListModel.doAdd(DynamicComboBoxListModel.java:131)
    	at org.springframework.richclient.list.DynamicComboBoxListModel.valueChanged(DynamicComboBoxListModel.java:127)
    	at org.springframework.binding.value.support.AbstractValueModel.fireValueChanged(AbstractValueModel.java:70)
    	at org.springframework.binding.value.support.ValueHolder.setValue(ValueHolder.java:52)
    	at com.wplex.common.client.ui.location.QuarterForm.loadCities(QuarterForm.java:194)
    I solve this doing the follow changes in org.springframework.richclient.list.ComboBoxAutoCo mpletion:
    Code:
        // checks if str1 starts with str2 - ignores case
        private boolean startsWithIgnoreCase(String str1, String str2)
        {
            //TODO Mauro's code if (str1 != null && str2 != null)
                return str1.toUpperCase().startsWith(str2.toUpperCase());
            //TODO Mauro's code return false;
        }
    And
    Code:
        /**
         * @see javax.swing.text.Document#insertString(int, java.lang.String,
         *      javax.swing.text.AttributeSet)
         */
        public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException
        {
            ... code code code ...
    
            // if the user selects an item via mouse the the whole string will be
            // inserted.
            // highlight the entire text if this happens.
              /*TODO Mauro's code  if (itemString != null)
            {*/
                if (itemString.equals(str) && offs == 0)
                {
                    highlightCompletedText(0);
                }
                else
                {
                    highlightCompletedText(offs + str.length());
                    // show popup when the user types
                    if (comboBox.isDisplayable())
                        comboBox.setPopupVisible(true);
                }
             //TODO Mauro's code }
           }
    I don't know if I miss some in my code I should to do or if it's a JIRA issue.

    Follow my code:
    Code:
    /*
     * $Id: CityForm.java,v 1.3 2005/04/11 14:20:25 mauro Exp $
     * 
     * Copyright (c) 2004-2005 Wplex Software Ltda. All rights reserved.
     */
    package com.wplex.common.client.ui.location;
    
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JOptionPane;
    
    import org.springframework.binding.form.FormModel;
    import org.springframework.binding.value.ValueModel;
    import org.springframework.binding.value.support.ValueHolder;
    import org.springframework.richclient.forms.AbstractForm;
    
    import com.jgoodies.forms.factories.FormFactory;
    import com.jgoodies.forms.layout.CellConstraints;
    import com.jgoodies.forms.layout.ColumnSpec;
    import com.jgoodies.forms.layout.FormLayout;
    import com.jgoodies.forms.layout.RowSpec;
    
    /**
     * Formulário para bairro.
     * 
     * @author Mauro Ransolin
     * @version $Revision: 1.3 $
     *
     */
    public class QuarterForm extends AbstractForm
    {
        /**
         * Nome do formulário para lookup pelo Spring.
         */
        public static final String QUARTER_FORM_PAGE = "quarterPage"; //$NON-NLS-1$
    
        CommonQueryDelegate delegate;
    
        ValueModel citiesModel = new ValueHolder();
    
        /**
         * @param formModel
         */
        public QuarterForm(FormModel formModel)
        {
            super(formModel, QUARTER_FORM_PAGE);
            try
            {
                this.delegate = CommonQueryDelegate.getInstance();
            }
            catch (ConfigurationError e)
            {
                JOptionPane.showMessageDialog(this.getControl(), e.getMessage());
            }
        }
    
        protected JComponent createFormControl()
        {
            getObjectConfigurer().configure(this, QUARTER_FORM_PAGE);
            CellConstraints cc = new CellConstraints();
            FormLayout layout = new FormLayout(new ColumnSpec[] {
                FormFactory.DEFAULT_COLSPEC,
                FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
                new ColumnSpec("max(default;40dlu)"), //$NON-NLS-1$
                FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
                new ColumnSpec("max(default;90dlu)") }, new RowSpec[] { //$NON-NLS-1$
                FormFactory.DEFAULT_ROWSPEC, FormFactory.LINE_GAP_ROWSPEC,
                    FormFactory.DEFAULT_ROWSPEC, FormFactory.LINE_GAP_ROWSPEC,
                    FormFactory.DEFAULT_ROWSPEC });
    
            WplexCommonFormBuilder formBuilder = new WplexCommonFormBuilder(
                getFormModel(), layout);
    
            //state
            formBuilder.add(getFormModel().createLabel("state"), //$NON-NLS-1$
                cc.xy(1, 1));
            formBuilder.add(createStatesField(), cc.xy(3, 1));
    
            //city
            formBuilder.add(getFormModel().createLabel("city"), //$NON-NLS-1$
                cc.xy(1, 3));
            formBuilder.add(createCitiesField(), cc.xywh(3, 3, 3, 1));
    
            return formBuilder.getForm();
        }
    
        /**
         * @return JComboBox com provincias.
         */
        private JComponent createStatesField()
        {
            //create..."city" precisa estar aqui para não ocorrer npe!
            getFormModel().createFormValueModel("city"); //$NON-NLS-1$
            getFormModel().createFormValueModel("state"); //$NON-NLS-1$
            ValueHolder statesHolder = null;
            try
            {
                Collection states = this.delegate.findStates();
                Collection filteredStates = new ArrayList();
                for (Iterator iter = states.iterator(); iter.hasNext();)
                {
                    State state = (State) iter.next();
                    if (!this.delegate.findCities(state).isEmpty())
                        filteredStates.add(state);
                }
                statesHolder = new ValueHolder(filteredStates);
            }
            catch (WplexError e)
            {
                JOptionPane.showMessageDialog(this.getControl(), e.getMessage());
            }
    
            JComboBox jcbStates = getFormModel().createBoundComboBox(
                "state", statesHolder, "code"); //$NON-NLS-1$ //$NON-NLS-2$
    
            jcbStates.addItemListener(new ItemListener()
            {
                public void itemStateChanged(ItemEvent event)
                {
                    if (event.getStateChange() == ItemEvent.SELECTED)
                    {
                        loadCities(((State) event.getItem()));
                    }
                }
            });
            return jcbStates;
        }
    
        /**
         * @return JComboBox com cidades.
         */
        private JComponent createCitiesField()
        {
            ValueModel statesModel = getFormModel().getValueModel("state"); //$NON-NLS-1$
            State state = (State) statesModel.getValue();
            try
            {
                this.citiesModel = new ValueHolder(this.delegate.findCities(state));
            }
            catch (WplexError e)
            {
                JOptionPane.showMessageDialog(this.getControl(), e.getMessage());
            }
    
            return getFormModel().createBoundComboBox(
                "city", this.citiesModel, "name"); //$NON-NLS-1$ //$NON-NLS-2$
        }
    
        /**
         * Recarrega as cidades quando o estado for alterado.
         * @param state
         */
        private void loadCities(State state)
        {
            try
            {
                this.citiesModel.setValue(this.delegate.findCities(state));
            }
            catch (WplexError e)
            {
                JOptionPane.showMessageDialog(getControl(), e.getMessage());
            }
    
        }
    }
    Code:
    /*
     * $Id:$
     * 
     * Copyright (c) 2004-2005 Wplex Software Ltda. All rights reserved.
     */
    
    package com.wplex.common.client.struct;
    
    /**
     * @author Mauro Ransolin
     * @version $Revision:$
     *
     */
    public class CityStruct
    {
        private StateVO state;
    
        private CityVO city;
    // +getters, setters, equals, hash...
    }
    Can someone help me?

    ps: all my provinces have cities and I'm using JDK 1.4.2_08

    BTW, I'm hungry to see the RCP FCS

    TIA,

    Mauro.

  • #2
    Hi folks,

    My problem is already mapped to JIRA: RCP-106

    Thanks all!

    Comment


    • #3
      BTW, I'm hungry to see the RCP FCS Smile
      it will be today

      Comment

      Working...
      X