NUllpointer异常和堆栈溢出异常在线,它有sql查询。
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)
我在MyDbHandler.java的这一行得到NullPointerException
SQLiteDatabase db = getWritableDatabase();
并在display.java ....
的这一行String dispname=helper.onSelect(vname);
答案 0 :(得分:2)
试试这个,
您尚未初始化上下文,并且您已尝试使用该上下文初始化DBHandler,因此它正在抛出NLP。
public class display extends Activity {
MyDBHandler helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
helper = new MyDBHandler(this);
.... //your code
}
答案 1 :(得分:1)
请在下面选择onSelect Method并告诉我任何错误: -
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;
}
这个: -
MyDBHandler helper = new MyDBHandler(this);
更改此行,如下所示
String query1="INSERT INTO "+ TABLE_PRODUCTS + "(name,address) VALUES
('name1','address1');";
String query2="INSERT INTO "+ TABLE_PRODUCTS + "(name,address) VALUES
('name2','address2')";
您的数据库查询如下(供参考): -
INSERT INTO产品(名称,地址)VALUES('name1','address1');
并且您的查询构建如下: -
INSERT INTOproducts (姓名,地址)VALUES('name1','address1');
所以你得到了以下错误。