繁体   English   中英

我调用方法时出现空指针异常

[英]Null-pointer exception when i call a method

调用两个方法时,出现空指针异常。 而且我不知道为什么要得到它。 当我检查代码似乎正确

这是我的HentbestillingsordreHandler类

public class HentbestillingsordreHandler extends JPanel{

private Connection con = null;
private ResultSet rs = null;
private HentbestillingsordreRegistrer ho;
private ArrayList<HentbestillingsordreRegistrer> hbor = new ArrayList<HentbestillingsordreRegistrer>();

HentbestillingsordreHandler() {

}

public void Hentbestillingsordre(){
    KaldSQL ks = new KaldSQL();
    try {
        con = ks.connectNow();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ResultSet rs = ks.Hentalleordreliste(con);
    try {
        while(rs.next()){
            String status = "";
            if(rs.getInt("BestillingsStatus") == 1){
                status = "Leveret";
            }else{
                status = "Ikke Leveret";
            }
            System.out.println(status);
            HentbestillingsordreRegistrer ho = new HentbestillingsordreRegistrer(
                    rs.getInt("BestillingsID"),
                    rs.getString("BestillingsStatus"),
                    rs.getInt("ModtagetAf"),
                    rs.getInt("Vare"),
                    rs.getInt("Antal"));

                    hbor.add(ho);
                    System.out.println(ho);

        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        rs.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}   
public void printBestillingsordre(){

        for(HentbestillingsordreRegistrer hentbestillingsordreRegistrer: hbor){
        String temp = "";
        temp += ho.getLeverandoerID()+" \n";
        System.out.println(temp);
    }

}

}

我的kaldsql

public ResultSet Hentalleordreliste(Connection con){

    ResultSet Hentalleordreliste = null;

    try {
        PreparedStatement statement = con.prepareStatement("select varebestillinger.BestillingsID, " +
                "varebestillinger.LeverandoerID, "+
                "varebestillinger.BestillingsStatus, varebestillinger.ModtagetAf, "+
                "varebestillingsliste.Vare, " +
                "varebestillingsliste.Antal from varebestillinger left outer join " +
                "varebestillingsliste on  ListeID = BestillingsID");
                ResultSet result = statement.executeQuery();
                Hentalleordreliste = result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Hentalleordreliste;


}

这是我的班级HentbestillingsordreRegistrer

int LeverandoerID;
String BestillingsStatus;
int ModtagetAf;
int Vare;
int Antal;

public HentbestillingsordreRegistrer(int LeverandoerID, String BestillingsStatus, int ModtagetAf,int Vare,int Antal){
    this.LeverandoerID = LeverandoerID;
    this.BestillingsStatus = BestillingsStatus;
    this.ModtagetAf = ModtagetAf;
    this.Antal = Antal;
}

public int getLeverandoerID() {
    return LeverandoerID;
}

public void setLeverandoerID(int leverandoerID) {
    LeverandoerID = leverandoerID;
}

public String getBestillingsStatus() {
    return BestillingsStatus;
}

public void setBestillingsStatus(String bestillingsStatus) {
    BestillingsStatus = bestillingsStatus;
}

public int getModtagetAf() {
    return ModtagetAf;
}

public void setModtagetAf(int modtagetAf) {
    ModtagetAf = modtagetAf;
}

public int getVare() {
    return Vare;
}

public void setVare(int vare) {
    Vare = vare;
}

public int getAntal() {
    return Antal;
}

public void setAntal(int antal) {
    Antal = antal;
}

当我叫它的时候

    HentbestillingsordreHandler hboh = null;
hboh.Hentbestillingsordre();
hboh.printBestillingsordre();

不要使用

HentbestillingsordreHandler hboh = null;

相反,尝试

HentbestillingsordreHandler hboh = new HentbestillingsordreHandler();

当您调用new HentbestillingsordreHandler() ,您正在创建一个新的HentbestillingsordreHandler对象,该对象将存储在计算机内存中的某个位置。 然后,您要告诉hboh指向每次访问hboh时要使用的特定内存。

但是,第一个语句只是将hboh指向计算机内存的无用(且无法访问)的点。 因此在运行时,当您尝试访问hboh ,会得到一个Null Pointer Exception,因为变量hboh是“指向”不存在的内存。

您在声明方法的名称以及其中的同名变量::

public ResultSet Hentalleordreliste(Connection con){
ResultSet result = null;

    try {
        PreparedStatement statement = con.prepareStatement("select varebestillinger.BestillingsID, " +
                "varebestillinger.LeverandoerID, "+
                "varebestillinger.BestillingsStatus, varebestillinger.ModtagetAf, "+
                "varebestillingsliste.Vare, " +
                "varebestillingsliste.Antal from varebestillinger left outer join " +
                "varebestillingsliste on  ListeID = BestillingsID");
                result = statement.executeQuery();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;


}

例如,与Objective-C相反,Java不允许对空引用进行方法调用。 因此,通常在尝试调用对象实例之前,必须使用对象实例初始化引用。

通常很容易找出NullPointerExceptions。 在堆栈跟踪中,您可以获取发生异常的类和行号。 在您的情况下,应为hboh.Hentbestillingsordre() 使用该信息,您可以检查将值分配给变量的位置。 您的分配是HentbestillingsordreHandler hboh = null 由于您不能对null进行任何方法调用,因此会得到异常。

在某些情况下,方法不需要对象的任何属性。 在这种情况下,您可以将方法static 然后,您可以使用HentbestillingsordreHandler.Hentbestillingsordre()进行调用,而无需该类的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM