【问题标题】:Null-pointer exception when i call a method调用方法时出现空指针异常
【发布时间】:2012-12-10 08:46:46
【问题描述】:

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

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

【问题讨论】:

  • 你也可以发布你的堆栈跟踪吗?
  • 您帖子中的最后 3 行代码清楚地表明您正在尝试在 null 上调用方法,而不是在任何对象实例上。请参阅 username tbd 答案以修复它。您正在使用 Swing 和 JDBC,但该错误属于 Hello World 难度级别。

标签: java class arraylist resultset


【解决方案1】:

不要使用

HentbestillingsordreHandler hboh = null;

相反,试试

HentbestillingsordreHandler hboh = new HentbestillingsordreHandler();

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

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

【讨论】:

  • 我现在可以调用 Hentbestillingsordre() 但我仍然收到错误并且我在 temp += ho.getLeverandoerID()+" \n" 上出现空指针异常;
  • @user1880497 尝试用简单的ho = new 替换代码HentbestillingsordreRegistrer ho = new
  • 私有 HentbestillingsordreRegistrer ho = new HentbestillingsordreRegistrer();不起作用,因为它需要内部参数
  • 发现问题。谢谢你让我去
  • @user1880497 如果这个回答解决了你的问题,请采纳。
【解决方案2】:

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

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;


}

【讨论】:

  • @jlordo:我不知道,为什么是-1,正如你看到的OP代码,在方法'Hentalleordreliste'中,他声明了一个与Hentalleordreliste同名的变量。所以我指出了这一点......
  • 允许有与方法同名的局部变量,这将编译和工作:String foo() { String foo = "abc"; return foo; }你的回答对解决OPs问题没有帮助,也没有任何其他问题。
【解决方案3】:

例如,与 Objective-C 相比,Java 不允许对空引用进行方法调用。所以一般来说,你必须先用一个对象实例初始化你的引用,然后再尝试调用它的方法。

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2016-01-01
      • 2015-10-21
      • 2013-01-16
      • 1970-01-01
      相关资源
      最近更新 更多