ComboBox setNullSelectionAllowed(true) does not show item in drop down

I am working on a survey that I would like to track the complete percentage of the questions with answers. Everything works when a value is selected, but I want to be able to select a null value to un-select an answer. When I tried to do this without implicitly specifying setNullSelectionAllowed to true, I would get a NullPointerException when comboBox.getValue() was called. This guided me to set the setNullSelectionAllowed to true. After compiling and deploying, the blank value that was there initially is gone, so I cannot select a value for null. Here is the code for the ComboBox:


		final ComboBox resultBox = new ComboBox();
		resultBox.setImmediate(true);
		resultBox.setWidth("100%");
		resultBox.setNullSelectionAllowed(true);
		resultBox.addItem(-1);
		resultBox.setItemCaption(-1, " ");
		resultBox.setNullSelectionItemId(-1);

		if (question.getResponseType() == 2)
		{
			resultBox.addItem(ResponseValue.ONE.ordinal());
			resultBox.setItemCaption(ResponseValue.ONE.ordinal(), ResponseValue.ONE.display());
			resultBox.addItem(ResponseValue.TWO.ordinal());
			resultBox.setItemCaption(ResponseValue.TWO.ordinal(), ResponseValue.TWO.display());
			resultBox.addItem(ResponseValue.THREE.ordinal());
			resultBox.setItemCaption(ResponseValue.THREE.ordinal(), ResponseValue.THREE.display());
			resultBox.addItem(ResponseValue.FOUR.ordinal());
			resultBox.setItemCaption(ResponseValue.FOUR.ordinal(), ResponseValue.FOUR.display());
			resultBox.addItem(ResponseValue.FIVE.ordinal());
			resultBox.setItemCaption(ResponseValue.FIVE.ordinal(), ResponseValue.FIVE.display());
			resultBox.addItem(ResponseValue.SIX.ordinal());
			resultBox.setItemCaption(ResponseValue.SIX.ordinal(), ResponseValue.SIX.display());
			resultBox.addItem(ResponseValue.SEVEN.ordinal());
			resultBox.setItemCaption(ResponseValue.SEVEN.ordinal(), ResponseValue.SEVEN.display());
			resultBox.addItem(ResponseValue.EIGHT.ordinal());
			resultBox.setItemCaption(ResponseValue.EIGHT.ordinal(), ResponseValue.EIGHT.display());
			resultBox.addItem(ResponseValue.NINE.ordinal());
			resultBox.setItemCaption(ResponseValue.NINE.ordinal(), ResponseValue.NINE.display());
			resultBox.addItem(ResponseValue.TEN.ordinal());
			resultBox.setItemCaption(ResponseValue.TEN.ordinal(), ResponseValue.TEN.display());
		} 
		else 
		{
			resultBox.addItem(ResponseValue.PASS.ordinal());
			resultBox.setItemCaption(ResponseValue.PASS.ordinal(), ResponseValue.PASS.display());
			resultBox.addItem(ResponseValue.FAIL.ordinal());
			resultBox.setItemCaption(ResponseValue.FAIL.ordinal(), ResponseValue.FAIL.display());
			resultBox.addItem(ResponseValue.N_A.ordinal());
			resultBox.setItemCaption(ResponseValue.N_A.ordinal(), ResponseValue.N_A.display());
		}
		
		resultBox.addListener(new ComboBox.ValueChangeListener() 
		{
			
			@Override
			public void valueChange(ValueChangeEvent event) 
			{
				Result result = (Result) table.getItem(item).getItemProperty("result").getValue();
				
				if (result == null && !resultBox.isSelected(-1))
				{
					result = Result.create(survey, question);
					table.getItem(item).getItemProperty("result").setValue(result);
				}
				
				else if (result != null && resultBox.isSelected(-1))
				{
					result=Result.create(survey, question);
					table.getItem(item).getItemProperty("result").setValue(null);
				}
				
				result.setResultsValue((Integer) resultBox.getValue());
				getDataSource().updateResult(result);
			}
		});

resultBox.addListener(new Property.ValueChangeListener()
		{
			private static final long serialVersionUID = -6792325980430002436L;

			@Override
			public void valueChange(ValueChangeEvent event)
			{
				if (ignoreUpdates)
				{
					return;
				}
				
				Result result = (Result) table.getItem(item).getItemProperty("result").getValue();
				
				if (result == null)
				{
					result = Result.create(survey, question);
					table.getItem(item).getItemProperty("result").setValue(result);
				}
			
				if (resultBox.getValue().equals(ResponseValue.FAIL.ordinal()))
				{
					recommendBox.setValue(true);
					result.setRecommend(1);
					result.setResultsValue(ResponseValue.FAIL.ordinal()); 
				}
					
				else if (resultBox.getValue().equals(ResponseValue.PASS.ordinal()))
				{
					result.setResultsValue(ResponseValue.PASS.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.ONE.ordinal()))
				{
					result.setResultsValue(ResponseValue.ONE.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.TWO.ordinal()))
				{
					result.setResultsValue(ResponseValue.TWO.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.THREE.ordinal()))
				{
					result.setResultsValue(ResponseValue.THREE.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.FOUR.ordinal()))
				{
					result.setResultsValue(ResponseValue.FOUR.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.FIVE.ordinal()))
				{
					result.setResultsValue(ResponseValue.FIVE.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.SIX.ordinal()))
				{
					result.setResultsValue(ResponseValue.SIX.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.SEVEN.ordinal()))
				{
					result.setResultsValue(ResponseValue.SEVEN.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.EIGHT.ordinal()))
				{
					result.setResultsValue(ResponseValue.EIGHT.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.NINE.ordinal()))
				{
					result.setResultsValue(ResponseValue.NINE.ordinal());
				}

				else if (resultBox.getValue().equals(ResponseValue.TEN.ordinal()))
				{
					result.setResultsValue(ResponseValue.TEN.ordinal());
				}
				
				else if (resultBox.getValue().equals(ResponseValue.N_A.ordinal()))
				{
					result.setResultsValue(ResponseValue.N_A.ordinal());
				}
				
				getDataSource().updateResult(result);
				
				List<IGadget> gadgets = getApplicationContainer().getPage().getGadgets();
				
				for (IGadget gadget : gadgets)
				{
					if (gadget instanceof ClientDetailsGadget)
					{
						((ClientDetailsGadget) gadget).getBanner().calculateCompliance();
					}
					else if (gadget instanceof AssessmentGadget)
					{
						((AssessmentGadget) gadget).getBanner().calculateCompliance();
					}
				}
			}

I am getting the NullPointerException on line 88 when I don’t specify setNullSelectionAllowed, if that matters.

What could I be doing wrong? I have tried to mimic the examples using null values on vaadin.com, but I got the same result.

Thank you!

I just had a thought. Perhaps the setNullSelctionAllowed just allows a value of null to be returned for getValue(), but it does not actually have a selectable item in the list? I was thinking that it would have an item in the drop down list that could be selected and it would return null. Do I misunderstand how this works?

I must ask really unclear or really stupid questions, because I can hardly ever get a reply on my posts… :frowning: I am new to Java programming and Vaadin, so I am still just getting the basics down. I got thrown face first into a project and was told to learn it, so I need a little more help than the average developer.

Thanks!

It is my impression that setNullSelectionAllowed(true) just means that the user can select nothing, or if an item is selected, it can be unselected ctrl-clicking it.

You should get your blank choice no matter what, though, since you add it in. It’s unrelated to being null, though, as selecting the blank label will return -1 for its value.

So, if you select nothing at all, it should return null. If you call setNullSelectionItemId(-1), I believe that if the user also selects your blank element, you will get back null instead of the -1 value.

Thank you, David. I didn’t think about ctrl-clicking to deselect. The blank choice does go away when I set setNullSelectionAllowed(true) though with the -1 as the itemId. I am working through some alternatives to get it to behave the way I want it to. I think I just misunderstood the purpose of this method.

I think many people would do something like what you are doing, but instead of a blank choice for the option, use something like ‘-SELECT-’ mapping to your -1. You can still allow for null selection and setNullSelectionItemId(-1) – assuming it’s okay to not select anything and then if -SELECT- or nothing is chosen, you’ll get -1 as the value, otherwise the selected value.

Ok, I got it to work. What I had to do was remove the “-1” option altogether and it still left an empty/null value to select. I inherited the code I worked on from a more seasoned developer and the “-1” option was something he did. I figured he had it there for a reason, but I found that if I remove that item from the ComboBox altogether I get the result I want and no null pointer exception anymore. Thanks again for the guidance and ideas.