I am getting an error
Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
I have tried everything, but nothing seems to work
After Run project, my app stopped on this method:
getPriceDataByNameProductAndSupplier
Code:
/*_____________________________ My query _______________________*/
public double getPriceDataByNameProductAndSupplier(int product_id,int supplier_id)
{
Product product=null;
double Price_for_every_one=0.0;
String query= "select max(price_for_every_one) from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;
Cursor cursor = sqLiteDatabase.rawQuery(query,null/*ew String[] {String.valueOf(product_id),
String.valueOf(supplier_id)}*/); /*("product_Multi_Value",
new String[] {"MAX(price_for_every_one) AS MAX"},"_id=? and _idSupplier=?",
new String[] {String.valueOf(product_id),String.valueOf(supplier_id)},
null,null,null)*/;// cursor.moveToFirst();
if (cursor !=null)
{
cursor.moveToFirst();
do
{
Price_for_every_one = cursor.getDouble(cursor.getColumnIndexOrThrow("price_for_every_one") );
product = new Product(Price_for_every_one);
}
while (cursor.moveToNext());
}
return Price_for_every_one;
}
Table Creation
/_______________________ (My Table) _______________________/ String Crete_Table_Product_For_Multi_Value = "CREATE TABLE IF NOT EXISTS product_Multi_Value( Product_Type TEXT NOT NULL,product_date TEXT NOT NULL,supplier_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,exp_date TEXT NOT NULL, quantity_package REAL NOT NULL, number_quantity_ber_package INTEGER NOT NULL, total_of_quantity REAL NOT NULL, price_for_Allquantity REAL NOT NULL, price_for_every_one REAL NOT NULL, price_for_sales REAL,_id INTEGER NOT NULL, idSupplier INTEGER NOT NULL, FOREIGN KEY( _idSupplier) REFERENCES Supplier(_id), FOREIGN KEY(_id) REFERENCES product(_id));";
Error Log:
The error:
E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ans.accouting_s_p, PID: 1349
java.lang.IllegalStateException: Could not execute method for android:onClickat androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:5623) at android.view.View$PerformClick.run(View.java:22433) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6316) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:5623) at android.view.View$PerformClick.run(View.java:22433) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6316) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetDouble(Native Method) at android.database.CursorWindow.getDouble(CursorWindow.java:543) at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:87) at com.ans.accouting_s_p.databaseTable.DataSourceProduct.getpriceDataByNameProductAndSupplier(DataSourceProduct.java:322)
The issue is you are trying to access the column price_for_every_one
in the cursor and it doesn't exist because when you use aggregation method like MAX, MIN, SUM etc
it changed the name of the column in the result unless you use AS
keyword
in your SQL query
String query = "select max(price_for_every_one) from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;
column name in the result will be
max(price_for_every_one)
not
price_for_every_one
so you can't use the column name in
Price_for_every_one = cursor.getDouble(cursor.getColumnIndexOrThrow("price_for_every_one") );
since you are selecting only one column you can access the column by its index
cursor.getDouble(0)
or you can use AS
keyword in your query like this
String query = "select max(price_for_every_one) As price_for_every_one from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;```
I hope this helps you