设置值时出现NullPointerException

时间:2014-12-29 15:15:46

标签: java database jsp nullpointerexception

我在动作类中遇到了一个问题。当我写这段代码时:

listRehber.add(new Rehber(
    rs.getString("KISI_ID").toString(),
    rs.getString("AD").toString(),
    rs.getString("SOYAD").toString(),
    rs.getString("UNVANI").toString(),
    rs.getString("GOREVI").toString(),
    rs.getString("FIRMA").toString(),
    rs.getString("ADRES").toString(),
    rs.getString("TEL").toString(),
    rs.getString("FAX").toString(),
    rs.getString("GSM").toString(),
    rs.getString("MAIL").toString(),
    rs.getString("WEB").toString()));
}

因为db可能是空值。例如,DB将具有“名称”值,但它不具有“传真”。怎么能通过这个nullpointer异常 非常感谢你

我的Rehber.class

public class Rehber {
    private String adi = "";
    private String soyadi = "";
    private String unvani = "";
    private String gorevi = "";
    private String firma = "";
    private String adres = "";
    private String tel = "";
    private String fax = "";
    private String gsm = "";
    private String mail = "";
    private String web = "";
    private String id = "";

    public Rehber(String id, String adi, String soyadi, String unvani,
            String gorevi, String firma, String adres, String tel, String fax,
            String gsm, String mail, String web) {

        this.adi = adi; 
        this.soyadi = soyadi;
        this.unvani = unvani;   
        this.gorevi = gorevi;       
        this.firma = firma;     
        this.adres = adres;     
        this.tel = tel;     
        this.fax = fax; 
        this.gsm = gsm;     
        this.mail = mail;   
        this.web = web;
        this.id = id;
    }
}

并获取/设置方法

1 个答案:

答案 0 :(得分:3)

您已经在调用ResultSet#getString,这将返回String。无需在toString中调用String,因此请删除针对您的参数的方法调用。

listRehber.add(
    new Rehber(
        rs.getString("KISI_ID"),
        rs.getString("AD"),
        rs.getString("SOYAD"),
        rs.getString("UNVANI"),
        rs.getString("GOREVI"),
        rs.getString("FIRMA"),
        rs.getString("ADRES"),
        rs.getString("TEL"),
        rs.getString("FAX"),
        rs.getString("GSM"),
        rs.getString("MAIL"),
        rs.getString("WEB")
    ));