javaeetutorial
  1. javaeetutorial
  2. JAVAEETUTORIAL-16

Bean Validation null string comparison code snippet would throw NPE

    Details

      Description

      "In this case, the user input for the field is not required.

      if (testString.equals(null)) {
      doSomething();
      } else {
      doAnotherThing();
      }

      By default, the doAnotherThing method is called even when the user enters no data, because the testStringelement has been initialized with the value of an empty string."

      This code is broken in that if testString actually were null, the code would throw a NullPointerException instead of correctly checking the comparison.

      It should be:
      if (testString == null) {
      doSomething();
      } else {
      doAnotherThing();
      }

        Activity

        Hide
        devikag added a comment -

        Code snippet modified as per user suggestion.

        The chapter(9) is updated and the modified content will be visible after next publication.

        Show
        devikag added a comment - Code snippet modified as per user suggestion. The chapter(9) is updated and the modified content will be visible after next publication.

          People

          • Assignee:
            devikag
            Reporter:
            Ian Evans
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: