• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException when trying to getText() from Textfield  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having issues with my code, I have been able to pass the contents of an array list from one class to another, however whenever I decide to use CreateUsername.getText() or simply getText(), I get an NullPointerException. I have no idea why, I've tried using another if statement to say if the program couldn't handle another one, however I was able to loop through the number of users and print to the console with another test if statement.

The comments will provide further information

This is the class in which the array list has been past to.



Just in case if needed I have also included the class with the array list



I believe my FXML isn't causing the issue. So I haven't included this in the post.



 
Rancher
Posts: 3053
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I get an NullPointerException


Please copy the full text of the error message and paste it here. It has important info about the error.

Where is the variable: CreateUsername given a value?
 
Marshal
Posts: 59081
180
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you are getting the exception at line 75, presumably the text field object has actually been created by that point. I can't see that in the code. If the text field reference points to null, you will get that sort of exception. I would have thought the FXML would have created the object, but I am not sure.
Also: if you haven't entered any text, does getText() return the empty String or null?
Start by printing out the text field, and make sure this prints true:- System.out.printf("Text field: %b%n", myTextField); Line 74½. Look up the %b tag here for more information.

Don't use date format and the date class. Use the newer classes instead.
Don't start package names or variable names with CapitalLetters. Why are you calling the text fields CreateXXX and the labels emptyXXX?
Don't try to test whether a String is equal to "". Use its isEmpty() method instead.
Don't catch a Throwable. In fact I don't think you should catch any exceptions there at all. If you suffer an unchecked exception, let it propagate and prevent it happening again.
Remove the long comments; they don't tell us anything we can't tell from the code anyway. They also make your lines too long to read. You should also break the lines in the second code block into shorter parts for the same reason. I tried to find the old Sun style guide because it tells you how to break lines, but the link doesn't seem to work. Maybe it will reappear later.
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the full error



I've added the code you've provided

The console printed

I don't think CreateUsername is given a value in the code, but in the GUI. When the user enters a username, the string UserName is given CreateUsername.GetText();

To explain the CamalCase I've just learned that Java has a different code formatting rules, I haven't been told about them until recently haha. 


 
Norm Radder
Rancher
Posts: 3053
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't think CreateUsername is given a value in the code, but in the GUI.


CreateUsername is a variable whose value should be a reference to a TextField object.  If it has a null value then trying to call any methods with it will cause the NPE.
Have you tried to print out its value to see if it is null or not?

java.lang.NullPointerException
    at BunnyCorp.Controllers.AdminUserController.arrayTest(AdminUserController.java:83)


What variable on line 83 has a null value?  Add a print statement just before line 83 that prints the values of the variables used on line 83.
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay few changes.

I just realised since I've isolated the code more, some of the line numbers are different.

Line 85 is Line 75.

That being said, I noticed that I placed System.out.printf("Text field: %b%n", CreateUsername); in the wrong area.

Now it says false and not true.

CreateUsername seems to be printing null.

However when I put System.out.printf("Text field: %b%n", CreateUsername); into createUser(); which is the button, it returns true.



 
Norm Radder
Rancher
Posts: 3053
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If CreateUsername is null when it is used to call a method then you will get a NPE.  Where is CreateUsername given a value?

Note: createUsername is normally the type of name given to a method: a verb that describes what the method does.  It is a not a natural name for an input field.
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I've given CreateUsername a value.

The code I've left out is the part I want to implement the test into and in it it takes the text from CreateUsername and gives it to the string UserName.

I would provide but, I'm on my smartphone and about to rest.
 
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not realy familair with a full fxml program, I only used it in springBoot, but I don't tink you suposed to make a instance of your controller class.
In your mainclass you use the fxmlLoader to load your fxmlfile and there you call your controller.

 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I'm back.

Here is the code which CreateUsername.getText() sends to userName

 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We still can't see where you asign a value to createUserName and I think you have an infinite loop to.
You set i to 0 and say while i is smaller then the lenght myUsers.toArray, but at no point you increment i.
 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post the full code cause your codesnippets don't make scence
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I have set a value. The user inputs the username into the text field.
 
Norm Radder
Rancher
Posts: 3053
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't think I have set a value


There are two very different values we are talking about.
One is the contents of a variable: CreateUsername that should have a reference to an instance of a TextField
The other is the contents of the TextField.

The first one will cause a NPE if the variable does not have the reference to anything.
The second one will not cause an exception if the user has not entered any data into the TextField.

Where in the code does the variable: CreateUsername get a value?
 
Sheriff
Posts: 4865
136
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually in a controller in JavaFX, you have your GUI fields marked like this:
Otherwise JavaFX doesn't know to inject a value from your FXML file.  Could you show the class(es) that load the FXML files and launch the controller?
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my full code for clarification, I believe I haven't added a value. I'm not too sure how, If it's "public TextField CreateUsername = New TextField();" I've noticed that "works" but it doesn't get text or set labels to visible 

 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:
Usually in a controller in JavaFX, you have your GUI fields marked like this:
Otherwise JavaFX doesn't know to inject a value from your FXML file.  Could you show the class(es) that load the FXML files and launch the controller?



If they are public, you don't need the @FXML annotation, only if they marked private
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This loads the FXML

 
Norm Radder
Rancher
Posts: 3053
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

  it doesn't get text or set labels to visible 


That is probably because there are TWO instances of the component:
one is shown in the GUI but NOT referenced by the variable
the other is NOT shown in the GUI but is referenced by the variable.

The code needs to be fixed so that the variable references the component that is shown in the GUI.
 
Knute Snortum
Sheriff
Posts: 4865
136
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:

Knute Snortum wrote:
Usually in a controller in JavaFX, you have your GUI fields marked like this:
Otherwise JavaFX doesn't know to inject a value from your FXML file.  Could you show the class(es) that load the FXML files and launch the controller?



If they are public, you don't need the @FXML annotation, only if they marked private


So the FXMLLoader knows to inject the values into the fields if they're just public?  Are you sure?
 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at line 71 you create a usserClass, which calls a method that makes an instance of your controller, I still don't think your supposed to do that.
When your application starts it loads your fxml which is connected to your controller, the value for createUser should be asigned in your fxml by the user and call a method in your controllerclass where it gets used
 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure, I always use the annotation, but when i forget, my idea warns me that my fields should be public or marked with @FXML, so I supose
 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should know to inject from your fxml since you specify a controller in your fxml.
 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're right, even when marked public if  used in a fxmlfile they should be annotated
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the FXML code



I think they reference each other, the code worked before, it stopped working when I began to pass the array list from another class to the method.

Also userClass is needed otherwise it won't be able to pass the array list.

 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not saying you shouldn't use userclass, I'm saying you shouldn't make an instance of your controllerclass
 
Knute Snortum
Sheriff
Posts: 4865
136
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess we'll need to see the FXML for the AdminMenu, or if you use SceneBuilder, check that the CreateUserName TextField is tied to the field in AdminUserController.
SceneBuilderControllerField.PNG
[Thumbnail for SceneBuilderControllerField.PNG]
 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shoudln't his controller not be annotated with @Component or @Controller, or is this also only in spring boot
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Restarted my PC cause I'm an Intel User and scared of the bug and windows released an update.

Last time, I checked on SceneBuilder.

The TextField fx:id is CreateUsername

 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh and it's tied to the controller also
 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is, line 117 of your fxmlfile
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignore the fact I put Here's the AdminMenu FXML in the code section
 
Daniel Demesmaecker
Rancher
Posts: 279
6
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you use buttons for a menu and not a menubar?
I would start with annotating your controllerclass with @Component and your fxmlvariables with @FXML, remove the line where you making a instance of the controller and try again
Otherwis put your project on github and I'll try to rebuild it
 
Norm Radder
Rancher
Posts: 3053
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the code worked before, it stopped working when ...


Can you go back to the working code and work forward from there one small step at a time, stopping when the code stops working?
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It pretty much stops working when I pass an Array-List into the method
 
Norm Radder
Rancher
Posts: 3053
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it work if you remove the statement(s) that pass the ArrayList?

The objective now is to restore the code that works and then add back the code that was removed in very small steps to find the code that causes the problem.


pass an Array-List into the method


What is the name of the ArrayList?
what is the name of the method?
 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code works if I add the array list in the same class, and remove (ArrayList < Users > myUsers) from the method however, my program needs to be able to access the array-list in other classes, so I created another class that creates a predefined list of users in an array as required.

Even without the loop, the issue is being caused by (ArrayList <Users> myUsers).






 
Alec Goodsir
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the ArrayList is called myUsers and the method is arrayTest
 
Norm Radder
Rancher
Posts: 3053
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, do you now have a program that works again?
Was the NPE happening in the code that was removed?
 
You get good luck from rubbing the belly of a tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!