0

Hi I'm new to android programming, and i don't know or i really can't figure out why do i get this error message after i click the button on the app that i created

java.lang.NullPointerException at com.example.dailydoseofhappiness.MainActivity.searchRecord(MainActivity.java:62) at com.example.dailydoseofhappiness.MainActivity.searchRecord(MainActivity.java:52)

here is the code fragment.

public void onClick(View arg){ 
     if(arg.getId()==R.id.btnfortune){ //this is line 52
         searchRecord(count);
     }

 }

public void searchRecord(int count)
throws SQLException
{
    Cursor rsCursor;
    String [] rsFields = {"mesNum","Message"}; // this is line 62
    rsCursor = dbm.dbase.query("MessageFile", rsFields, "mesNum = " + count, null,       null, null, null, null);
    rsCursor.moveToFirst();

    if(rsCursor.isAfterLast()==false){

     lblmessageS.setText(rsCursor.getString(0));
    }
    rsCursor.close();
     }

can anyone check this code fragment if whats wrong thank you very much

Liam
  • 27,717
  • 28
  • 128
  • 190
Ako Si Emjey
  • 3
  • 1
  • 2
  • Is the dbm object initialized anywhere? – Geneb Mar 07 '13 at 15:10
  • yes sir. it is initialized above. which has been instantiated from DatabaseManager – Ako Si Emjey Mar 07 '13 at 15:12
  • What about rsCursor? (Sorry, should have asked this in the previous comment) – Geneb Mar 07 '13 at 15:14
  • You're off by one, btw, line 52 is the call to searchRecord and 62 is the `query` method of dbm.dbase. – KevinDTimm Mar 07 '13 at 15:14
  • @Geneb yes sir.. its also been initialized. – Ako Si Emjey Mar 07 '13 at 15:18
  • @KevinDTimm dbm sir is actually an instantiation of DatabaseManager class that i created. and also you mentioned that i'm off by one, can you tell me the solution for that sir? thank you for your reply Sir Geneb and Sir KevinDTimm – Ako Si Emjey Mar 07 '13 at 15:19
  • possible duplicate of [SQLIteDatabase.query method](http://stackoverflow.com/questions/10600670/sqlitedatabase-query-method) – KevinDTimm Mar 07 '13 at 15:22
  • The only other thing I can think of would be that dbm.dbase is null or dbm.dbase.query is returning null. You said you created the DatabaseManager class, could you provide the .query method? – Geneb Mar 07 '13 at 15:32
  • yeah.. i think its null, actually its my fault though, i'm trying to add values into the table without using an entry form, i'm trying to add record via backend. maybe thats the problem. by the way is there a possibility that i can do that, coz i'm using this SQLite database browser but i dont know if i'm doing it correctly – Ako Si Emjey Mar 07 '13 at 16:05

1 Answers1

0

Try this searchRecord method. I added some silly checks so if any variable is null it will warn you:

public void searchRecord(int count) throws SQLException {
    Cursor rsCursor;
    String [] rsFields = {"mesNum","Message"}; // this is line 62

    if (dbm == null)
        throw new Exception("dbm object is null");

    if (dbm.dbase == null)
        throw new Exception("dbm.dbase is null");

    rsCursor = dbm.dbase.query("MessageFile", rsFields, "mesNum = " + count, null, null, null, null, null);

    if (rsCursor == null)
        throw new Exception("rsCursor is null");

    rsCursor.moveToFirst();

    if (rsCursor.isAfterLast()==false){
        if (lblmessageS == null)
            throw new Exception("lblmessageS is null");

        lblmessageS.setText(rsCursor.getString(0));
    }

    rsCursor.close();
 }
orique
  • 1,295
  • 1
  • 27
  • 36
  • Thank you sir orique, theres no more error in the code however. nothing will happen if i will click on the button. – Ako Si Emjey Mar 07 '13 at 15:46
  • @AkoSiEmjey I think some exception is being thrown and you aren't handling it in a proper way. There should be an exception in the log telling you which variable is null. Anyway, if my answer has been somewhat useful could you accept or/and upvote it? – orique Mar 07 '13 at 15:54