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

时间:2012-12-10 08:46:47

标签: java class arraylist resultset

当我调用我的两个方法时,我得到一个空指针异常。我不知道,为什么我明白了。当我检查代码似乎是正确的

这是我的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();

4 个答案:

答案 0 :(得分:5)

不要使用

HentbestillingsordreHandler hboh = null;

相反,请尝试

HentbestillingsordreHandler hboh = new HentbestillingsordreHandler();

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

然而,第一个语句只是将hboh指向计算机内存中无用(且无法访问)的点;所以在运行时,当你尝试访问hboh时,你会得到一个空指针异常,因为变量hboh是“指向”一个不存在的内存。

答案 1 :(得分:0)

您正在使用相同的名称::

声明方法的名称及其中的变量
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;


}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

NullPointerExceptions通常很容易搞清楚。在堆栈跟踪中,您将获得发生异常的类和行号。在您的情况下,它应该是行hboh.Hentbestillingsordre()。使用该信息,您可以检查将值分配给变量的位置。您的作业是HentbestillingsordreHandler hboh = null。由于您无法在null上进行任何方法调用,因此您将获得例外。

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

相关问题