Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have problem when using my database i got null pointer exception and i cannot solve it any help ?? here my code , i donot know why i get this error but iam sure that throws from add friend method

database.java

    public class frienddatabase extends SQLiteOpenHelper {
        public static String databasename="frienddatabase";
        public static int databaseVersion =6;
        public String tablename="friends";
        public String id="_id";
        public String name="name";
        public String email="email";
        public String phone="phone";
        public String pic="pic";
        public String age="age";
        public String edu="edu";

constructor :

        public frienddatabase(Context context) {
            super(context,databasename,null,databaseVersion);
            // TODO Auto-generated constructor stub
        }



        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String create = "CREATE TABLE "+tablename+"("+id+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+name+" Text unique Not null,"+
            email+" TEXT NOT NULL,"+phone+" TEXT NOT NULL,"+pic+" TEXT,"+age+" TEXT NOT NULL,"+edu+" TEXT NOT NULL"+")";
            db.execSQL(create);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS "+tablename);
            onCreate(db);
        }

and this is the function i try to use and get NullPointer Exception

        public boolean addFriend(String Fname,String Femail,String Fphone,String Fage,String Fedu)
        {
            SQLiteDatabase db=this.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(name, Fname);
            cv.put(email, Femail);
            cv.put(phone, Fphone);
            cv.put(age, Fage);
            cv.put(edu, Fedu);
            db.insert(tablename, pic, cv);
            db.close();
            return true;
        }

i get error when i trying to using addfriend method :here addfriend class i call addfriend method in it

    public class addfriend extends Activity {
        EditText name,email,mobile,age,edu;
            Button save,cancel,addimage;
            frienddatabase helper;
            String namex;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.add);
                name=(EditText)findViewById(R.id.name);
                email=(EditText)findViewById(R.id.email);
                mobile=(EditText)findViewById(R.id.mobile);
                age=(EditText)findViewById(R.id.age);
                edu=(EditText)findViewById(R.id.edu);
                save=(Button)findViewById(R.id.save);
                cancel=(Button)findViewById(R.id.cancel);
                addimage=(Button)findViewById(R.id.addimage);
                helper=new frienddatabase(getApplicationContext());
                        save.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        namex=name.getText().toString();
                        Toast.makeText(getApplicationContext(),namex, Toast.LENGTH_LONG).show();
                        Boolean check = helper.addFriend(name.getText().toString(), email.getText().toString(), mobile.getText().toString(), age.getText().toString(), edu.getText().toString());
                        if(check==null)
                        {
                            Toast.makeText(getApplicationContext(), "null boolean",Toast.LENGTH_LONG).show();
                        }
                        if(check)
                        {
                            Toast.makeText(getApplicationContext(), "new friend added successfull!!", Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "data not saved ", Toast.LENGTH_LONG).show();
                        }
                    }
                });
}
}

here also log cat

06-25 12:53:25.041: W/dalvikvm(1290): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
06-25 12:53:25.061: E/AndroidRuntime(1290): FATAL EXCEPTION: main
06-25 12:53:25.061: E/AndroidRuntime(1290): java.lang.NullPointerException
06-25 12:53:25.061: E/AndroidRuntime(1290):     at com.apk.addfriend$1.onClick(addfriend.java:38)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.view.View.performClick(View.java:3460)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.view.View$PerformClick.run(View.java:13955)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.os.Handler.handleCallback(Handler.java:605)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.os.Looper.loop(Looper.java:137)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at android.app.ActivityThread.main(ActivityThread.java:4340)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at java.lang.reflect.Method.invokeNative(Native Method)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at java.lang.reflect.Method.invoke(Method.java:511)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 12:53:25.061: E/AndroidRuntime(1290):     at dalvik.system.NativeStart.main(Native Method)

any help please ??

share|improve this question
8  
Logcat error pls... – user370305 Jun 25 '12 at 10:19
And relate exception-throwing line with code posted... – m0skit0 Jun 25 '12 at 10:22
This link might be useful stackoverflow.com/questions/6703286/… – Andy Jun 25 '12 at 10:44

1 Answer

you are not inserting ID in your addFriend() method but declare ID in primary key field check this in your code....

share|improve this answer
not my id autoincrement so i do not need pass id to addfriend () thx – Sniver Jun 25 '12 at 10:52
just print and check the data which all you are passing contains any character. – kalandar Jun 25 '12 at 11:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.