ArulKumar
ArulKumar

Reputation: 35

how to fetch database values and display it into textview in android

intent: i tried many times fetching values from database. but it returns error message please anyone can help me.when i am getting username from database that will shows nullpointer exception.

        submit_btn.setOnClickListener(new OnClickListener() {             String user;
            Cursor cursor;
            @Override
            public void onClick(View v) {
                cursor=database.rawQuery("SELECT * FROM " + "login" + "WHERE" + "username" + "=" +t, null);
                do
                {
                    if(cursor!=null&&cursor.getCount()>0)
                    {
                        user=cursor.getString(0);
                    }
                }while(cursor.moveToNext());
                Log.i(LOGTAG, ""+user);
                String s=t.getText().toString();
                float rate_bar_value=rb.getNumStars();
                System.out.println(":::::::::::::::"+rate_bar_value);
                if(rate_bar_value==5.0)
                {
                    Intent i=new Intent(getApplicationContext(),ToggleActivity.class);
                    startActivity(i);
                    Toast.makeText(getApplicationContext(), "Welcome",Toast.LENGTH_LONG).show();
                    System.out.println(+pass);
                }
                else
                { 

                    System.out.println(+pass);
                    Toast.makeText(getApplicationContext(), "Try Again",Toast.LENGTH_LONG).show();
                    Intent i=new Intent(getApplicationContext(),CarActivity.class);
                    startActivity(i);
                }


            }
        });

    }




    public void onRatingChanged(RatingBar rb, float rating,
               boolean fromTouch)

    {

        final int numStars=rb.getNumStars();

        pass=numStars;
        System.out.println(+pass);

        }
    @Override
    protected void onResume() {

        super.onResume();
        datasource.open();
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        susper.onPause();

    }
    private void createData() {
        Tables tables=new Tables();
        tables.setuser("kumar");
        tables.setpass("kumar");
        Log.i(LOGTAG, "congrates");
    }

                }

I have been working on some sqlite database and i have created one table,and ... inserted data,How to fetch the data and display in a textview? .... How to retrieve data from sqlite database in android and display it in TextView.

error log:

02-21 00:55:38.164: E/AndroidRuntime(1079): FATAL EXCEPTION:
main 02-21 00:55:38.164: E/AndroidRuntime(1079):
Process: com.example.splash, PID: 1079 02-21 00:55:38.164: E/AndroidRuntime(1079): java.lang.RuntimeException: 
    Unable to start activity 
    ComponentInfo{com.example.splash/com.example.splash.LoginActivity}: 
    java.lang.NullPointerException 02-21 00:55:38.164: E/AndroidRuntime(1079): at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 

Upvotes: 0

Views: 985

Answers (1)

Will Richardson
Will Richardson

Reputation: 7960

It looks like your query isn't put together properly:

database.rawQuery("SELECT * FROM " + "login" + "WHERE" + "username" + "=" +t, null);

This will create an SQL statement:

SELECT * FROM loginWHEREusername=null

Which obviously won't return the result you're expecting, which probably leads to your NullPointerException.


Plugging something I made, which makes it easier to do basic queries without SQL. I wouldn't recommend it if you're a beginner.

Upvotes: 2

Related Questions