我收到错误java.lang.NullPointerException

我收到错误java.lang.NullPointerException,java,html,servlets,dao,Java,Html,Servlets,Dao,我的代码有问题:现在我使用Stringbuilder指定日期,但收到一些错误: 我的Servlet: package br.com.cad.basica; import java.util.Calendar; public class Contato { private Long id; private String nome; private String sobrenome; private String email;

我的代码有问题:现在我使用Stringbuilder指定日期,但收到一些错误: 我的Servlet:

  package br.com.cad.basica;

import java.util.Calendar;

public class Contato {

        private Long id;
        private String nome;
        private String sobrenome;
        private String email;
        private String endereco;
        private Calendar dataNascimento1;
        private Calendar dataNascimento2;
        private Calendar dataNascimento3;
        private String rg;
        private String cpf;
        private String sexo;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getNome() {
            return nome;
        }
        public void setNome(String nome) {
            this.nome = nome;
        }
        public String getSobrenome() {
            return sobrenome;
        }
        public void setSobrenome(String sobrenome) {
            this.sobrenome = sobrenome;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getEndereco() {
            return endereco;
        }
        public void setEndereco(String endereco) {
            this.endereco = endereco;
        }
        public Calendar getDataNascimento1() {
            return dataNascimento1;
        }
        public void setDataNascimento1(Calendar dataNascimento1) {
            this.dataNascimento1 = dataNascimento1;
        }
        public Calendar getDataNascimento2() {
            return dataNascimento2;
        }
        public void setDataNascimento2(Calendar dataNascimento2) {
            this.dataNascimento2 = dataNascimento2;
        }
        public Calendar getDataNascimento3() {
            return dataNascimento3;
        }
        public void setDataNascimento3(Calendar dataNascimento3) {
            this.dataNascimento3 = dataNascimento3;
        }
        public String getRg() {
            return rg;
        }
        public void setRg(String rg) {
            this.rg = rg;
        }
        public String getCpf() {
            return cpf;
        }
        public void setCpf(String cpf) {
            this.cpf = cpf;
        }
        public String getSexo() {
            return sexo;
        }
        public void setSexo(String sexo) {
            this.sexo = sexo;
        }




}
我的类Contato(我不知道这里是否需要实现一些代码?)

My htmllet代码cadastra.jsp(将数据发送到我的servlet并保存我的数据库):


在添加联系人(?)之前,必须调用
contato.setDataNascimento1(someDate)
。这句话的问题在于:

contato.getDataNascimento1().getTimeInMillis()
contato.getDataNascimento1()
在执行时解析为
null

已计算行数,该语句位于第30行


添加

你应该把线换掉

private Calendar dataNascimento1;
private Calendar dataNascimento2;
private Calendar dataNascimento3;

并相应地更改getter/setter。现在您有了一个包含联系人生日的字段,您可以将其添加到insert语句中,而无需进一步转换


剩下的唯一挑战是如何基于从ui捕获的值创建
Date
对象。格式化程序就是这样做的。但是
Date
有一些方便的构造函数可以基于这些值创建日期。注意,月份是以零为基础的,根据法律,一月是
0
,二月是
1
,依此类推。创建了
新日期(…)
后,可以调用Contato类上相应的setter并将出生日期存储在DTO上。

堆栈跟踪非常具体:在
Cadastro.adiciona
方法的第30行中有一个
NullPointerException
。这意味着,第30行中的变量具有
null
值,并且正在使用。请学习阅读stacktrace。并尝试只发布相关代码而不是完整代码。
NullPointerException
似乎位于调用
getDataNascimento1()的行上
-您确定这总是返回非空值吗?但我的疑问是,我在stringbuilder finaldate中指定了datanascimento1+datanascimento2+DataCimento3如何添加包含这3个日期的finaldate?contato.getFinalDate不起作用,所以我需要做什么?我还有一些疑问。您的“Contato”类有三个
Calendar
字段用于生日(?),在ui上,您可以使用相同名称的字段(字符串)存储日期、月份和年份。那不太合适。将您的数据设计为有一个字段作为生日,并将连接的字符串解析为
日期
@Wesley-我已经在我的答案中添加了一些提示和建议。很好!!!我取代了我的课程Contato:私人日期dataNascimento;public Date getDataNascimento(){return dataNascimento;}public void setDataNascimento(Date dataNascimento){this.dataNascimento=dataNascimento;}但我现在的疑问是如何实现我的servlet?StringBuilder finalDate=newstringbuilder(“dataNascimento”).append(“/”+request.getParameter(“dataNascimento”)‌​追加(“/”+request.getParameter(“DataNascimento3”);SimpleDataFormat sdf=新的SimpleDataFormat(“日/月/年”);finalDate.toString();我需要为??User
Integer.parseInt(someString)
替换此代码,以将字符串转换为数字,然后查看
Date
类构造函数。最后的步骤其实并不难。
<...some code here
    <label>Data de nascimento</label>  
            <br>  
                <select id="birthDay" name="dataNascimento1">  
                    <option selected="" value="">Dia</option>  
                        <option value="01">1</option>  
                        <option value="02">2</option>  
</select>  
              <select id="birthMonth" name="dataNascimento2">  
                    <option selected="" value="">Mês</option>  
                        <option value="01">janeiro</option>  
                        <option value="02">fevereiro</option>  
</select>  
              <select id="birthYear" name="dataNascimento3">  
                 <option selected="" value="">Ano</option>  
                        <option value="2013">2013</option>  
                        <option value="2012">2012</option>  
</select> 
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
    br.com.cad.dao.Cadastro.adiciona(Cadastro.java:30)
    br.com.cad.servlet.AddDados.service(AddDados.java:48)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.40 logs.
contato.getDataNascimento1().getTimeInMillis()
private Calendar dataNascimento1;
private Calendar dataNascimento2;
private Calendar dataNascimento3;
private Date dataNascimento;