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

Java 调用方法时出现空指针异常,java,class,arraylist,resultset,Java,Class,Arraylist,Resultset,当我调用我的两个方法时,我得到一个空指针异常。我不知道为什么我会得到它。当我检查代码时,它似乎是正确的 这是我的HentbestillingsordreHandler课程 public class HentbestillingsordreHandler extends JPanel{ private Connection con = null; private ResultSet rs = null; private HentbestillingsordreRegistrer ho; priv

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

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

}
这是我的班级Hentbestillingsordrereregister

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
时,会出现一个空指针异常,因为变量
hboh
正“指向”一个不存在的内存块。

不要使用

HentbestillingsordreHandler hboh = null;
相反,试试看

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


然而,第一条语句只是将hboh指向计算机内存中一个无用(且无法访问)的点;因此,在运行时,当您尝试访问
hboh
时,您会得到一个空指针异常,因为变量
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;


}

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

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不允许对空引用进行方法调用。因此,一般来说,在尝试调用对象实例上的方法之前,必须先用对象实例初始化引用。

例如,与Objective-C相反,Java不允许对空引用进行方法调用。因此,一般来说,在尝试调用对象实例上的方法之前,必须先用对象实例初始化引用。

NullPointerException通常很容易理解。在stacktrace中,您可以获得发生异常的类和行号。在您的情况下,应该是行
hboh.Hentbestillingsordre()
。使用该信息,您可以检查将值分配给变量的位置。您的作业是
HentbestillingSorrehandler hboh=null
。由于无法对
null
进行任何方法调用,因此会出现异常


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

nullpointerException通常很容易理解。在stacktrace中,您可以获得发生异常的类和行号。在您的情况下,应该是行
hboh.Hentbestillingsordre()
。使用该信息,您可以检查将值分配给变量的位置。您的作业是
HentbestillingSorrehandler hboh=null
。由于无法对
null
进行任何方法调用,因此会出现异常



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

您也可以发布stacktrace吗?您文章的最后3行代码清楚地表明,您试图在
null
上调用方法,而不是在任何对象实例上。请参阅用户名tbd答案以修复它。您正在使用Swing和JDBC,但该错误属于Hello World难度级别。请您也发布stacktrace,好吗?您文章中的最后3行代码清楚地表明,您试图在
null
上调用方法,而不是在任何对象实例上。请参阅用户名tbd答案以修复它。您正在使用Swing和JDBC,但该错误属于Hello World难度级别。我现在可以调用HentTestillingSorre(),但仍然收到一个错误,并且在temp+=ho.getLeverAndOrid()+“\n”上出现空指针异常@用户1880497尝试将代码
HentbestillingsordreRegistrer ho=new
替换为简单的
ho=new
。私有HentbestillingsordreRegistrer ho=new HentbestillingsordreRegistrer();将不起作用,因为它需要找到参数insideproblem。谢谢你让我going@user1880497如果这个答案解决了您的问题,请接受它。我现在可以调用HentTestillingSorre(),但是我仍然得到一个错误,并且在temp+=ho.getLeverAndoreId()+“\n”上出现nullpointer异常@用户1880497尝试将代码
HentbestillingsordreRegistrer ho=new
替换为简单的
ho=new
。私有HentbestillingsordreRegistrer ho=new HentbestillingsordreRegistrer();将不起作用,因为它需要找到参数insideproblem。谢谢你让我going@user1880497如果这个答案解决了您的问题,请接受它。@jlordo:我不知道,为什么-1,正如您看到的OP代码一样,在方法“Hentalleordreliste”中,他声明了一个与Hentalleordreliste同名的变量。所以我指出了这一点……允许有与方法同名的局部变量,这将编译并工作:
String foo(){String foo=“abc”;return foo;}
您的答案对解决OPs问题或任何其他问题都没有帮助。@jlordo:我不知道,为什么-1,正如您看到的OP代码一样,在方法“Hentalleordreliste”中,他声明了一个与Hentalleor同名的变量