-1

I am designing a staff data manager which will allow the users to add, edit and view the staff details.

In the code I have a JFormattedTextField defined as follows.

 SimpleDateFormat dateDOB = new SimpleDateFormat("dd/MM/yyyy");
 JFormattedTextField DOBBX = new JFormattedTextField(dateDOB);

When I want to edit the staff data, I load the date to the JFormattedTextField as follows:

DOBBX.setText(""+retrievedStaff.getDOB());

After editing if I save the data, the value for DOB is null and I get a java.lang.NullPointerException. This only happens if I leave the value for DOB unedited. If I make changes to DOB or enter it again then DOB value is correctly saved.

Also if I add a new staff and then try to edit the staff data without closing the program, the date is properly saved to the binary file with no nullpointer error.

How do I fix this?

(I have no idea how to provide an MCVE for this as my code is quite long and consists of many classes)

Before saving the date to binary file I use the following method to convert it to a string:

public String getDOB() {
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    try{
    String textDate = df.format((DOB));
    return textDate;
    }catch(Exception e){e.printStackTrace();}
    return null;

}

In the StackTrace it is mentioned that the error is produced by this part of the code:

String textDate = df.format((DOB));
Charlie
  • 1,780
  • 2
  • 25
  • 44
  • You need to clarify what you mean by "After editing if I save the data", what does editing means exactly in this case? And I mean the complete interaction. Are you just putting a value, are you also tabbing away, are you pressing enter, or do you just enter the text and try to "getValue" ? – Gnoupi Apr 03 '15 at 15:35
  • 2
    `"I have no idea how to provide an MCVE for this as my code is quite long and consists of many classes"` -- Time to debug and then refactor so that your code is MCVE-able (so that you reduce cyclomatic complexity). Note that your variable names should begin with lower case letters to conform to Java naming conventions. Also, you should set your JFormattedTextField with `setValue(...)` passing in a date, not `setText(...)` – Hovercraft Full Of Eels Apr 03 '15 at 15:35
  • I am using a Save button with an action listener and I am reading it as Date using `getValue` – Charlie Apr 03 '15 at 15:40
  • I am using `setText()`because I am converting the date into a string when I write the date to binary file – Charlie Apr 03 '15 at 15:41
  • It's still a bit unclear. Like @HovercraftFullOfEels said, you should probably use setValue. setText comes from the JTextComponent parent, and it might not trigger the appropriate events. Other than that, to make sure: you only press a button after entering text, right? You don't press enter? Does it work if you enter text, then press enter, then click the button? – Gnoupi Apr 03 '15 at 15:51
  • Do you know how to debug most NullPointerExceptions? I would expect that if you did, you would have already posted the stacktrace with your question and would have indicated which line(s) of code throws the exception. Again, please improve this question. – Hovercraft Full Of Eels Apr 03 '15 at 15:53
  • @HovercraftFullOfEels - to be fair, I expect his NPE to come from his use of the value afterwards, and the actual question is why is it null when he entered text. But I might be wrong. – Gnoupi Apr 03 '15 at 15:54
  • 1
    @Gnoupi: who knows until he shows us. I would like to see for myself. All we're getting are little code snippets that we can't test and broad descriptions that are hard to understand. – Hovercraft Full Of Eels Apr 03 '15 at 15:55
  • I have added the code which is causing the error – Charlie Apr 03 '15 at 16:18
  • So now you and we know that DOB is null at the time that `df.format((DOB))` is called, and your job is to trace back into your code to see why. This is looking like any and all NullPointerException type questions. My suggestion: rename DOB to dob, **extract the value from the JFormattedTextField into dob inside of your `getDOB()` method**, and then use that value. – Hovercraft Full Of Eels Apr 03 '15 at 16:24
  • I think you have to show us a little more code in order to understand. You can also try a 'System.out.println(DOB.toString())' before try to format it, to understand what is in there. – AndreaTaroni86 Apr 03 '15 at 16:25
  • @AndreaTaroni86: it looks like `null` is in there. – Hovercraft Full Of Eels Apr 03 '15 at 16:35
  • @HovercraftFullOfEels There is no null error if add a new staff member save their data to a binary file and then try to edit the data without closing the program. This only happens if I restart the program and then try to edit the data of an existing staff member – Charlie Apr 03 '15 at 16:36
  • @AndreaTaroni86 I have added the code for edit, view and save button – Charlie Apr 03 '15 at 16:45
  • @HovercraftFullOfEels I just noticed even if I just place the cursor inside the JFormattedTextField without editing the date, it works. It doesnt work only if I leave the DOB field untouched during editing – Charlie Apr 03 '15 at 16:53
  • 1
    Unfortunately, it is unclear still what is happening, because we don't have the full cinematic. I would suggest however that you follow the advice from @HovercraftFullOfEels, and I would add to that that you should check exactly what comes out of your formatted field. My guess, given the little I know about the situation, is that the text you entered is not committed. Committing happens when you press enter, ou when you call yourself commitEdit(). Until the change is committed, the field does not have a valid value, hence the null, probably. Then again, I lack details, so this is a wild guess – Gnoupi Apr 03 '15 at 18:06
  • Setting the value of the JFormattedTextField via `setValue(...)` may also work. – Hovercraft Full Of Eels Apr 03 '15 at 19:11
  • @Gnoupi I am saving the edited data by clicking the Save button and not by using enter key...Also can you tell me what is still unclear about the question – Charlie Apr 04 '15 at 02:42
  • @HovercraftFullOfEels `setValue` will not work since I convert the date to string in the `getMethod`..So when I retrieve the data into the fields, `date` can only be retrieved as a string. – Charlie Apr 04 '15 at 02:44

1 Answers1

0

I solved this problem. So as I told in the question that the date is properly saved to binary file if I click or change the DOB textbox. So I set the cursor to DOB textfield when the user clicks edit button using requestFocus().

Charlie
  • 1,780
  • 2
  • 25
  • 44