简体   繁体   English

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

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

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

}

} }

my kaldsql 我的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 这是我的班级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();

Don't use 不要使用

HentbestillingsordreHandler hboh = null;

Instead, try 相反,尝试

HentbestillingsordreHandler hboh = new HentbestillingsordreHandler();

When you call new HentbestillingsordreHandler() , you're creating a new HentbestillingsordreHandler object that will be stored somewhere in the computer's memory; 当您调用new HentbestillingsordreHandler() ,您正在创建一个新的HentbestillingsordreHandler对象,该对象将存储在计算机内存中的某个位置。 you're then telling hboh to point to that particular piece of memory to be used whenever you access hboh . 然后,您要告诉hboh指向每次访问hboh时要使用的特定内存。

The first statement, however, simply points hboh to a useless (and unreachable) point of the computer's memory; 但是,第一个语句只是将hboh指向计算机内存的无用(且无法访问)的点。 so at runtime, when you try to access hboh , you get a Null Pointer Exception, since the variable hboh is "Pointing" to a non-existent piece of memory. 因此在运行时,当您尝试访问hboh ,会得到一个Null Pointer Exception,因为变量hboh是“指向”不存在的内存。

You are declaring the name of the method as well as a variale inside it with the same name :: 您在声明方法的名称以及其中的同名变量::

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;


}

Java as opposed to Objective-C for example, doesn't allow method invocations on null references. 例如,与Objective-C相反,Java不允许对空引用进行方法调用。 So in general you have to initialize your reference with an object instance before trying to call a method on it. 因此,通常在尝试调用对象实例之前,必须使用对象实例初始化引用。

NullPointerExceptions are usually pretty easy to figure out. 通常很容易找出NullPointerExceptions。 In the stacktrace you get the class and line number where the exception occurred. 在堆栈跟踪中,您可以获取发生异常的类和行号。 In your case it should be the line hboh.Hentbestillingsordre() . 在您的情况下,应为hboh.Hentbestillingsordre() Using that information you can check where a value was assigned to the variable. 使用该信息,您可以检查将值分配给变量的位置。 Your assignment is HentbestillingsordreHandler hboh = null . 您的分配是HentbestillingsordreHandler hboh = null Since you cannot make any method calls on null you get the exception. 由于您不能对null进行任何方法调用,因此会得到异常。

In some cases you do not need any attributes of the objects for the method. 在某些情况下,方法不需要对象的任何属性。 In that case you can make the method static . 在这种情况下,您可以将方法static You would call it with HentbestillingsordreHandler.Hentbestillingsordre() then, without the need for an instance of that class. 然后,您可以使用HentbestillingsordreHandler.Hentbestillingsordre()进行调用,而无需该类的实例。

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

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