arjun arjun - 16 days ago 5
Android Question

Android SQLite NullPointer Exception and stack overflow exception

NUllpointer Exception and stack overflow exception on line which has has sql query.

display.java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class display extends Activity {
private Context context;
MyDBHandler helper

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
helper = new MyDBHandler(context);

Bundle nameValue=getIntent().getExtras();
String vname=nameValue.getString("Name");
TextView tvname=(TextView)findViewById(R.id.TVname);
tvname.setText(vname);

String dispname=helper.onSelect(vname);
TextView tvaddress=(TextView)findViewById(R.id.TVaddress);
tvaddress.setText(dispname);


}
/*public void onDisplay(){
Bundle nameValue=getIntent().getExtras();
if(nameValue==null){

}
String vname=nameValue.getString("Name");
String dispname=helper.onSelect(vname);
TextView tvaddress=(TextView)findViewById(R.id.TVaddress);
tvaddress.setText(dispname);
}*/




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


MyDBHandler

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productDB.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_ADDRESS = "address";


private final Context myContext;
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}


@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_ADDRESS + " TEXT " +
");";
db.execSQL(query);
String query1="INSERT INTO"+ TABLE_PRODUCTS + "(name,address) VALUES
('name1,address1');";
String query2="INSERT INTO"+ TABLE_PRODUCTS + "(name,address) VALUES
('name2,address2');";
db.execSQL(query1);
db.execSQL(query2);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}

public String onSelect(String sname){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String displayname= "SELECT address FROM"+ TABLE_PRODUCTS + " WHERE "+


COLUMN_NAME + "=\""+ sname + "\";";
Cursor c = db.rawQuery(displayname, null);

if (c.getString(c.getColumnIndex("address")) != null) {
dbString = c.getString(c.getColumnIndex("address"));
}
db.close();
return dbString;
}

}



NullPointerExcepion


E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arjun.tablesqlite/com.example.arjun.tablesqlite.display}: java.lang.NullPointerException
at com.example.arjun.tablesqlite.MyDBHandler.onSelect(MyDBHandler.java:57)
at com.example.arjun.tablesqlite.display.onCreate(display.java:27)


I'm getting NullPointerException at this line in MyDbHandler.java

SQLiteDatabase db = getWritableDatabase();


and at this line in display.java....

String dispname=helper.onSelect(vname);

Answer

Please try below onSelect Method and let me know any error in it:-

  public String onSelect(String sname) {
        String dbString = "";
        SQLiteDatabase db = this.getWritableDatabase();
        String displayname = "SELECT address FROM" + TABLE_PRODUCTS + " WHERE " +


                COLUMN_NAME + "='" + sname + "'";
        Cursor c = db.rawQuery(displayname, null);

/*
        if (c.getString(c.getColumnIndex("address")) != null) {
            dbString = c.getString(c.getColumnIndex("address"));
        }
*/
        if (c.moveToFirst()) {
            if (c.getString(c.getColumnIndex("address")) != null) {
                dbString = c.getString(c.getColumnIndex("address"));
            }else{
                dbString="No Address";
            }
        }else{
            dbString="No Address Found";
        }
        db.close();
        return dbString;
    }

And this:-

MyDBHandler helper = new MyDBHandler(this);

Change this line like below

String query1="INSERT INTO "+ TABLE_PRODUCTS + "(name,address) VALUES   
    ('name1','address1');";
String query2="INSERT INTO "+ TABLE_PRODUCTS + "(name,address) VALUES   
    ('name2','address2')";

Your database query is like below(for reference):-

INSERT INTO products (name,address) VALUES ('name1','address1');

and your query build like this:-

INSERT INTOproducts (name,address) VALUES ('name1','address1');

so you have get below error.

Comments