java.lang.NumberFormatException:doPost 中的空错误

Posted

技术标签:

【中文标题】java.lang.NumberFormatException:doPost 中的空错误【英文标题】:java.lang.NumberFormatException: null error in doPost 【发布时间】:2021-12-13 23:32:02 【问题描述】:

我正在创建一个包含疫苗列表的网页。当我单击链接以编辑列表中的特定疫苗条目时,这就是用户被带到的网页。该条目的 id 参数被传递到此页面。单击保存按钮调用 doPost。当我尝试将 id 参数从 doGet 传递到新的疫苗条目对象时,id 的值变为 null。

这是疫苗列表的 servlet 代码:

package vaccineList.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import NewVaccine.servlet.NewVaccine;
import NewDose.servlet.NewDose;

//import cs3220.model.GuestBookEntry;
//import cs3220.utilities.*;

@WebServlet(urlPatterns = "/ListVaccine", loadOnStartup = 1)
public class ListVaccine extends HttpServlet 

    private static final long serialVersionUID = 1L;

    public ListVaccine()
    
        super();
    

    public void init( ServletConfig config ) throws ServletException
    
        super.init( config );

        List<VaccineEntry> entries = new ArrayList<VaccineEntry>();
        entries.add( new VaccineEntry( "Pfizer/BioNTech", "2", "21", "10000", "10000" ) );
        entries.add( new VaccineEntry( "Johnson&Johnson", "1", "0", "5000", "5000"  ) );

        getServletContext().setAttribute( ServletFunctions.VaccineEntries, entries );
    

    @SuppressWarnings("unchecked")
    protected void doGet( HttpServletRequest request,
        HttpServletResponse response ) throws ServletException, IOException
    
        StringBuilder html = new StringBuilder();
        List<VaccineEntry> entries = ServletFunctions.getVaccineEntriesFromServletContext(getServletContext());

        response.setContentType( "text/html" );
        PrintWriter out = response.getWriter();

        html.append( "<p>List Vaccine</p>" );
        
        html.append( "<p><a href='NewVaccine'>New Vaccine</a> |" );
        html.append( "<a href='NewDose'> New Dose</a></p>" );
        
        html.append("<table border='1'>");
        
        html.append("<tr>");
        html.append( "<td>   " + " <b>Vaccine</b>   </td>" );
        html.append( "<td>" + " <b>Doses Required</b></td>" );
        html.append( "<td>" + " <b>Days Between Doses</b></td>" );
        html.append( "<td>" + " <b>Total Doses Recieved</b></td>" );
        html.append( "<td>" + " <b>Total Doses Left</b></td>" );
        html.append("<td></td>" );
        html.append( "</tr>" );
        
        for( VaccineEntry entry : entries )
        
            
            html.append("<tr>");
            html.append( "<td>" + entry.getName() + "</td>" );
            html.append( "<td>" + entry.getDosesReq() + "</td>" );
            if(entry.getDaysBtwnDoses() == "0") 
                html.append( "<td></td>" );
            
            else
                html.append( "<td>" + entry.getDaysBtwnDoses() + "</td>" );
            
            
            html.append( "<td>" + entry.getTotalDosesRecieved() + "</td>" );
            html.append( "<td>" + entry.getTotalDosesLeft() + "</td>" );
            
            
            html.append( "<td><a href='editVaccine?id=" + entry.getId() + "'>Edit</a></td>"  );
            
            
            html.append( "</td>" );
            html.append( "</tr>" );
        
        html.append( "</table>" );
        
        
        
        out.println( ServletFunctions.titleWithBody("ListVaccine", html.toString()) );
    


这里是 editVaccine 页面的代码:

package EditVaccine.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import vaccineList.servlet.*;


@WebServlet("/editVaccine")
public class editVaccine extends HttpServlet 
    private static final long serialVersionUID = 1L;
       
    
    public editVaccine() 
        super();
        
    
    
    @SuppressWarnings("unchecked")
    private VaccineEntry getEntry( int id )
    
        List<VaccineEntry> entries = (List<VaccineEntry>) getServletContext()
            .getAttribute( "entries" );
        System.out.println("entry taken is " + id);
        
        
        for( VaccineEntry entry : entries )
            if( entry.getId() == id ) return entry;
        return null;
    

    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        
        StringBuilder html = new StringBuilder();
        
        String id = request.getParameter( "id" );
        VaccineEntry entry = getEntry(Integer.parseInt(id));
        System.out.println("The value of id is " + id);
        
        
        System.out.println("The value of getEntry is " + entry.getId());

        response.setContentType( "text/html" );
        PrintWriter out = response.getWriter();
        
        html.append("<form action='editVaccine' method='post'>" );
        html.append("<table border='1' cellpadding='2'>");
        html.append("<tr>" + "<th colspan='2'>Name</th>");
        html.append("<td><input type='text' name='name' value=" + entry.getName() + "></input></td>" + "</tr>");
        html.append("<tr><th colspan='2'>Doses Required</th>" + "<td><select name='DosesReq' value='" + entry.getDosesReq() + "'>"); // id='Doses Required'
        html.append("<option value='1' name='dosage' value='" + entry.getDosesReq() + "'>1</option>");
        html.append("<option value='2' name='dosage' value='" + entry.getDosesReq() + "'>2</option>");
        html.append("<option value='3' name='dosage' value='" + entry.getDosesReq() + "'>3</option>");
        html.append("</select></td>");
        html.append("<tr><th colspan='2'>Days Between Doses</th><td><input type='text' name= 'DaysBtwnDoses' value='" + entry.getDaysBtwnDoses() + "'> </input></td></tr>");
        html.append("<td colspan = '2'><button>Save</button></td>");
        html.append("</form>");
        html.append("</table>");
        
        
        out.println( ServletFunctions.titleWithBody("ListVaccine", html.toString()) );
        
       
        
    

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        System.out.println("In post method");
        
        VaccineEntry entry = getEntry(Integer.parseInt(request.getParameter("id")));
        
        entry.setName(request.getParameter("name"));
        String name = entry.getName();
        System.out.println("name is " + name);
        entry.setDosesReq(request.getParameter("dosage"));
        String dosage = entry.getDosesReq();
        System.out.println("dosage is " + dosage);
        entry.setDaysBtwnDoses(request.getParameter("DaysBtwnDoses"));
        String DaysBtwnDoses = entry.getDaysBtwnDoses();
        System.out.println("DaysBtwnDoses is " + DaysBtwnDoses);
        
        response.sendRedirect("ListVaccine");
        return;
        
    


【问题讨论】:

对不起,这是我第一次发布关于堆栈溢出的问题。我不知道如何解决这个问题。 好的,谢谢! 【参考方案1】:

您必须在请求中传递id 参数。在这种情况下,您可以将其添加为 hidden 表单元素,即:

html.append("<input type='hidden' value='" + id + "' name='id' id='id' />")

【讨论】:

非常感谢!虽然现在我看到我在将选项值列表中所需的剂量值传递给 editVaccine 页面上的 doPost 方法时也遇到了问题。我该怎么做? select 元素名称和 id 属性的名称必须与您在 request.getParameter 中引用的参数名称相同(在您的情况下是 dosage)。跨度>

以上是关于java.lang.NumberFormatException:doPost 中的空错误的主要内容,如果未能解决你的问题,请参考以下文章