Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Error: Attempt to invoke virtual method on null object reference ‘android.database.Cursor com.myapplication.DatabaseHeleper.getData()’

Posted on November 11, 2022 By

Solution 1 :

It seems that you have to call getReadableDatabase or getWritableDatabase before you need the data, this is because it takes time to create the database.So, you have to be aware of that.

Also, I wanted to remark that you must cursor.moveToFirst() before you do cursor.moveToNext() on sendsms.java

Solution 2 :

Please change your code like this:-

 try {

        Cursor cursor = databaseHelper.getdata();
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

               String num = cursor.getString(0);
               String numm = cursor.getString(1);
               String nummm = cursor.getString(2);

           }
     }

   }
    catch(Exception e){
        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }

Problem :

This the error

I have attached the error above after executing code below mentioned, the code given below is not full code but it targets the main code for which my question is.I am trying to send sms through a variable in which number is stored by fetching from sqlite database. So below code shows that I tried to fetch from dbhelper.java class and store in variable num in sendsms.java class but i think it is not fetching Hence i would request you to see the code and guide where i am wrong for improvement. I hope that now question is clear and sufficient description is given so please help.

sendsms.java


   try {

            Cursor cursor = databaseHelper.getdata();
            while (cursor.moveToNext())

            {

                String num = cursor.getString(0);
                String numm = cursor.getString(1);
                String nummm = cursor.getString(2);

            }

       }
        catch(Exception e){
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }

            public void run() {

                sms.sendTextMessage(num, null, "Help! I've met with an accident at http://maps.google.com/?q=" + String.valueOf(latitude) + "" +
                        "," + String.valueOf(longitude), null, null);

dbhelper.java


    public static final String TABLE_REGISTER = "signin";
    public static final String COL_ID = "USER_ID";
    public static final String COL_NAME = "NAME";
    public static final String COL_PHONE = "PHONE_NUMBER";
    public static final String COL_EMAIL = "EMAIL";
    public static final String COL_PASSWORD = "PASSWORD";
    public static final String COL_CONFIRM_PASSWORD = "CONFIRM_PASSWORD";

    public static final String COL_NAMEone_CON = "NAMEONE";
    public static final String COL_NUMBERone_CON = "NUMBERONE";
    public static final String COL_NAMEtwo_CON = "NAMETWO";
    public static final String COL_NUMBERtwo_CON = "NUMBERTWO";
    public static final String COL_NAMEthree_CON = "NAMETHREE";
    public static final String COL_NUMBERthree_CON = "NUMBERTHREE";

    public SQLiteDatabase db;

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_REGISTER + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , "
                + COL_NAME + " TEXT , " + COL_PHONE + "  LONG  UNIQUE  ," + COL_EMAIL + " VARCHAR UNIQUE," + COL_PASSWORD + " VARCHAR , "
                + COL_CONFIRM_PASSWORD + " VARCHAR ," + COL_NAMEone_CON + " TEXT , "
                + COL_NUMBERone_CON + "  LONG  UNIQUE ," + COL_NAMEtwo_CON + " TEXT ," + COL_NUMBERtwo_CON + "  LONG  UNIQUE , "
                + COL_NAMEthree_CON + " TEXT ," + COL_NUMBERthree_CON + " LONG  UNIQUE " + ")");

    }

    public Cursor getdata(){
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.rawQuery("select NUMBERONE, NUMBERTWO,NUMBERTHREE  from signin ", null);

        return cursor;
    }

Comments

Comment posted by Shalu T D

you are getting crash while executing the code “while (cursor.moveToNext())” ?

READ  [FIXED] android - Execution failed for task ':app:checkDebugDuplicateClasses'. java.lang. Duplicate class com.yqritc.scalablevideoview.BuildConfig
Powered by Inline Related Posts

Comment posted by developer.android.com/reference/android/database/sqlite/…

Sure! I was trying to say that you probably need to call it earlier in your app. Some asynchronous call when you run the app to make sure that the database is created. As the documentation says:

Comment posted by Guillergood

So can you confirm that the db actually has information?

Comment posted by Shalu T D

so your cursor not going to be null rt?

Android Tags:android, android-studio-3.0, database, java, sqlite

Post navigation

Previous Post: [FIXED] How to get Firebase auth token in Android login
Next Post: [FIXED] android – what should be the input and output forom of variables for float object detection model

Related Posts

[FIXED] android – Dagger/MissingBinding when Injecting Dependencies into a ViewModel Android
[FIXED] java – “Choose File” button did not work after I changed permission Android
[FIXED] flutter – How to add windows (desktop) to android studio devices Android
[FIXED] Flutter Development (flutter-dev) › Need emergency help…. I can’t run flutter app in android device and can’t build apk due to gradle issue Android
[FIXED] bash – Wait for download file and then play sound after finish all tasks in Android Gradle Android
[FIXED] android – How to change the color of a RadioGroup in Java Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme