-4

Controller.java

Map<String, String[]> formData  = request().body().asFormUrlEncoded();

    if(formData.get("firstName") != null) {
        JOptionPane.showMessageDialog(null, "not null");

    }

When I click the save button after I run the program, it always returns an error message of NullPointerException even if I put a value in the firstName textbox.

Bibek Shakya
  • 1,233
  • 2
  • 23
  • 45
no_one
  • 1
  • 4
  • 3
    I read your question as "Here is some random code that may or may not be related to the error message that I'm not going to give you." – John3136 Aug 22 '16 at 03:04
  • may be you need to read this http://stackoverflow.com/questions/271526/avoiding-null-statements – Bibek Shakya Aug 22 '16 at 03:07
  • The code above is my code in trying to get the value of a textbox via the map. The error I get everytime is NullPointerException. – no_one Aug 22 '16 at 03:09
  • Wherever you read `NullPointerException`, there would have been a mention of exact line number too, where this error is occurring. What is it? – Raman Sahasi Aug 22 '16 at 03:46
  • @rD. It points at this line - if(formData.get("firstName") != null) { – no_one Aug 22 '16 at 03:56

2 Answers2

1

There is some good information in answers to this question on how to detect and resolve null pointer exceptions. In your case it is likely that on of your object references is null. Check if request() or request().body() or formData is null. An easy way to do this is to add assert statements before you use the reference. It's also possible that the error is unrelated to the code you have included in your question.

To pinpoint the location of the error, use the stacktrace or have your debugger take you to the line of code that causes the exception.

Community
  • 1
  • 1
sprinter
  • 27,148
  • 6
  • 47
  • 78
0

It's important that to validate for

  1. Whether your map is null or not
  2. Whether your map contains "firstName" key
  3. Whether the value for "firstName" is empty or not.

Please note that these validations should be in exact order as mentioned above.

You can change your code to:

Map<String, String[]> formData  = request().body().asFormUrlEncoded();

if(formData!= null && formData.containsKey("firstName") && !formData.get("firstName").equals("")) {
        JOptionPane.showMessageDialog(null, "not null");
}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71