-1

I have my controller class as:

public class Empty_pageController implements Initializable,Runnable{

Which declares all IDs as:

@FXML 
private TextField IDno;
@FXML 
private TextField branch;
...

Now these is my clear() function to clear the FXML form:

public void clear(ActionEvent Event){
    IDno.clear();
    branch.clear();
    bookname.clear();
    ID.clear();
    author.clear();
    address.clear();
    name.clear();
    year.clear();
}

Then I have another class (in same package) that calls these clear():

new Empty_pageController().clear(null);

These gives me following error:

Exception in thread "Thread-4" java.lang.NullPointerException
at library.Empty_pageController.clear(Empty_pageController.java:147)
at library.server.doInBackground(server.java:45)
at library.Empty_pageController.run(Empty_pageController.java:84)
at java.lang.Thread.run(Thread.java:748)

What I tried after seeing some solutions here: I passed the object of contructor class by following code:

s.doInBackground(this);

and then made changes accordingly in the other class's method:

public void doInBackground(Empty_pageController E) throws InterruptedException {

Then I Called using these statement:

E.clear(null);

I got the same error Don't Know where I'm going wrong....?

EDIT:MY FXML file Empty_page.fxml

KANE
  • 63
  • 1
  • 1
  • 11
  • Can you add to your question where you're trying to use: `new Empty_pageController().clear(null);` – Sam Orozco Sep 07 '18 at 17:51
  • In doInBackground() – KANE Sep 07 '18 at 18:02
  • What action are you using to try to clear the text fields? Is it a button? Can you put your FXML file in the question? – Sam Orozco Sep 07 '18 at 18:02
  • Yes it is a button....and Its working...But I need to carry out a function (for Eg. get Input from a scanner) as soon as my FXML is loaded. Somehow I created a Thread and it gives me the resultant String which is just stored. I need to put this result into my Textfields without the click event. Is it possible? – KANE Sep 07 '18 at 18:10
  • Can you put the FXML file in the questions and not in a link? – Sam Orozco Sep 07 '18 at 18:17
  • Can you also show here you're getting that string input? – Sam Orozco Sep 07 '18 at 18:18
  • Why don't you get the string input from the JavaFX app? – Sam Orozco Sep 07 '18 at 18:18
  • I am taking Input from my android app using Socket – KANE Sep 07 '18 at 18:23
  • `ss=new ServerSocket(5000); while(status) { s=ss.accept(); isr=new InputStreamReader(s.getInputStream()); br=new BufferedReader(isr); message=br.readLine(); E.clear(null); }` – KANE Sep 07 '18 at 18:24

1 Answers1

1

So when JavaFX initially initializes your Controller Empty_pageController. It will inject

@FXML 
private TextField IDno;
@FXML 
private TextField branch;

base on that elements fx:id and your FXML tag.

If you construct that Controller yourself those components aren't being constructed and that's why you're seeing the NullPointerException.

If you want to clear the components you need to use the instance of your controller that JavaFX created.

Sam Orozco
  • 1,258
  • 1
  • 13
  • 27