1

i'm getting a null pointer exception when i call my two methods. And i have no idea, why i get it. when i check the code it seems right

this is my HentbestillingsordreHandler class

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);
    }

}

}

my 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;


}

and this is my class 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;
}

and when i call it i type this

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

4 に答える 4

5

使用しないでください

HentbestillingsordreHandler hboh = null;

代わりに、試してください

HentbestillingsordreHandler hboh = new HentbestillingsordreHandler();

を呼び出すと、コンピュータのメモリのどこかに保存されるnew HentbestillingsordreHandler()新しいオブジェクトが作成されます。次に、にアクセスするたびに使用される特定のメモリを指すようにHentbestillingsordreHandler指示しています。hbohhboh

ただし、最初のステートメントhbohは、コンピューターのメモリの役に立たない (そして到達できない) ポイントを指しているだけです。そのため、実行時に にアクセスしようとすると、変数が存在しないメモリを「指している」hbohため、Null Pointer Exceptionが発生します。hboh

于 2012-12-10T08:48:45.477 に答える
0

NullPointerExceptionsは通常、非常に簡単に理解できます。スタックトレースでは、例外が発生したクラスと行番号を取得します。あなたの場合、それは行でなければなりませんhboh.Hentbestillingsordre()。その情報を使用して、値が変数に割り当てられた場所を確認できます。あなたの割り当てはHentbestillingsordreHandler hboh = nullです。メソッド呼び出しを行うことができないためnull、例外が発生します。

場合によっては、メソッドのオブジェクトの属性は必要ありません。その場合、メソッドを作成できますstaticHentbestillingsordreHandler.Hentbestillingsordre()そのクラスのインスタンスを必要とせずに、thenで呼び出します。

于 2012-12-10T08:57:59.157 に答える
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;


}
于 2012-12-10T08:51:49.143 に答える
0

たとえば、Java は Objective-C とは対照的に、null 参照でのメソッド呼び出しを許可しません。そのため、一般に、メソッドを呼び出そうとする前に、オブジェクト インスタンスで参照を初期化する必要があります。

于 2012-12-10T08:53:49.517 に答える