Hello ppl,

I am trying to create an application similar to login form where user
can register themselves for username, password, email etc.The
functionality is as follows.
When the user comes to this app it will be ask to either register or
login. In case of user registering himself he enters firstname,
lastname, username, password and email id using EditText.
On hitting the submit button it should be forwarded to another screen
where he sees his details like username, email etc
I am using SQLite for persisting the data and taking help of
SQLiteOpenHelper for the same. It seems that I am able to create
database and table but no able to insert any values in it. The stack
traces shows that I have an unhandled NullPointer exception

The help class called DatabaseHelper is defined as follows

package com.app.login;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.widget.Toast;

public class DatabaseHelper extends SQLiteOpenHelper
{

            public static final int DB_version = 1;
            public static final String DB_name = "User";
                public SQLiteDatabase database;
                public static final String TABLE_NAME = "tbl_users";
                //public static final String TABLE_NAME = "tbl_temp";
                public static final String CREATE_USER_TABLE = "CREATE TABLE
"+TABLE_NAME+"(id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT,
lastname TEXT, username TEXT, password TEXT, email TEXT);";
            LogIn login = new LogIn();
            public DatabaseHelper(Context context, String name, CursorFactory
factory,int version)
            {
                super(context, DB_name, factory,DB_version);
                // TODO Auto-generated constructor stub
            }

            public void onCreate(SQLiteDatabase db)
                {

                        Toast.makeText(login.getApplicationContext(),"In Helper 
executing
onCreate()", Toast.LENGTH_SHORT).show();
                        // Create the table
                db.execSQL("DROP TABLE IF EXISTS tbl_users");
                db.execSQL(CREATE_USER_TABLE);

                }

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
                        // Housekeeping here.
                        // Implement how "move" your application data during an 
upgrade of
schema versions
                        // There is no ALTER TABLE command in SQLite, so this 
generally
involves
                        // CREATING a new table, moving data if possible, or 
deleting the
old data and starting fresh
                        // Your call.
                        //db.execSQL("DROP TABLE IF EXISTS tbl_users");
                        //onCreate(database);
                }

                @Override
                public void onOpen(SQLiteDatabase db) {
                        super.onOpen(db);
                }
        }

--------------------------------------------------------------------------------------------------------------------
and the class extending the activity is as follows
package com.app.login;

//import android.R.array;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

//import java.sql.Date;
import java.util.Arrays;
import java.util.Locale;

//import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
//import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
//import android.database.sqlite.SQLiteQueryBuilder;
//import android.database.sqlite.SQLiteStatement;
import com.app.login.DatabaseHelper;

public class LogIn extends Activity {
        public static final String DEBUG_TAG = "FullDatabase Log";
        public static final int DB_version = 1;
        //public static final String DB_name = "User.db";

        public static final String TABLE_NAME = "tbl_users";
        //public static final String TABLE_NAME = "tbl_temp";
        //public static final String DB_name = "User_temp";
        public static final String DB_name = "User";
        public static final String CREATE_USER_TABLE = "CREATE TABLE
tbl_users (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT,
lastname TEXT, username TEXT, password TEXT, email TEXT);";
        public static final String DROP_USER_TABLE = "DROP TABLE tbl_users;";
        public DatabaseHelper database_helper;
        public SQLiteDatabase database;
        public SQLiteDatabase database_rd;
        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Toast.makeText(this,"Before Bundled",
Toast.LENGTH_SHORT).show();
        createDataBase();

        //LogIn login = new LogIn();
        Button submit = (Button)findViewById(R.id.submit);
        Toast.makeText(this,"after Bundled",
Toast.LENGTH_SHORT).show();

        submit.setOnClickListener(new View.OnClickListener()
        {

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

                                LogIn login = new LogIn();
                                
Toast.makeText(login.getApplicationContext(),"In Helper executing
onCreate()", Toast.LENGTH_SHORT).show();

                                String[] success = insertRecords();
                                if(success[0].equalsIgnoreCase("1"))
                                {
                                        Intent mIntent = new 
Intent(LogIn.this,DisplayUser.class);
                                        
mIntent.putExtra("firstname",success[1]);
                                        mIntent.putExtra("lastname",success[2]);
                                        mIntent.putExtra("username",success[3]);
                                        mIntent.putExtra("password",success[4]);
                                        mIntent.putExtra("email",success[5]);
                                        startActivity(mIntent);
                                }

                        }

                });

        //database.close();
    }

    public void LogCursorInfo(Cursor c)
    {
                Log.i(DEBUG_TAG, "*** Cursor Begin *** " + " Results:" +
c.getCount() + " Columns: " + c.getColumnCount());

                // Print column names
                String rowHeaders = "|| ";
                for (int i = 0; i < c.getColumnCount(); i++) {

                        rowHeaders = rowHeaders.concat(c.getColumnName(i) + " 
|| ");
                }
                Log.i(DEBUG_TAG, "COLUMNS " + rowHeaders);
                //Integer rows = c.getCount();
                //Toast.makeText(this, rows.toString(), 
Toast.LENGTH_SHORT).show();
                // Print records
                c.moveToFirst();
                while (c.isAfterLast() == false) {
                        String rowResults = "|| ";
                        for (int i = 0; i < c.getColumnCount(); i++) {
                                rowResults = rowResults.concat(c.getString(i) + 
" || ");
                        }
                        Log.i(DEBUG_TAG, "Row " + c.getPosition() + ": " + 
rowResults);
                        Toast.makeText(this, rowResults.toString(),
Toast.LENGTH_SHORT).show();
                        //Toast.makeText(this, "In LogCursorInfo",
Toast.LENGTH_SHORT).show();
                        c.moveToNext();
                }
                Log.i(DEBUG_TAG, "*** Cursor End ***");
        }

    void createDataBase()
    {
        if(Arrays.binarySearch(databaseList(), DB_name)>=0)
        {
                deleteDatabase(DB_name);
        }

        database_helper = new
DatabaseHelper(this.getApplicationContext(),DB_name,null,DB_version);
        //database_helper.onCreate(database);
        database = database_helper.getWritableDatabase();
        database.setLocale(Locale.getDefault()); // Set the locale
        database.setLockingEnabled(true); // SQLiteDatabase is made
thread-safe by using locks around critical sections.
        database_rd = database_helper.getReadableDatabase();

        Toast.makeText(this, "created database",
Toast.LENGTH_SHORT).show();
        /*
        database =
openOrCreateDatabase(DB_name,SQLiteDatabase.CREATE_IF_NECESSARY,
null);
        database.setLocale(Locale.getDefault()); // Set the locale
        database.setLockingEnabled(true); // SQLiteDatabase is made
thread-safe by using locks around critical sections.
        database.setVersion(1);

        database.execSQL(CREATE_USER_TABLE);
        */
    }

        String[] insertRecords()
        {
                Toast.makeText(this,"In insertrecords", 
Toast.LENGTH_SHORT).show();
                //database.openDatabase(DB_name,database.create(factory),
ACCESSIBILITY_SERVICE);
                final EditText firstname = 
(EditText)findViewById(R.id.Firstname);
        final EditText lastname = (EditText)findViewById(R.id.Lastname);
        final EditText username = (EditText)findViewById(R.id.Username);
        final EditText password = (EditText)findViewById(R.id.Password);
        final EditText email = (EditText)findViewById(R.id.Email);
        Long newUserID = 0l;

        String fstname  = firstname.getText().toString();
        String lstname  = lastname.getText().toString();
        String usrname  = username.getText().toString();
        String pwd              = password.getText().toString();
        String emailid  = email.getText().toString();

        ContentValues values = new ContentValues();
        values.put("firstname",fstname);
        values.put("lastname",lstname);
        values.put("username",usrname);
        values.put("password",pwd);
        values.put("email",emailid);
                //StringBuilder info = new StringBuilder();
        String[] info = new String[10];


        database.beginTransaction();
        try
        {
                Toast.makeText(this,"Before calling inserting values",
Toast.LENGTH_SHORT).show();
                newUserID = database.insertOrThrow(TABLE_NAME, null, values);
                Toast.makeText(this,"After calling inserting values",
Toast.LENGTH_SHORT).show();

                info[0] = newUserID.toString();
                info[1] = fstname;
                info[2] = lstname;
                info[3] = usrname;
                info[4] = pwd;
                info[5] = emailid;

                System.out.println(newUserID);
                Toast.makeText(this, newUserID.toString(),
Toast.LENGTH_SHORT).show();


                Cursor c = database_rd.query(TABLE_NAME, null, null, null, null,
null,null);
                LogCursorInfo(c);
                c.close();

        }
        catch(Exception e)
        {
                //Transaction failed;
                e.printStackTrace();
        }
        finally
        {
                database.endTransaction();
                database.close();
        }
        return info;
        }
}

----------------------------------------------------------------------------------------------------
and another class for going to next screen is as follows

package com.app.login;

//import java.util.Locale;

import android.app.Activity;
//import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
//import com.app.login.*;
public class DisplayUser extends Activity
{
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                //setContentView(R.layout.userinfo);


                final String DB_name = "User";
                final String TABLE_NAME = "tbl_users";
                final int       DB_version = 1;
                //final  DatabaseHelper database_helper;
                //final SQLiteDatabase database_wr;


                /*
                database =
openOrCreateDatabase(DB_name,SQLiteDatabase.CREATE_IF_NECESSARY,
null);
        database.setLocale(Locale.getDefault()); // Set the locale
        database.setLockingEnabled(true); // SQLiteDatabase is made
thread-safe by using locks around critical sections.
        database.setVersion(1);

        if(database.isOpen())
        {
                Toast.makeText(this, "Yes database is open",
Toast.LENGTH_SHORT).show();
        }
        if(database.isDbLockedByCurrentThread())
        {
                Toast.makeText(this, "Yes database is locked",
Toast.LENGTH_SHORT).show();
        }
        Cursor c = database.query(TABLE_NAME, null, null, null, null,
null,null);

        LogIn login = new LogIn();
        login.LogCursorInfo(c);
                c.close();
                */

                //LogIn login = new LogIn();
                //database_wr = login.database_helper.getWritableDatabase();
                String firstname= getIntent().getStringExtra("firstname");
                String lastname= getIntent().getStringExtra("lastname");
                String email= getIntent().getStringExtra("email");

                Toast.makeText(this, firstname, Toast.LENGTH_SHORT).show();

                View infoPanel = _createInfoPanel(firstname,lastname,email);
                setContentView(infoPanel);


        }
        public View _createInfoPanel(String firstname,String lastname,String
email)
        {
                LinearLayout panel = new LinearLayout(this);
                panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
                panel.setOrientation(LinearLayout.VERTICAL);

          // user id
                TextView Welcome = new TextView(this);
                Welcome.setText("Welcome "+firstname);
                Welcome.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
                Welcome.setLayoutParams(new 
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));

                TextView FirstName = new TextView(this);
                FirstName.setText("Firstname:"+firstname);
                FirstName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
                FirstName.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                TextView LastName = new TextView(this);
                LastName.setText("Lastname:"+lastname);
                LastName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
                LastName.setLayoutParams(new 
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));

                TextView Email = new TextView(this);
                Email.setText("E-mailID:"+email);
                Email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
                Email.setLayoutParams(new 
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));


                panel.addView(Welcome);
                panel.addView(FirstName);
                panel.addView(LastName);
                panel.addView(Email);
                return panel;
        }
}
--------------------------------------------------------------------------------------------
I have been trying to figure out the null pointer exception but unable
to do so
would appreciate if anybody could help me out

the manifest file is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.app.login"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".LogIn"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DisplayUser"  android:label="@string/
app_name">
                <intent-filter>
                <action
android:name="android.intent.action.GET_CONTENT" />

            </intent-filter>
        </activity>


    </application>
    <uses-sdk android:minSdkVersion="4" />

</manifest>
------------------------
and the main.xml is as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:minWidth="2px"/>
<EditText android:text="Firstname" android:id="@+id/Firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="10px"></
EditText>
<EditText android:text="Lastname" android:id="@+id/Lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></EditText>
<EditText android:text="Username" android:id="@+id/Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></EditText>
<EditText android:text="Password" android:id="@+id/Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"></EditText>
<EditText android:text="E-mail" android:id="@+id/Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></EditText>
<Button android:text="submit" android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>




-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to