Null Pointer Exception print in needs a message error from sqlite - android

I'm getting a NullPointerException: println needs a message Error when trying to start an activity. I can't see the error.
Here is my database concerning the error
public HashMap<String, String> getsaleInfo(String saleId) {
HashMap<String, String> wordList = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM SUPERSALE where supersaleId='"+saleId+"'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
//HashMap<String, String> map = new HashMap<String, String>();
wordList.put("saleCust", cursor.getString(1));
wordList.put("saleName", cursor.getString(2));
wordList.put("saleTime", cursor.getString(4));
wordList.put("saleTax", cursor.getString(5));
wordList.put("saleNotes", cursor.getString(6));
//wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
And here is how I retrieve in the activity
saleCust = (TextView) findViewById(R.id.saleCust);
saleNotes = (TextView) findViewById(R.id.saleNotes);
saleName = (TextView) findViewById(R.id.saleName);
saleTime = (TextView) findViewById(R.id.saleTime);
saleTax = (TextView) findViewById(R.id.saleTax);
Intent objIntent = getIntent();
String saleId = objIntent.getStringExtra("supersaleId");
Log.d("Reading: ", "Reading all contacts..");
HashMap<String, String> saleList = controller.getsaleInfo(saleId);
Log.d("saleCust",saleList.get("saleCust"));
Log.d("saleName",saleList.get("saleName"));
Log.d("saleTime",saleList.get("saleTime"));
Log.d("saleTax",saleList.get("saleTax"));
Log.d("saleNotes",saleList.get("saleNotes"));
if(saleList.size()!=0) {
saleCust.setText(saleList.get("saleCust"));
saleName.setText(saleList.get("saleName"));
saleTime.setText(saleList.get("saleTime"));
saleTax.setText(saleList.get("saleTax"));
saleNotes.setText(saleList.get("saleNotes"));
}}
The error is coming from line
Log.d("saleCust",saleList.get("saleCust"));

Please post your logcat next time and your UI activity in question. Since you have textview from your layout, you could be using a Listview, ExpandedListView, etc. If so, android view's require that there is a column _id in your table and is retrieved in your query so that the view can keep in sync with the data adapter's data from your database cursor via the _id column.
Hope this helps.

Related

how to remove duplicate entries when displayed in listview

i have textfile and i manage to display it in listview , my problem is that i want to remove the duplicate entries in column Category, Code,Description when displayed in listview. Thank you for helping! //here is my textfile with duplicate entries
//here is my code in displaying
public ArrayList<HashMap<String, String>> getAllProducts() {
ArrayList<HashMap<String, String>> proList;
proList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM Countsheet";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Category", cursor.getString(1));
map.put("Code", cursor.getString(2));
map.put("Description", cursor.getString(3));
map.put("Unit", cursor.getString(4));
map.put("Quantity", cursor.getString(5));
proList.add(map);
} while (cursor.moveToNext());
}
return proList;
}

Hashmap into database

I stored hashmap in database but when the items in hashmap are more than one, the last value is shown multiple times... for example in hashmap i have
bag
pen
book
but when I retrieve the values I get
book
book
book
How do I fix this?
DatabaseHandler.java
public void addQuote(Quote q, ArrayList<HashMap<String, String>> put_q_list)
{
myDataBase=this.getWritableDatabase();
try{
ContentValues values=new ContentValues();
values.put("q_customer_name", q.getCustomer_name());
values.put("q_customer_email", q.getCustomer_email());
values.put("q_b_street", q.getBilling_street());
values.put("q_b_city", q.getBilling_city());
values.put("q_b_state", q.getBilling_state());
values.put("q_b_zipcode", q.getBilling_zipcode());
values.put("q_b_country", q.getBilling_country());
values.put("q_s_street", q.getShipping_street());
values.put("q_s_city", q.getShipping_city());
values.put("q_s_state", q.getShipping_state());
values.put("q_s_zipcode", q.getShipping_zipcode());
values.put("q_s_country", q.getShipping_country());
values.put("q_day", q.getDay());
values.put("q_month", q.getMonth());
values.put("q_year", q.getYear());
values.put("q_total", q.getTotal());
values.put("q_discount", q.getTotal_discount());
myDataBase.insert("quote",null,values);
}catch(Exception e)
{
Log.e("bderror",e.toString());
e.printStackTrace();
}
String selectQuery = "SELECT quote_id FROM quote WHERE q_customer_email=?";
Cursor cursor = myDataBase.rawQuery(selectQuery, new String[] {q.getCustomer_email()});
int i=0;
while(cursor.moveToNext())
{
i=cursor.getInt(0);
}
cursor.close();
for (HashMap<String, String> map : put_q_list) {
ContentValues cv = new ContentValues();
cv.put("quote_id", i);
cv.put("q_item_code", map.get("item_code_final"));
cv.put("q_item_desc", map.get("desc_final"));
cv.put("q_item_price", map.get("item_price_final"));
cv.put("q_item_qty", map.get("item_qty_final"));
cv.put("q_item_tax", map.get("item_tax_final"));
cv.put("q_item_discount", map.get("item_discount_final"));
myDataBase.insert("q_item", null, cv);
}
}
Add.java`
HashMap<String, String> map_item_final = new HashMap<String, String>();
map_item_final.put("item_code_final",autoComplete_item.getText().toString());
map_item_final.put("desc_final", item_desc.getText().toString());
map_item_final.put("item_price_final", String.valueOf(item_price.getText()));
map_item_final.put("item_tax_final",String.valueOf(item_tax.getText()));
map_item_final.put("item_discount_final",String.valueOf(item_discount.getText()));
map_item_final.put("item_qty_final", item_qty.getText().toString());
final_items.add(map_item_final);
dataBase.addQuote(q, final_items);
Retrieving....
public ArrayList<HashMap<String, String>> getQuoteItem(String quote_id)
{
ArrayList<HashMap<String, String>> put_list= new ArrayList<HashMap<String, String>>();
myDataBase=this.getWritableDatabase();
Cursor cursor=myDataBase.rawQuery("SELECT * from q_item where quote_id="+Integer.parseInt(quote_id),null);
HashMap<String, String> map = new HashMap<String, String>();
while(cursor.moveToNext())
{
map.put("i_item_code", cursor.getString(1));
map.put("i_item_desc", cursor.getString(2));
map.put("i_item_price", String.valueOf(cursor.getInt(3)));
map.put("i_item_qty", String.valueOf(cursor.getInt(4)));
map.put("i_item_tax", String.valueOf(cursor.getFloat(5)));
map.put("i_item_discount", String.valueOf(cursor.getInt(6)));
put_list.add(map);
}
cursor.close();
return put_list;
}
SQLite database browser
The first items is becoming null!!! Only last item is stored
[img]
https://drive.google.com/file/d/0B8fPYT_K7J81OXl0ejFiZlpFRDQ/edit?usp=sharing
Please HELP!!!!
I think your problem is this bit of code in add quote():
int i=0;
while(cursor.moveToNext()) {
i=cursor.getInt(0);
}
You're storing the value in the cursor into an int, then overwriting it each time before you do anything with it.
create hashmap object inside of while loop
HashMap<String, String> map = null;
while(cursor.moveToNext())
{
map = new HashMap<String, String>();
map.put("i_item_code", cursor.getString(1));
map.put("i_item_desc", cursor.getString(2));
map.put("i_item_price", String.valueOf(cursor.getInt(3)));
map.put("i_item_qty", String.valueOf(cursor.getInt(4)));
map.put("i_item_tax", String.valueOf(cursor.getFloat(5)));
map.put("i_item_discount", String.valueOf(cursor.getInt(6)));
put_list.add(map);
}

Key value pairs from sqlite to spinner

Could someone show me the correct way to achieve this.
I have setup a hashmap from a sqlite query as follows
public HashMap<String, String> getData(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_DEVICES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("device_name", cursor.getString(1));
user.put("id", cursor.getString(3));
}
cursor.close();
db.close();
// return user
return user;
}
I then want a spinner where the device names are displayed but when selected its the id that is then used for the next action
I was thinking something like this , but I am not sure exactly how.
private void loadLockScreenSpinner() {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
HashMap<String,String> hm = db.getData();
String device_name = hm.get("device_name");
String id = hm.get("id");
ArrayAdapter<HashMap<String, String>> adapter = new ArrayAdapter<HashMap<String,String>>(this, android.R.layout.simple_spinner_item);
adapter.add(hm);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
devicesSpinner.setAdapter(adapter);
devicesSpinner.setWillNotDraw(false);
}
Not sure what to put in the onItemSelected

Android - SQLite delete and update data not working well

I created database with few tables, got 1 table called friend. A friend has few expenses, some expenses might share with another friend. Now i am trying to delete a friend, what i am trying to do is when the friend share expenses with another friend, the expense of the friend that shared with another friend will then added to another friend and the expense will not deleted. Then, the expenses that are not shared with another friend is directly deleted and will not added to any friend since the friend has many expenses. Now the problem is when i trying to add the expense to another friend, its not working. And i am not getting any error.
Here is my code : i think the problem occur inside if(sharerList.size()>1)...., since the rest of the code works well.
public void deleteFriend(String id) {
Log.d(LOGCAT, "delete");
SQLiteDatabase database = this.getWritableDatabase();
ArrayList<HashMap<String, String>> wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM FriendsExpenses WHERE friendId='" + id + "'";
SQLiteDatabase databaseread = this.getReadableDatabase();
Cursor cursor = databaseread.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("expenseId", cursor.getString(0));
wordList.add(map);
} while (cursor.moveToNext());
}
for (int a=0; a<wordList.size();a++){
HashMap<String, String> ValexpenseId = wordList.get(a);
for (Entry<String, String> entry : ValexpenseId.entrySet()) {
String value = entry.getValue();
String selectQuery2 = "SELECT * FROM FriendsExpenses WHERE expenseId='" + value + "'";
SQLiteDatabase databaseread2 = this.getReadableDatabase();
Cursor cursor2 = databaseread2.rawQuery(selectQuery2, null);
ArrayList<HashMap<String, String>> sharerList = new ArrayList<HashMap<String, String>>();
if (cursor2.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("friendId", cursor2.getString(0));
wordList.add(map);
} while (cursor2.moveToNext());
}
else{};
if (sharerList.size() > 1){
String selectQuery3 = "SELECT expenseTotal FROM expenses WHERE expenseId='" + value + "'";
SQLiteDatabase databaseread3 = this.getReadableDatabase();
Cursor cursor3 = databaseread3.rawQuery(selectQuery3, null);
String expenseTotal = null;
if (cursor3.moveToFirst()) {
do {
expenseTotal = cursor3.getString(cursor3.getColumnIndex("expenseTotal"));
} while (cursor3.moveToNext());
}
for (int b=0; b<sharerList.size();b++){
HashMap<String, String> ValfriendId = sharerList.get(b);
for (Entry<String, String> entry2 : ValfriendId.entrySet()) {
String value2 = entry2.getValue();
String currentSpend = currentSpending(value2);
double currentSpending = (Double.parseDouble(currentSpend));
double expTotal = (Double.parseDouble(expenseTotal));
double newSpending = currentSpending + ((expTotal/sharerList.size()) /sharerList.size()-1);
updateSpending(value2, newSpending);
}
}
}
else
{
String deleteQuery = "DELETE FROM expenses where expenseId='" + value + "'";
Log.d("query", deleteQuery);
database.execSQL(deleteQuery);
}
}
}
String deleteQuery = "DELETE FROM friends where friendId='" + id + "'";
String deleteQuery2 = "DELETE FROM FriendsExpenses where friendId='" + id + "'";
Log.d("query", deleteQuery2);
Log.d("query", deleteQuery);
database.execSQL(deleteQuery2);
database.execSQL(deleteQuery);
}
sharerList is empty because you never add any entries.
There are likely to be other copy/paste errors, such as getString(0) with SELECT *.

Android Hashmap return value from other class

i will like to pass the value from DatabaseHandler.java lets say "name" to mainActivity and show it on textview.
DatabaseHandler.java
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
return user;
}
MainActivity.java
TextView lblid;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lblid = (TextView) findViewById(R.id.showid);
HashMap<string, string=""> details = getUserDetails();
String nametext = details.getString("name");
lblid.setText(nametext);
}
I am having trouble writing correct code to make it works, i keep getting "Syntax error on token". Please help me out.
Thanks.
this line cause problem
HashMap<string, string=""> details = getUserDetails();
replace it with
HashMap<string, string> details = getUserDetails();
Usage exaple
Main.java
public class MainActivity extends Activity {
...
private DatabaseHandler dbh;
...
public void onCreate(Bundle) {
dbh = new DatabaseHandler(...);
...
}
...
HashMap<String, String> details = dbh.getUserDetails();
...
String nametext = details.get("name");

Categories

Resources