Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9220277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:18:36+00:00 2026-06-18T03:18:36+00:00

I am coding a web-app in Java-EE and find myself with a very unexpected

  • 0

I am coding a web-app in Java-EE and find myself with a very unexpected result when I am trying to display errors on user-input.

The app is built on the JSP/Servlet/Form/Bean model. Basically, the JSP stores data in the request, and transfers it to the servlet. Then the servlet transfers the request raw to the form, which then reads the data, performs the necessary checks and returns the bean to the servlet.

Most of the fields must have specific values, some others must simply be non-null.

I have written error detection code to secure inputs however I find myself with a very strange result:

  • when the field is non-null but the value is incorrect (say, an hour located outside the 00:00-23:59 range), it does return the proper error, along with the error message, stored in a HashMap, and I can access it in my JSP.
  • However, when the field is null, it returns the message, probably stores it in the HashMap as well (I know this because the ${!empty errors.dataErrors} test returns true and the error field is displayed in my JSP) but there’s no way to access the values of the errors

I have searched through my code but still can’t find where the error comes from. Here are snippets of it if someone knows where the problem comes from

doPost method from the servlet:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    NewBookingForm form = new NewBookingForm();
    Booking booking = form.registerBooking(request);
    String VUE;

    request.setAttribute("booking", booking);
    request.setAttribute("errors", form);

    this.getServletContext().getRequestDispatcher(VIEW).forward(request, response);
}

the map is a field in the NewBookingForm class, declared and initialized outside the registerBooking method like this private Map<String,String> dataErrors = new HashMap<String,String>(); and it has a private setter (for access within the class) and a public getter (for access in the Servlet and in the JSP)

inside the form class, I use this function to get the field values:

private static String getFieldValue(HttpServletRequest request, String fieldName)
{
    String value = request.getParameter(fieldName);
    if (value == null || value.trim().length() == 0){return null;}
    else{return value;}
}

After getting the values with a series of calls like String fieldDepartureStation = getFieldValue(request, FIELD_DEPARTURE_STATION); at the beginning of my method, I then check them using try/catch blocks like this

try
{validation.departureStation(fieldDepartureStation);}
catch(Exception e)
{setDataErrors(FIELD_DEPARTURE_STATION, e.getMessage());}

The validations method within the validation class are a bit different if the data must have specific value-ranges or must simply be non-null.

In the former case, they are something like this:

public void departureTime(String time) throws Exception
{
    if (!validationRETime(time)) { throw new Exception("Please input a time with the hh:mm pattern"); }
}
....
private boolean validationRETime(String strTime)
{
    String regExp = "^([01][0-9]|2[0-3])[:][0-5][0-9]$"; // hh:mm
    if (strTime.matches(regExp))
    {
        return true;
    }
    else
    {
        return false;
    }
}

whereas in the latter case they are simply

public void departureStation(String station) throws Exception
{
    if (station.equals(null)) { throw new Exception("Please input a departure station"); }
}

Finally, in my JSP, I use the following code to display errors:

<c:if test="${!empty errors.dataErrors}">
     <p>Errors</p>
     <c:forEach items="${errors.dataErrors}" var="message">
           <p><c:out value="${message.value}" /></p>
     </c:forEach>
</c:if>

And it does display the Error paragraph when I purposedly enter incorrect values, but the <c:forEach> is only looping and displaying the error messages when the wrong field is non-null but with an incorrect value. Thus with a field that only needs to be non-null, I never get the message (but I do get the error)

These are all the things I could think of that could possibly go wrong, but I have yet to discover where they did and if someone could help me, I’d be very glad.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:18:37+00:00Added an answer on June 18, 2026 at 3:18 am

    The problem is in your departureStation method: –

    public void departureStation(String station) throws Exception
    {
        if (station.equals(null)) { 
            throw new Exception("Please input a departure station"); 
        }
    }
    

    Your test for null value is itself triggering a NPE. So, as soon as station.equals(null) is executed for station = null, a NPE exception is thrown, which is then propagated to the caller. So, your if block will not even be executed. And hence you are not throwing the Exception as you might be thinking.

    Now, also note that, the NPE that is thrown does not contain any message. So, e.getMessage() will return null on it.

    Now, let’s move back to the caller: –

    try
    {validation.departureStation(fieldDepartureStation);}
    catch(Exception e)
    {setDataErrors(FIELD_DEPARTURE_STATION, e.getMessage());}
    

    Here you are doing the biggest Crime in the world of Exception Handling, by using a catch block for Exception. Since Exception is the super class of all the exceptions, it will handle all the exceptions in the same way. So, it consumes the NPE, and passes it to setDataErrors().

    So, you will of course get the errors, but, the value e.getMessage() will be null. And that is why you are not seeing any message. You can even test it by logging the value of e.getMessage() in the catch block above.


    Solution ??

    Just change your null check with this one: –

    if (station == null) { 
        throw new Exception("Please input a departure station"); 
    }
    

    And everything will be ok. I think, you will have to do this change in all of your methods. Always perform the null check using == operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m new to web development but have coding experience (Java) and I’m trying to
Very basic question: I am coding a web app that has a handful of
I'm coding a web app that uses a picture that the user can upload
Started coming up with a java web app for online user interaction. Decided to
I'm working on a web app that can allow the user to input some
I am coding a web portal which stores a lot user data and later
I use Eclipse Java EE IDE for Web Developers for most of my coding,
I am currently trying to stream content out to the web after a trans-coding
I have tomcat 7.0.14, jdk1.6 and Eclipse Indigo for Java EE web app developers.
I am coding a classifieds ad web app. What is the optimal way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.