SQLite总是抛出nullPointerException

时间:2013-10-21 15:47:53

标签: android sqlite

//this is in my custom dialog class oncreate method
InvoiceDatabase invoiceDatabase = new InvoiceDatabase(context);
TotalInvoiceVO totalInvoiceVo = invoiceDatabase.getPrintInvoiceDetails(custCode);
invoiceDetails.setText(totalInvoiceVo.toString());

//this is my InvoiceDatabase class
public static String GET_INVOICEDETAILS = "select P.ProductCode productCode, P.MRP mrp, P.SellingRate sellingRate, P.ShortProductDesc desc, P.Vat vat, " +
            " P.AdditionalTaxes additionalVat,"
            + " P.Cess cess, PalmOrderNo palmOrderNum, "
            + " SaleQty saleQty, DiscAmount stkDiscount ,SchemeAmount schemeAmount, SchemeDisc schemeDiscount, NetValue netValue from PALM_ORD_DET, PALM_ORDER"
            + " PRODUCT P where PALM_ORD_DET.ProductCode=PRODUCT.ProductCode and PALM_ORD_DET.PalmOrderNo=PALM_ORDER.PalmOrderNo and PALM_ORDER.CustCode=?";

    public TotalInvoiceVO getPrintInvoiceDetails(String custCode) {
        TotalInvoiceVO totalInvoiceVO = new TotalInvoiceVO();
        // getStockiest Detail
        // get Customer Detail
        // get CustomerSummary
        List<InvoiceVO> listOfProductInvoice = getProdOrderInvoiceDetails(custCode);
        totalInvoiceVO.setListOfProductInvoice(listOfProductInvoice);
        return totalInvoiceVO;
    }

    private List<InvoiceVO> getProdOrderInvoiceDetails(String custCode) {
        List<InvoiceVO> listOfInvoiceVO = new ArrayList<InvoiceVO>();

        Cursor cursor = null;
        try {

            /*cursor=db.rawQuery("select * from PALM_ORD_DET", null);
            if(cursor.moveToFirst())
            {
                String m="m";
            }*/

            cursor = db.rawQuery(GET_INVOICEDETAILS, new String[] { custCode });
            if (cursor.moveToFirst()) {
                do {
                    InvoiceVO invoiceVO = new InvoiceVO();
                    invoiceVO.setProdDesc(cursor.getString(cursor.getColumnIndex("desc")));
                    invoiceVO.setProdCode(cursor.getString(cursor.getColumnIndex("productCode")));
                    invoiceVO.setMrp(cursor.getDouble(cursor.getColumnIndex("mrp")));
                    invoiceVO.setSellingPrice(cursor.getDouble(cursor.getColumnIndex("sellingRate")));
                    invoiceVO.setNetValue(cursor.getDouble(cursor.getColumnIndex("netValue")));
                    invoiceVO.setStockDiscount(cursor.getString(cursor.getColumnIndex("stkDiscount")));
                    listOfInvoiceVO.add(invoiceVO);
                } while (cursor.moveToNext());
            }
        } finally {
            cursor.close();
            cursor=null;
        }

我正在尝试从数据库中检索值,但场景是当我查询数据库时,它总是抛出nullPointerException。可能有什么不对?

10-21 20:40:24.098: E/AndroidRuntime(27346): FATAL EXCEPTION: main
10-21 20:40:24.098: E/AndroidRuntime(27346): java.lang.NullPointerException
10-21 20:40:24.098: E/AndroidRuntime(27346):    at comColgateDMS.org.db.InvoiceDatabase.getProdOrderInvoiceDetails(InvoiceDatabase.java:97)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at comColgateDMS.org.db.InvoiceDatabase.getPrintInvoiceDetails(InvoiceDatabase.java:66)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at comColgateDMS.org.CustomDialog_invoicePrint.onCreate(CustomDialog_invoicePrint.java:46)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.app.Dialog.dispatchOnCreate(Dialog.java:353)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.app.Dialog.show(Dialog.java:257)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at comColgateDMS.org.OrderBookingSummary$1.onItemClick(OrderBookingSummary.java:214)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.widget.AbsListView.performItemClick(AbsListView.java:1065)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2522)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.widget.AbsListView$1.run(AbsListView.java:3183)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.os.Handler.handleCallback(Handler.java:605)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.os.Looper.loop(Looper.java:137)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at android.app.ActivityThread.main(ActivityThread.java:4441)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at java.lang.reflect.Method.invokeNative(Native Method)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at java.lang.reflect.Method.invoke(Method.java:511)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-21 20:40:24.098: E/AndroidRuntime(27346):    at dalvik.system.NativeStart.main(Native Method)
10-21 20:40:25.878: I/Process(27346): Sending signal. PID: 27346 SIG: 9

3 个答案:

答案 0 :(得分:0)

... from PALM_ORD_DET, PALM_ORDER"
+ " PRODUCT P where ...

这将连接到

... from PALM_ORD_DET, PALM_ORDER PRODUCT P where ...

哪个无效!

您必须选择

... from PALM_ORD_DET, PALM_ORDER PRODUCT where ...

... from PALM_ORD_DET, PALM_ORDER PRODUCT where ...

并相应地使用对PALM_ORDER的引用。

答案 1 :(得分:0)

cursor.close()不正确如果要关闭数据库连接使用sQLiteDatabase.close();

,则不需要它而不是

答案 2 :(得分:0)

您的查询返回null,这是您尝试访问它时的光标。您可能想要添加

cursor != null

访问前的条件。