0

Hello Please help me figure out the problem here. I've looked up it on the StackOverFlow I found some similar questions but not really helped me figure it out.

    if(result.isPresent() && result.get() == ButtonType.OK) {
        DialogController dialogController = new DialogController();
       dialogController.processResult(); //Line 90
        System.out.println("Ok was pressed");
    }
    else {
        System.out.println("Cancel was pressed");
    } 

this piece of code is from my main controller.java class and it refers to a dialog by pressing OK in the dialog it runs DialogController.proccessResult() which is supposed to call the proccessResult and print out the content entered in my dialog textArea and textField: SCREENSHOT OF MY DIALOG

    public class DialogController {

    @FXML
    private TextArea longDescription;
    @FXML
    private TextField shortDescription;
    
 public void processResult () {
        String ld = longDescription.getText().trim(); //Line 24
        String sd = shortDescription.getText().trim();
        System.Out.PrintLn (ld);
        System.Out.PrintLn (sd);

  }
}

but it throws a null pointer exception, what seems to be the problem? I'm new to coding though.

Caused by: java.lang.NullPointerException at untitled18/ir.sepich.todolist.DialogController.processResult(DialogController.java:24) at untitled18/ir.sepich.todolist.controller.showItemNewDialog(controller.java:90) ... 54 more

  • Just saying: if you are new to coding, then maybe starting with a JavaFX GUI is a very challenging approach. I would go for something smaller. And if you really want to start with JavaFX GUIs, then follow a tutorial. These frameworks are complex, you have to understand what you are doing, and what different elements need to come together in order for the overall thing to work. – GhostCat Jun 24 '21 at 11:58
  • 1
    And then, please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. As in: add the exception stack trace to your question please. So we understand **which** line throws the exception. – GhostCat Jun 24 '21 at 11:58
  • 1
    Where, in general, the one and only answer would be: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – GhostCat Jun 24 '21 at 11:59
  • @GhostCat Yeah I agree, actually this was part of an online course I've been taking part in... so I really wan to know what's going on here. – SyntaxTerrorist Jun 24 '21 at 12:00
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Joe Jun 24 '21 at 12:01
  • As said, step 1: stack trace. – GhostCat Jun 24 '21 at 12:02
  • Yes, stack trace needed, you never said _where_ the null pointer is happening. – Paul T. Jun 24 '21 at 12:08

1 Answers1

0

I might not be knowledgeable enough to answer your question, but here are some tips for someone who's had his own share of nullpointerexeptions:

Check what value is null:

public void processResult () {
    System.out.println(longDescription.getText().trim());
    System.out.println(shortDescription.getText().trim());
    System.out.println(datePicker.getValue());
}

Give a value to the items when you declare them (they can be empty like this:"")

public class DialogController {

@FXML
private TextArea longDescription;
longDescription=new TextArea("",5,40);
@FXML
private TextField shortDescription;
longDescription=new TextField("",20);
@FXML
private DatePicker datePicker;
datePicker.setValue(LOCAL_DATE("01-05-2016"));

The nullpointerexeption should be telling you at what row it is happening, so you might not need to do this for all the items in order to find out the origin of the problem.
However it is usually a bad idea to leave an item undeclared when you want the user to put a value in it.

Edit: even if you do not want to add an initial value I think you need to initialize the objects meaning:

public class DialogController {

@FXML
private TextArea longDescription;
longDescription=new TextArea();
@FXML
private TextField shortDescription;
longDescription=new TextField();
@FXML
private DatePicker datePicker;
datePicker=new DatePicker();
The Blind Hawk
  • 1,199
  • 8
  • 24
  • Thank you bro, You're comment helped me realize where the root of my problem is.. I now know that when I fill out my form in my dialog the entered, for some reason they are not associated with longDescription and shortDescription ... I hardcoded some values to my longDescription and shortDescription and they show on the console... but when u try to give them values through the GUI dialog I keep getting nullException.. – SyntaxTerrorist Jun 24 '21 at 14:07
  • Glad to know it helped :) please select it as the correct answer if it solved the problem – The Blind Hawk Jun 24 '21 at 14:27
  • @SyntaxTerrorist aside hardcoding some values into them do you at least initialize them? meaning: private TextArea longDescription=new TextArea(); This is something you should always do for objects if this is not the problems I have no clue. – The Blind Hawk Jun 24 '21 at 15:07
  • Have you tried declaring the objects at the start? Any news? – The Blind Hawk Jun 25 '21 at 12:06
  • Yeah I did ,no difference, the problem seems to be with the connection between my JavaFX UI and the variables in my DialogController, it seems like it's not colleting the user input to initialize my variable with them. – SyntaxTerrorist Jun 30 '21 at 05:43