• Post Reply Bookmark Topic Watch Topic
  • New Topic

Could not read text from textfield to Java

 
Jeyaganeshan Jeyatharsini
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create the FXML file with the following code:
<TextField id="username" prefHeight="49.0" prefWidth="332.0" GridPane.columnIndex="1" />

I tried to get the text user enters as follows:
public class LoginPage implements Initializable{
@FXML
private TextField username;

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
username = new TextField();
}

@FXML
private void handleButtonAction(ActionEvent event) {
try
{
System.out.println("?" + username.getText() + "?");
}
catch(NullPointerException e)
{
System.out.println("Errors");
}
}
}

It gives empty string.. Please help me..
 
John Damien Smith
Ranch Hand
Posts: 321
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never create a new node when you already have an FXML injected one.

Remove this line from your code:

 
Jeyaganeshan Jeyatharsini
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same problem here... If I don't initialize it says NULLPointerException
 
John Damien Smith
Ranch Hand
Posts: 321
19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same additional problem as your other post, you need to use fx:id rather than id in your FXML markup if you want to inject @FXML defined node references into your controller. See the answer on your other question for more information:
http://www.coderanch.com/t/638300/JavaFX/java/customizable-pane-Java-code

 
Jeyaganeshan Jeyatharsini
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks... It is the solution for my problem!
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!