簡體   English   中英

空指針指向外鍵

[英]Null Pointer at foreign key

我正在使用MySQL和Hibernate。 我已將記錄添加到“ Receiving表中,並將idbarcode字段作為transactionrcidbarcode外鍵。

當我使用ID搜索特定記錄時:

int id =(Integer)defaultTableModel.getValueAt(jReceivingsTable.getSelectedRow(),0);

Receivings receivings = manager.searchReceivingsId(id);

那不是null ,打印id返回適當的值,那么為什么當我將它設置為Transaction table時卻得到null?

Rctransaction rctransaction = new Rctransaction();
    rctransaction.getReceivings().getId().setId(id); // here id has value 
    rctransaction.getReceivings().getId().setBarcode(barcode);
    rctransaction.setReceivings(receivings);  

這是課程:
Receivings.java

public class Receivings  implements java.io.Serializable {


     private ReceivingsId id;
     private Suppliers suppliers;
     private String itemName;
     private String category;
     private double wholesalePrice;
     private double retailPrice;
     private long tax1;
     private long tax2;
     private String type1;
     private String type2;
     private int quantityStock;
     private int receivingQuantity;
     private int recorderLevel;
     private String receivingDate;
     private double discount;
     private Set itemses = new HashSet(0);
     private Set rctransactions = new HashSet(0);

    public Receivings() {
    }


    public Receivings(ReceivingsId id, Suppliers suppliers, String itemName, String category, double wholesalePrice, double retailPrice, long tax1, long tax2, String type1, String type2, int quantityStock, int receivingQuantity, int recorderLevel, String receivingDate, double discount) {
        this.id = id;
        this.suppliers = suppliers;
        this.itemName = itemName;
        this.category = category;
        this.wholesalePrice = wholesalePrice;
        this.retailPrice = retailPrice;
        this.tax1 = tax1;
        this.tax2 = tax2;
        this.type1 = type1;
        this.type2 = type2;
        this.quantityStock = quantityStock;
        this.receivingQuantity = receivingQuantity;
        this.recorderLevel = recorderLevel;
        this.receivingDate = receivingDate;
        this.discount = discount;
    }
    public Receivings(ReceivingsId id, Suppliers suppliers, String itemName, String category, double wholesalePrice, double retailPrice, long tax1, long tax2, String type1, String type2, int quantityStock, int receivingQuantity, int recorderLevel, String receivingDate, double discount, Set itemses, Set rctransactions) {
       this.id = id;
       this.suppliers = suppliers;
       this.itemName = itemName;
       this.category = category;
       this.wholesalePrice = wholesalePrice;
       this.retailPrice = retailPrice;
       this.tax1 = tax1;
       this.tax2 = tax2;
       this.type1 = type1;
       this.type2 = type2;
       this.quantityStock = quantityStock;
       this.receivingQuantity = receivingQuantity;
       this.recorderLevel = recorderLevel;
       this.receivingDate = receivingDate;
       this.discount = discount;
       this.itemses = itemses;
       this.rctransactions = rctransactions;
    }

    public ReceivingsId getId() {
        return this.id;
    }

    public void setId(ReceivingsId id) {
        this.id = id;
    }
    public Suppliers getSuppliers() {
        return this.suppliers;
    }

    public void setSuppliers(Suppliers suppliers) {
        this.suppliers = suppliers;
    }
    public String getItemName() {
        return this.itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
    public double getWholesalePrice() {
        return this.wholesalePrice;
    }

    public void setWholesalePrice(double wholesalePrice) {
        this.wholesalePrice = wholesalePrice;
    }
    public double getRetailPrice() {
        return this.retailPrice;
    }

    public void setRetailPrice(double retailPrice) {
        this.retailPrice = retailPrice;
    }
    public long getTax1() {
        return this.tax1;
    }

    public void setTax1(long tax1) {
        this.tax1 = tax1;
    }
    public long getTax2() {
        return this.tax2;
    }

    public void setTax2(long tax2) {
        this.tax2 = tax2;
    }
    public String getType1() {
        return this.type1;
    }

    public void setType1(String type1) {
        this.type1 = type1;
    }
    public String getType2() {
        return this.type2;
    }

    public void setType2(String type2) {
        this.type2 = type2;
    }
    public int getQuantityStock() {
        return this.quantityStock;
    }

    public void setQuantityStock(int quantityStock) {
        this.quantityStock = quantityStock;
    }
    public int getReceivingQuantity() {
        return this.receivingQuantity;
    }

    public void setReceivingQuantity(int receivingQuantity) {
        this.receivingQuantity = receivingQuantity;
    }
    public int getRecorderLevel() {
        return this.recorderLevel;
    }

    public void setRecorderLevel(int recorderLevel) {
        this.recorderLevel = recorderLevel;
    }
    public String getReceivingDate() {
        return this.receivingDate;
    }

    public void setReceivingDate(String receivingDate) {
        this.receivingDate = receivingDate;
    }
    public double getDiscount() {
        return this.discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }
    public Set getItemses() {
        return this.itemses;
    }

    public void setItemses(Set itemses) {
        this.itemses = itemses;
    }
    public Set getRctransactions() {
        return this.rctransactions;
    }

    public void setRctransactions(Set rctransactions) {
        this.rctransactions = rctransactions;
    }   
}  

ReceivingsId.java

//Composition of Id and Barcode of Receivings  
public class ReceivingsId  implements java.io.Serializable {


     private int id;
     private int barcode;

    public ReceivingsId() {
    }

    public ReceivingsId(int id, int barcode) {
       this.id = id;
       this.barcode = barcode;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public int getBarcode() {
        return this.barcode;
    }

    public void setBarcode(int barcode) {
        this.barcode = barcode;
    }


   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
         if ( (other == null ) ) return false;
         if ( !(other instanceof ReceivingsId) ) return false;
         ReceivingsId castOther = ( ReceivingsId ) other; 

         return (this.getId()==castOther.getId())
 && (this.getBarcode()==castOther.getBarcode());
   }

   public int hashCode() {
         int result = 17;

         result = 37 * result + this.getId();
         result = 37 * result + this.getBarcode();
         return result;
   }   
}

Rctransaction.java

public class Rctransaction  implements java.io.Serializable {


     private Integer id;
     private Receivings receivings;
     private String transaction;
     private String supplierName;
     private String itemName;
     private double quantity;
     private String paymentType;
     private double amountTendred;
     private double due;
     private double total;
     private double price;
     private int recorderLevel;

    public Rctransaction() {
    }

    public Rctransaction(Receivings receivings, String transaction, String supplierName, String itemName, double quantity, String paymentType, double amountTendred, double due, double total, double price, int recorderLevel) {
       this.receivings = receivings;
       this.transaction = transaction;
       this.supplierName = supplierName;
       this.itemName = itemName;
       this.quantity = quantity;
       this.paymentType = paymentType;
       this.amountTendred = amountTendred;
       this.due = due;
       this.total = total;
       this.price = price;
       this.recorderLevel = recorderLevel;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    public Receivings getReceivings() {
        return this.receivings;
    }

    public void setReceivings(Receivings receivings) {
        this.receivings = receivings;
    }
    public String getTransaction() {
        return this.transaction;
    }

    public void setTransaction(String transaction) {
        this.transaction = transaction;
    }
    public String getSupplierName() {
        return this.supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }
    public String getItemName() {
        return this.itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public double getQuantity() {
        return this.quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }
    public String getPaymentType() {
        return this.paymentType;
    }

    public void setPaymentType(String paymentType) {
        this.paymentType = paymentType;
    }
    public double getAmountTendred() {
        return this.amountTendred;
    }

    public void setAmountTendred(double amountTendred) {
        this.amountTendred = amountTendred;
    }
    public double getDue() {
        return this.due;
    }

    public void setDue(double due) {
        this.due = due;
    }
    public double getTotal() {
        return this.total;
    }

    public void setTotal(double total) {
        this.total = total;
    }
    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    public int getRecorderLevel() {
        return this.recorderLevel;
    }

    public void setRecorderLevel(int recorderLevel) {
        this.recorderLevel = recorderLevel;
    }
}

您以錯誤的順序實例化了Hibernate對象。

首先,您需要一個ReceivingsId:

ReceivingsId rid = new ReceivingsId (id, barcode);

然后將此rid添加到Receivings

Receivings rec = new Receivings();
rec.setId(rid);

最后將其設置為Rctransaction

Rctransaction rctransaction = new Rctransaction();
rctransaction.setReceivings(rec);  

如果在實例化對象后嘗試調用getReceivings而不進行設置,則始終會得到NullPointerException。 為避免這種情況,您可以在Rctransaction構造函數中實例化Receivings對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM