last day (26 days later) » 

7:43 PM
0
Q: I am unable to use .getLength(), and program not compiling

FoonbeeI am trying to create a simple UI that gets a users first and last name and then counts how many characters are in the name. I am receiving errors: Exception in Application start method java.lang.reflect.InvocationTargetException Caused by: java.lang.RuntimeException: Exception in Application ...

 
"Location is required". That means getClass().getResource("com/company/test_1.fxml") is returning null, which in turn means the resource doesn't exist at the path given. As mentioned by the documentation, a path which does not start with / is a relative path which is being resolved to /com/company/com/company/test_1.fxml. In other words, it's resolved against the class' package. Use an absolute path or use the correct relative path.
Also, as mentioned by @P.Soutzikevich, the title of your question does not match the content of your question at all. Not only do you not have a call to getLength() in your code, but you're getting a runtime error—which means your program compiled successfully.
 
I just realized i had removed the .getLength() call from "calcName.setOnAction(e -> fName.getText());" because i wanted to get the runtime error as you called. I had initally had calcName.setOnAction(e -> fName.getText().getLength()); in order to count the number of characters in the name and save it in a variable.
Also I do not know how to fix the location error, when i go into main and try to replace the "Parent root = FXMLLoader.load(getClass().getResource("com/company/test_1.f‌​xml"));" with the relative location the program gives me its word for word com/company/test_1.fxml
 
Another problem is you're instantiating the fields annotated with @FXML manually—never do this. The FXMLLoader is responsible for injecting the instances created when parsing the FXML file. Your code replaces those injected objects with your own objects which are not the ones connected to the displayed scene graph. Of course, your <TextField/> elements first need to have the appropriate fx:id attributes in order for the injection to work.
Assuming your test_1.fxml file is in the com.company package you should either use "test_1.fxml" or "/com/company/test_1.fxml" as the path (notice the leading / in the second path). If that doesn't work please edit your question to show your project structure. Are you using a build tool (e.g. Maven, Gradle)?
 
I don't know what a build tool is, and i tried adding the leading / and its still giving me an error
I've add all the changes I've done since posting the question
 
@Foonbee Couple things: (1) You've added the fx:id attributes but you're still manually instantiating the fields in your controller class; (2) Can you post your project structure as well as which IDE you're using?
 
7:50 PM
i am using intellij, and by post my project structure do you mean post a zip of the file?
and when i remove the @FXML on the controller i see errors for my objects
 
No, I mean post the directory structure of your project. And don't remove the @FXML annotation, remove the instantiation statements (e.g. fName = new TextField()). As I mentioned in a previous comment, the FXMLLoader is responsible for injecting those fields (via reflection); if you assign your own objects to those fields you will lose the reference to the objects attached to the displayed scene graph.
 
C:\Users\foonb\IdeaProjects\Oluwafunbi_Balogun_TEST_1\src\com\company\test_1.fxml
is this what you're asking for
and now i understand
 
Yes, that's what I meant. Is your src directory marked as a "resources root" (see this help page)?
 
i dont know what that means, also my other programs work fine with the same file location, i dont know if that helps at all
 
8:10 PM
I considered you might not know what a resources root is, which is why I linked to an official IntelliJ IDEA help page. The reason I ask is because it's possible your FXML file is not being copied to the build output and thus not ending up on the classpath; if your src directory is marked as a resources root, however, then that being the cause of your problem becomes less likely.
 
okay i figured it out, i had initially created the fxml at the wrong location and when i moved it to the proper location i dont think its location changed properly, i recreated a brand new fxml file and copied and pasted everything onto it and now it works properly
thank so much
 
Glad you figured it out :) Note if you feel an answer would help others in the future then you should answer your own question, otherwise consider deleting the question.
 
okay i will delete it cause i initially titled it wrong
i have another question
when use the button to add the lengths together its not recognizing the semicolons
int num;

calcName.setOnAction(e ->

num = fName.getLength() + lName.getLength();

nameNum.setText("Number of characters in your name is: " + num));
 
Note that local variables must be final or effectively final when used inside a lambda expression or an anonymous class. You also appear to want your lambda to do more than execute a single statement, which means you'll need to use a block: calcName.setOnAction(e -> { /* action code here */ });.
 
8:25 PM
okay i changed it to a block
int num = 0;

calcName.setOnAction(e ->{

num = fName.getLength() + lName.getLength();

nameNum.setText("Number of characters in your name is: " + num);
});
i thought u cant change the value of final variables
okay i figured out you can just make a method to pass the variable
thank you
 
You're correct, you cannot reassign final variables. This logically means a local variable declared outside a lambda expression cannot ever be reassigned if said local variable is to be used inside a lambda expression (this question and the linked duplicates provide more information). However, if you declare the local variable inside the lambda then the mentioned restrictions don't apply.
 

last day (26 days later) »