02-18 07:41:43.549: ERROR / AndroidRuntime (513): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase (SQLiteOpenHelper.java: 98)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at com.nj.dao.UsersSerive.getUsersByLoginI (UsersSerive.java: 25)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at com.nj.dao.UsersDaoImpl.getUsersByLoginId (UsersDaoImpl.java: 19)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at com.nj.dao.One $ 1.onClick (One.java: 34)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at android.view.View.performClick (View.java: 2408)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at android.view.View $ PerformClick.run (View.java: 8816 )
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at android.os.Handler.handleCallback (Handler.java: 587)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at android.os.Handler.dispatchMessage (Handler.java: 92)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at android.os.Looper.loop (Looper.java: 123)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at android.app.ActivityThread.main (ActivityThread.java: 4627)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at java.lang.reflect.Method.invokeNative (Native Method)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at java.lang.reflect.Method.invoke (Method.java: 521 )
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit . java: 868)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java : 626)
02-18 07:41:43.549: ERROR / AndroidRuntime (513): at dalvik.system.NativeStart.main (Native Method)
This is in the running to SQLiteDatabase sqlite = dbhelper.getWritableDatabase (); This step out of the wrong seek guidance
Here is my code DBHelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private static final String name = "mydata";
private static final int version = 1;
public DBHelper (Context context) {
super (context, name, null, version);
/ / TODO Auto-generated constructor stub
}
@ Override
public void onCreate (SQLiteDatabase db) {
/ / TODO Auto-generated method stub
db.execSQL ("create table if not exists users (id integer primary key autoincrement, loginId varchar (50), password varchar (50))");
db.execSQL ("insert into users (loginId, password) values ('123 ', '123')");
}
@ Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
/ / TODO Auto-generated method stub
db.execSQL ("drop table if exists users");
onCreate (db);
}
}
------ Solution ----------------------------------- ---------
/ / Instantiate your dbhelper class
DBHelper dbhelper = new DBHelper (activity name. This); sqlite = dbhelper.getWritableDatabase ();
------ Solution ------------------------------------ --------
dbhelper is empty chanting, new DBHelper see if there is an exception when
best to initialize it private DBHelper dbhelper = null;
------ Solution -------------------- ------------------------
plus trace to see it.
UsersSerive.java: 25
This is NOT true?
------ Solution ---------------------------------------- ----
you have not been instantiated dbhelper
------ For reference only ---------------------------------- -----
null pointer error, double check your instance
------ For reference only --------------------- ------------------
/ / Instantiate your dbhelper class
DBHelper dbhelper = new DBHelper (activity name. This); SQLiteDatabase sqlite = dbhelper.getWritableDatabase ();
------ For reference only ---------------------------------- -----
my serive code below
public class UsersSerive {
private DBHelper dbhelper;
public UsersSerive (Context context) {
dbhelper = new DBHelper (context);
}
/ / Save
public void save (Users user) {
SQLiteDatabase sqlite = dbhelper.getWritableDatabase ();
sqlite.execSQL ("insert into users (loginid, password) values (?,?)", new Object [] {user.getLoginId (), user.getPassword ()}) ;
}
/ / Find the user name
public Users getUsersByLoginId (String loginId) {
Log.e ("userserive4", loginId);
SQLiteDatabase sqlite = dbhelper.getWritableDatabase ();
Log.e ("userserive", loginId);
Cursor cursor = sqlite.rawQuery ("select * from users where loginId =?", new String [] {loginId});
Log.e ("userserive2", loginId);
if (cursor.moveToNext ()) {
Users user = new Users ();
Log.e ("userserive3", loginId);
user.setId (cursor.getInt (0));
user.setLoginId (cursor.getString (1));
user.setPassword (cursor.getString (2));
return user;
}
return null;
}
}
Chuan is a context where no activity
------ For reference only ------------------------- --------------
that there is an error in the above lines, the piece of code posted Come
------ For reference only ----- ----------------------------------
null pointer exception is the easiest exception, reported where empty take a look at where
------ For reference only ---------------------------------- -----
Thank you very much for replies, these days something could not Login Forum, tomorrow I will go to work tomorrow, the code is posted, ask you greatly enlighten
------ For reference only ---------------------------------------
Below I posted up my DBHelper and UsersSerive and UsersDaoImpl code you greatly look oh
------ For reference only -------------------------- -------------
which instance is not initialized it
------ For reference only ----------------- ----------------------
DBHelper and UsersSerive code above the code given below UsersDaoImpl
package com.nj.dao;
import android.test.AndroidTestCase;
import android.util.Log;
import com.nj.entity.Users;
public class UsersDaoImpl extends AndroidTestCase {
public void save (Users user) {
UsersSerive userSerive = new UsersSerive (this.getContext ());
userSerive.save (user);
}
public Users getUsersByLoginId (String loginId) {
Log.e ("userDaoImpl", loginId);
UsersSerive userSerive = new UsersSerive (this.getContext ());
Users user = new Users ();
Log.e ("userDaoImpl2", loginId);
user = userSerive.getUsersByLoginId (loginId);
return user;
}
}
------ For reference only --------------------------------- ------
above being given UsersSerive.java: 25 is the line: SQLiteDatabase sqlite = dbhelper.getWritableDatabase ();
UsersDaoImpl.java: 19 is: user = userSerive.getUsersByLoginId (loginId);
One.java: 34 yes: Users user = userDao.getUsersByLoginId (loginId.getText (). toString ());
are ultimately dbhelper.getWritableDatabase wrong, the return should be empty seeking advice
------ For reference only ---------------------------------- -----
[code = Jav] dbhelper = new DBHelperUtil (context);
db = dbhelper.getWritableDatabase (); [/ code]
------ For reference only ------------------- --------------------
[code = Jav]
dbhelper = new DBHelperUtil (context);
db = dbhelper.getWritableDatabase ();
[/ code]
------ For reference only ------------------------------ ---------
Upstairs positive solution!
------ For reference only -------------------------------------- -
uh reconstruction method in my way when instantiated
private DBHelper dbhelper = null;
public UsersSerive (Context context) {
dbhelper = new DBHelper (context);
}
------ For reference only --------------------------------- ------
not instantiate the object. . .
------ For reference only -------------------------------------- -
Will not solved? I also encountered dbhelper not empty, but db = dbhelper.getWritableDatabase () when it reported this null pointer exception, db is null;
------ For reference only ---------------------------------- -----
I also encountered this problem, solve it,
------ For reference only ------------------- --------------------
I am also in the constructor, which has been declared a variable, in the end the problem lie? ?
------ For reference only -------------------------------------- -
I have encountered this problem, how to solve, anxious, tangled!
Hello, all is going perfectly here and ofcourse
回复删除every one is sharing data, that's genuinely excellent, keep up writing.
Here is my web site ... worms 3 cheat tool
Java Lovers: Null Pointer Java.Lang.Nullpointerexception, Seek Expert Guidance >>>>> Download Now
回复删除>>>>> Download Full
Java Lovers: Null Pointer Java.Lang.Nullpointerexception, Seek Expert Guidance >>>>> Download LINK
>>>>> Download Now
Java Lovers: Null Pointer Java.Lang.Nullpointerexception, Seek Expert Guidance >>>>> Download Full
>>>>> Download LINK V4