lundi 27 avril 2015

How to retrieve data from sqlite and display in text view in android

I want to retrieve data from sqlite and display in text views in android. I had written some code below but it was showing null pointer exception. Please help me with this null pointer exception. thanks in advance this is my code

package com.developer.milanandroid;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AdminSettings extends Activity {
    public LoginDataBaseAdapter logindatabase_adapter_child;
    Button fetch_database;
    TextView text_fetched_database_results;
    String username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_settings);

        fetch_database = (Button)findViewById(R.id.Button_Fetch_Database);
        text_fetched_database_results = (TextView)findViewById(R.id.textView_fetched_database_results);
        fetch_database.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Cursor c = logindatabase_adapter_child.db.rawQuery("select * from MilanloginRegistration", null);
                text_fetched_database_results.setText("");
                c.moveToFirst();
                do{
                    username = c.getString(c.getColumnIndex("USERNAME"));
                    password = c.getString(1);
                    text_fetched_database_results.append("USERNAME::-->"+username+"PASSWORD::-->"+password+"\n");

                }while(c.moveToNext());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.admin_settings, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

This is my login_database_adapter:

package com.developer.milanandroid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

public class LoginDataBaseAdapter 
{
        AdminSettings admin_settings_child;
        static final String DATABASE_NAME = "MilanloginRegistration.db";
        static final int DATABASE_VERSION = 1;
        public static final int NAME_COLUMN = 1;
        // TODO: Create public field for each column in your table.
        // SQL Statement to create a new database.
        static final String DATABASE_CREATE = "create table "+"MilanLoginregistration"+
                                     "( " +"ID"+" integer primary key autoincrement,"+"USERNAME  text UNIQUE,PASSWORD text); ";
        // Variable to hold the database instance
        public  SQLiteDatabase db;
        // Context of the application using the database.
        private final Context context;
        // Database open/upgrade helper
        private DataBaseHelper dbHelper;
        public  LoginDataBaseAdapter(Context _context) 
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public  LoginDataBaseAdapter open() throws SQLException 
        {
            db = dbHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }

        public void insertEntry(String username,String password)
        {
           ContentValues newValues = new ContentValues();

            newValues.put("USERNAME",username);
            newValues.put("PASSWORD",password);

            // Insert the row into your table
            db.insert("MilanLoginregistration",null,newValues);
            ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }
        public int deleteEntry(String username,String password)
        {
            //String id=String.valueOf(ID);
            String where="USERNAME=?";
            int numberOFEntriesDeleted= db.delete("MilanLoginregistration", where, new String[]{username,password}) ;
           // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
            return numberOFEntriesDeleted;
        }   
        public String getSinlgeEntry(String userName)
        {
            Cursor cursor=db.query("MilanLoginregistration", null, " USERNAME=?", new String[]{userName}, null, null, null);
            if(cursor.getCount()<1) // UserName Not Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.moveToFirst();
            String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
            cursor.close();
            return password;                
        }
        public String checkSinlgeEntry(String userName)
        {
            Cursor cursor=db.query("MilanLoginregistration", null, " USERNAME=?", new String[]{userName}, null, null, null);
            if(cursor.getCount()>=1) // UserName  Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.close();
            return "";              
        }
        public void  updateEntry(String user_name,String pasword)
        {
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.  
            updatedValues.put("USERNAME", user_name);
            updatedValues.put("PASSWORD",pasword);


            String where="USERNAME = ?";
            db.update("MilanLoginregistration",updatedValues, where, new String[]{user_name});             
        }   
        /*public void Display(View v){
            Cursor c = db.rawQuery("select * from MilanloginRegistration", null);
            admin_settings_child.text_fetched_database_results.setText("");
            c.moveToFirst();
            do{
                String username = c.getString(c.getColumnIndex("USERNAME"));
                String password = c.getString(1);
                admin_settings_child.text_fetched_database_results.append("USERNAME::-->"+username+"PASSWORD::-->"+password+"\n");

            }while(c.moveToNext());
        }*/
    }

This is my logcat

04-27 13:26:17.040: E/AndroidRuntime(23436): FATAL EXCEPTION: main
04-27 13:26:17.040: E/AndroidRuntime(23436): Process: com.developer.milanandroid, PID: 23436
04-27 13:26:17.040: E/AndroidRuntime(23436): java.lang.NullPointerException
04-27 13:26:17.040: E/AndroidRuntime(23436):    at com.developer.milanandroid.AdminSettings$1.onClick(AdminSettings.java:28)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.View.performClick(View.java:4438)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.View.onKeyUp(View.java:8241)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.widget.TextView.onKeyUp(TextView.java:5682)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.View.dispatchKeyEvent(View.java:7665)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3718)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.os.MessageQueue.nativePollOnce(Native Method)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.os.MessageQueue.next(MessageQueue.java:138)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.os.Looper.loop(Looper.java:123)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at java.lang.reflect.Method.invokeNative(Native Method)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at java.lang.reflect.Method.invoke(Method.java:515)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-27 13:26:17.040: E/AndroidRuntime(23436):    at dalvik.system.NativeStart.main(Native Method)
04-27 13:27:20.620: I/Process(23436): Sending signal. PID: 23436 SIG: 9
04-27 13:27:20.960: D/libEGL(23654): loaded /system/lib/egl/libEGL_VIVANTE.so
04-27 13:27:20.980: D/libEGL(23654): loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so
04-27 13:27:21.070: D/libEGL(23654): loaded /system/lib/egl/libGLESv2_VIVANTE.so
04-27 13:27:21.160: D/OpenGLRenderer(23654): Enabling debug mode 0

Aucun commentaire:

Enregistrer un commentaire