I got a combobox, and a submit button, when the button is submitted, i want to check if the combobox value was null. Im using this code:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem().equals(null)) {
infoLabel.setText("Combo box value was null");
}
i am getting this error when i press the submit button:
java.lang.NullPointerException
how can i fix this?
You can not call equals on null. Instead simply use == null.
Something like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
Should work.
You can not give the null reference to equals(), do it like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
And a remark that has nothing to do with your question: I suggest using the Java Naming Convention, which would lead to your combo box being named comboBox (and not ComboBox).
The condition should be :
ComboBox.getSelectedItem() != null
or
ComboBox.getSelectedItem().toString().equals("")
This checks if what is selected in the Combobox is null or empty
Another way of doing this is leaving the first item empty, then check for the selected index against 0 i.e
ComboBox.getSelectedIndex() != 0
Thanks
Related
I want to bind a MenuItem of my contextMenu to an specific value of the selectedValue of the tableView.
Like you can see in the picture: if status is "anwesend" what is specified with an int = 2 in the Controller, then the option should be disabled.
I thought about this here, but it throws a NullPointer because the selection is empty:
contextMenuItemUnentschuldigt.disableProperty().bind(Bindings.createBooleanBinding(() -> !loggedIn || tableView.getSelectionModel().getSelectedItem().getExcused() == 2));
But how to check first if it's not empty and then check that it's not that specific value?
I got the answer:
contextMenuItemUnentschuldigt.disableProperty().bind(Bindings.createBooleanBinding(() -> tableView.getSelectionModel().getSelectedItems().isEmpty() || tableView.getSelectionModel().getSelectedItem().getExcused() == 2, tableView.getSelectionModel().getSelectedItems()));
Here is my code, i want to get last object from arraylist to get and set it for PrintActivity, can i?enter image description here
if(arrayList != null && !arrayList.isEmpty()){
arrayList.get(arrayList.size() - 1);
}
I currently have 2 checkboxes with an edit text field beside them. There are 4 checkboxes total and I have the logic to check each one and uncheck others if they are checked (almost radio button style). These 2 however could both be checked if they have values in the edit text fields.
However, if the EditText field (which is set to numeric) has a value of 0 or is blank I want it to uncheck the check box and set the value to 0.
Here is the code I have to do this
if (etBase.getText().toString() == "0" || etBase.getText().toString() == ""){
etBase.setText("0");
cbBase.setChecked(false);
} else {
cbBase.setChecked(true);
}
if (etField.getText().toString() == "" || etField.getText().toString() == "0"){
etField.setText("0");
cbField.setChecked(false);
} else {
cbField.setChecked(true);
}
As it sits right now I default the two fields to be "0" when it starts. When this logic runs, it is setting both checkboxes to checked.
I must be missing something here.
To compare string, you must use .equals() function. ( Or .compareTo() but that's no the today's deal)
Ex :
if (etBase.getText().equals("0") || etBase.getText().equals("")){
...
}
var1 == var2 is used to get if these both variable have the same object
while(i<word.length)
{
ans=swn.extract(word[i], pos[i]);
if(ans== null)
polarvalue[i]= " ";
else
polarvalue[i]=ans;
i++;
System.out.println(ans);
}
Hi, Friends this is my code and the swn.extracts a value which can be null so the ANS contains the null value and when i try to access it gives NULlPOinterException is there any way that i can check the NULL value and change it to any other value.? But if i removes the whole If..else section it gives no error and prints the "NULL" in the output...
If i remove the whole If..else section then the code prints the null
value.
If above is true, mean your polarvalue[] array is null and you are trying to assigned the ith position value by using polarvalue[i] that's way, it's throwing null pointer exception.
Do a null check of polarvalue[] array before assigned.
Your code is dangerous, It seems it can throw NPE every where like
while(i<word.length) // do a null check
ans=swn.extract(word[i], pos[i]); // do a null check
polarvalue[i]= " "; // do a null check
polarvalue[i]=ans; // do a null check
Do a null check it will take few minutes but reduce your most valuable time .
I am using a Bonded JComboBox. What I want to know is how do I check if that JComboBox has any items in it or not.
Thank you.
To test if the JComboxBox reference is null, you can compare it with null using the == operator. To test if the combo-box contains any items, you can use the instance method getItemCount, which returns the number of items it contains.
JComboxBox box = ...
boolean boxIsNull = (box == null); // answers the title of the question
boolean boxHasItems = (box.getItemCount() > 0);