JavaFX TextField causes NullPointerException - nullpointerexception

I am using a Button with an event to control the input of a user, but I am having trouble checking whether or not the TextField is empty.
The TextField and Button are declared before the button event, like
TextField svar = new TextField();
Button submitB = new Button("submit");
This is my code:
submitB.setOnAction((event) -> {
if (svar.getText().equals(null) {
primaryStage.setTitle("No input!");
}
if (svar.getText().subSequence(0, 1).toString().toLowerCase().equals(animals.get(pic).getName().subSequence(0, 1).toString().toLowerCase())) {
primaryStage.setTitle("RÄTT!");
bild.setImage(new Image(animals.get(pic+=1).getImgsrc()));
svar.setText(null);
svar.requestFocus();
}
else
{
primaryStage.setTitle("FEL!");
svar.setText(null);
svar.requestFocus();
}
});
This piece of the code above is what in my mind should handle a situation where the TextField is empty:
if (svar.getText().equals(null) {
primaryStage.setTitle("No input!");
}
However I am still getting a NullPointerException no matter what. I've tried some different solutions which have all failed and I'm hoping someone can point me in the right direction. Thanks!

Instead of calling equals to check for null, you should use "==" like:
if (svar.getText() == null) {
Reason you get NPE (NullPointerException) is, since as you say svar.getText() is null, you are trying to call equals on null and a reason for NPE.

The correct way to do it, is not check for Null, but to check for a empty String:
if (svar.getText().equals("") {
primaryStage.setTitle("No input!");
}
Some prefer:
if ("".equals(svar.getText() {
primaryStage.setTitle("No input!");
}

Related

Optimise Kotlin code for button clicked state

I want to change the button background when the button clicked, the function is work by using this code
bank1.setOnClickListener {
bank1.setBackgroundResource(R.drawable.selected_btn_border_blue_bg);
bank2.setBackgroundResource(R.drawable.default_option_border_bg);
bank3.setBackgroundResource(R.drawable.default_option_border_bg);
bank4.setBackgroundResource(R.drawable.default_option_border_bg);
}
bank2.setOnClickListener {
bank2.setBackgroundResource(R.drawable.selected_btn_border_blue_bg);
bank1.setBackgroundResource(R.drawable.default_option_border_bg);
bank3.setBackgroundResource(R.drawable.default_option_border_bg);
bank4.setBackgroundResource(R.drawable.default_option_border_bg);
}
bank3.setOnClickListener {
bank3.setBackgroundResource(R.drawable.selected_btn_border_blue_bg);
bank2.setBackgroundResource(R.drawable.default_option_border_bg);
bank1.setBackgroundResource(R.drawable.default_option_border_bg);
bank4.setBackgroundResource(R.drawable.default_option_border_bg);
}
bank4.setOnClickListener {
bank4.setBackgroundResource(R.drawable.selected_btn_border_blue_bg);
bank2.setBackgroundResource(R.drawable.default_option_border_bg);
bank3.setBackgroundResource(R.drawable.default_option_border_bg);
bank1.setBackgroundResource(R.drawable.default_option_border_bg);
}
But it kinda hardcoded, and make it to so many lines, any way to make the code shorter?
I would keep a variable that keeps track of the selected one like
private var selectedBank: View? = null
And then do
arrayOf(bank1, bank2, bank3, bank4).forEach {
it.setOnClickListener {
selectedBank?.setBackgroundResource(R.drawable.default_option_border_bg)
selectedBank = it
it.setBackgroundResource(R.drawable.selected_btn_border_blue_bg)
}
}
you only need to deselected the previous selected one

Button is always null when trying to find it?

I have this code:
val btn_click_me = findViewById(R.id.button1) as? Button
btn_click_me?.setOnClickListener {
// do something
}
which gives me this error:
java.lang.NullPointerException: null cannot be cast to non-null type android.widget.Button even though it exists in my layout file
the button is always null. how can i fix ? how can I set on click listener to button?
doing this from fragment...
You need context to find your ViewById. I recommend doing with requireContext() like this
val btn_click_me = requireContext().findViewById(R.id.button1) as? Button
btn_click_me?.setOnClickListener {
// do something
}
Also, if you wanna look into other ways of doing this, I recommend looking for viewBinding

Select a hidden element with PHPunit

Is it possible to locate an element altho it is hidden? I want a function that looks for my tooltip that says something was not filled correctly in my form.
the function looks like this:
public function checkTooltip()
{
$element = $this->byClassName('tooltip');
if ($element->displayed())
{
echo "something was not filled correctly";
}
}
$var1->value('hello');
$this->checkTooltip();
$var2->value('world');
$this->checkTooltip();
$var3->value('!');
$this->checkTooltip();
This works perfectly when the tooltip appears but if there is nothing wrong I get the message:
"PHPUnit_Extensions_Selenium2TestCase_WebDriverException: no such element"
And this make sence because it's hidden when nothing is going wrong but I want to select that element anyways, becouse I want my test to make sure that it's not vissible during my test to make sure everything is okey.So is there a way to select a hidden element using phpunit_Selenium2TestCase?
This worked as a charm!
public function checkError()
{
$tooltip = $this->elements($this->using('css selector')->value('div.active div.invalid'));
if(count($tooltip) != 0)
{
foreach ($tooltip as $tool)
{
$warning = $tool->text();
echo "You got a invalid tooltip with this message '".$warning."'";
}
}
}

actionscript 2.0 input text box

So, here's what I'm trying to do, and I, frankly, believe it should be obvious, but I can't figure it out. I am creating a very simple Artificial Intelligence simulation. And in this simulation there's an input box at the bottom of the screen (called "input" exactly). "input" has a variable in its properties that is called "inbox" (exactly). Using a key listener the script calls up a function when the enter button is pressed. This function has 2 if statements and an else statement which dictate the responses of the AI (named "nistra"). The problem is this, When I type in what I want to say, and hit enter, it always uses the second response ("lockpick" in the code below). I have tried variations on the code but I still don't see the solution. I believe the problem is that the "typein" variable holds all the format information from the text box as well as the variable, but I could be wrong, that information is in here as well, underneath the code itself. Any help I can get would be greatly appreciated.
var typein = ""; //copies the text from inbox into here, this is what nistra responds to
var inbox = ""; //this is where the text from the input text box goes
var respond = ""; //nistra's responses go here
my_listener = new Object(); // key listener
my_listener.onKeyDown = function()
{
if(Key.isDown(13)) //enter button pressed
{
typein = inbox; // moves inbox into typein
nistraresponse(); // calles nistra's responses
}
//code = Key.getCode();
//trace ("Key pressed = " + code);
}
Key.addListener(my_listener); // key listener ends here
nistraresponse = function() // nistra's responses
{
trace(typein); // trace out what "typein" holds
if(typein = "Hello") // if you type in "Hello"
{
respond = "Hello, How are you?";
}
if(typein = "lockpick") // if you type in "lockpick"
{
respond = "Affirmative";
}
else // anything else
{
respond = "I do not understand the command, please rephrase";
}
cntxtID = setInterval(clearnistra, 5000); // calls the function that clears out nistra's response box so that her responses don't just sit there
}
clearnistra = function() // clears her respond box
{
respond = "";
clearInterval(cntxtID);
}
// "typein" traces out the following
<TEXTFORMAT LEADING="2"><P ALIGN="CENTER"><FONT FACE="Times New Roman" SIZE="20" COLOR="#FF0000" LETTERSPACING="0" KERNING="0">test</FONT></P></TEXTFORMAT>
Since ActionScript is based on ECMAScript I'm pretty sure that you need to use == instead of = for equality comparison.
Right now your code works like this:
if(typein = "Hello") { // assign "Hello" to typein. always true.
respond = "Hello, How are you?";
}
if(typein = "lockpick") { // assign "lockpick" tot ypein. always true.
respond = "Affirmative";
}
// the else block is always false for obvious reasons
So you simply need to change the code like this:
if(typein == "Hello") {
respond = "Hello, How are you?";
}
else if(typein == "lockpick") {
respond = "Affirmative";
}
else {
respond = "I do not understand the command, please rephrase";
}

Close editor using IEditorreference? ( eclipse RCP )

I have a UI in which when I select an item (in a tree) and then press a button "add", I get a new editor. With each item I can get an editor. (but all have the same ID)
My purpose is to close only the editor of item1, for example, when I press "save".
I'm able to close all the editors with:
getSite().getWorkbenchWindow().getActivePage().closeAllEditors(true);
But not only the one that I need to close.
I think, this problem might be solved using the IEditorreferences but don't know exactly how to do it! :(
please help :)
List<IEditorReference> editors = new ArrayList<IEditorReference>();
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
for (IWorkbenchPage page : window.getPages()) {
for (IEditorReference editor : page.getEditorReferences()) {
editors.add(editor);
}
}
}
getSite().getWorkbenchWindow().getActivePage().closeEditor(editors.get(index)????,true);
Editor can be tracked with the editor-input. The object representing item1 must be part of your editor-input...
Something like:
// Creating and opening
MyObject item1 = ... //create item1
// open editor
myInput = new MyEditorInput(item1)
IDE.openEditor(workbenchPage, myInput, MY_EDITOR_ID);
// Closing
tmpInput = new MyEditorInput(item1)
IEditorReference[] editorReferences = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getEditorReferences();
List<IEditorReference> relevantEditors = new ArrayList<IEditorReference>();
for (IEditorReference iEditorReference : editorReferences) {
if (iEditorReference.getEditorInput().equals(tmpInput)) {
relevantEditors.add(iEditorReference);
}
}
PlatformUI
.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage()
.closeEditors(
(IEditorReference[]) relevantEditors.toArray(new IEditorReference[relevantEditors
.size()]), true);
Make sure that you have overriden the equals and hashCode of your EditorInput to check the equality of the wrapped MyObject-instance
When opening your editor you have to track the mapping between your items and the associated opened IEditorReference This can be done for example using a simple HashMap object.
thanks to Tom, your answer helps a lot.
As each IEditorInput could have its name that can be set, we also can use like followings:
// String str =.....
// str, could be an editor's property
if (iEditorReference.getEditorInput().getName().equals(str))
Besides, it shall throw PartInitException like this:
//....................
try {
for (IEditorReference iEditorReference : editorReferences) {
if (iEditorReference.getEditorInput().getName().equals(str)) {
relevantEditors.add(iEditorReference);
}
}
} catch (PartInitException e) {
e.printStackTrace();
}
//...................