Results 1 to 5 of 5
  1. #1
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default textfield and nullpointerexception

    Hey everyone :)

    I'm trying to send some form data to a servelt, but everytime i hit submit button I get a nullpointerexeption.

    If i check the checkbox only and then hit submit, then it works, but if I insert data to textfields and hit submit, then I get a nullpointer.

    How to fix this?

    Heres my code:

    jsp page:
    Java Code:
    <html>
    <head>
    <title>test</title>
    </head>
    <body>
    <table border="1">
                    <form action="SalaryFilter" method="post">
                    <tr>
                        <td>
                            Palk
                        </td>
                    </tr>
                    <tr>
                        <td>
                           <input type="checkbox" name="palk" />
                            Kõrgeimad palgad
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="text" name="minSal" /> kuni <input type="text" name="maxSal"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="submit" value="vaata" />
                        </td>
                    </tr>
                </form>
                </table>
    </body>
    </html>
    Servlet code:
    Java Code:
    package filter.filterServlets;
    
    import filter.model.Employee;
    import filter.sql.FilterManager;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    public class SalaryFilter extends HttpServlet {
    
    
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            doPost(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
            /*
             * Paremeters from EmployeeList.jsp
             */
            String checkboxState = request.getParameter("palk");
            String strMin = request.getParameter("minSal");
            String strMax = request.getParameter("maxSal");
            int min = 0;
            int max = 0;
            try{
                min = Integer.parseInt(strMin);
                max = Integer.parseInt(strMax);
            }catch(NumberFormatException e){
                e.printStackTrace();
            }
            /*
             * All filters
             */
            FilterManager filterManager = new FilterManager();
    
            /*
             * Filtered employee list
             */
            List<Employee> employeeList = null;
    
            if(checkboxState.equals("on")){
                employeeList = filterManager.getHighestSalary();
            }
            else{
                employeeList = filterManager.getSalaryMinMax(min, max);
            }
            request.setAttribute("list", employeeList);
            try{
                getServletConfig().getServletContext().
                        getRequestDispatcher("/EmployeeList.jsp").forward(request, response);
            }catch(ServletException e){
                System.err.println(e.getMessage());
            }catch(IOException e){
                System.err.println(e.getMessage());
            }
    
    
        }
    
    
    }

    error code:
    Java Code:
    java.lang.NullPointerException
    	filter.filterServlets.SalaryFilter.doPost(SalaryFilter.java:58)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default

    Have you located line 58? What variable used on that line is null?

  3. #3
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Have you located line 58? What variable used on that line is null?
    Thank you :)

    Found the problem

    fixed version:
    Java Code:
    if(checkboxState != null){
                employeeList = filterManager.getHighestSalary();
            }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    There you go.
    If you have a checkbox in a form, it will only be sent in the request if it is checked, otherwise (as you've seen) the getParameter() will return null.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. 1 question regarding textfield
    By henry in forum New To Java
    Replies: 3
    Last Post: 06-16-2010, 03:56 AM
  2. awt TextField nullPointerException error
    By k2k in forum AWT / Swing
    Replies: 3
    Last Post: 02-24-2009, 04:24 AM
  3. TextField Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:21 PM
  4. JSP - getting value from a textfield
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •