2

This is my Database.

@Database(entities = {Word.class}, version = 1, exportSchema = false)
    public abstract  class WordsDatabase extends RoomDatabase {
        private static final String DB_NAME = "words";
        private static WordsDatabase database;
        private  static final Object LOCK = new Object();

        public static WordsDatabase getInstance(Context context) {
        synchronized (LOCK){
            if(database == null) {
                database = Room.databaseBuilder(context,WordsDatabase.class, DB_NAME)
                        .createFromAsset("words.db")
                        .allowMainThreadQueries()
                        .build();
                Log.i("1111", "database was created now");
            } else {
                Log.i("1111", "was already created");
            }}
        return database;
    }
    public abstract WordsDao wordsDao();

This is my Dao

@Dao
public interface WordsDao {
    @Query("SELECT * FROM pronouns")
    List<Word> getAllWords();
    @Query("SELECT * FROM pronouns WHERE wordRus == :wordRus")
    Word getWordByWord(String wordRus);

This is my Entity

@Entity(tableName = "pronouns")
public class Word {
    @PrimaryKey
    private int position;
    private String wordRus;
    private String wordHin;
    private String wordDev;
    private int progress;

 1 constructor with all fields, getters, setters...

this is my onCreate()

database = WordsDatabase.getInstance(this);
        Word word = database.wordsDao().getWordByWord("я");
        Log.i("1111", word.getWordRus());

This is my log

I/1111: database was created now
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 

'java.lang.String com.vazheninapps.hindipoliglot.data.Word.getWordRus()' on a 

null object reference

This is my words.db in assets(SQL2 database) SQLSTUDIOIMAGE

what i am doing wrong? Why database null? I also try method getAllWords(). and return size of list. it is 0.

MikeT
  • 51,415
  • 16
  • 49
  • 68
Андрей
  • 166
  • 2
  • 7

2 Answers2

1

I found answer. First, I made SQL3 instead SQL 2, delete app from device and install application. Now Log is:

java.lang.IllegalStateException: Pre-packaged database has an invalid schema: pronouns
         Expected:
        TableInfo{name='pronouns', columns={progress=Column{name='progress', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
         Found:
        TableInfo{name='pronouns', columns={wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, progress=Column{name='progress', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}

========

progress=Column{name='progress', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}
progress=Column{name='progress', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}

wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}

position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}
position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}

wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}

wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}

There are expected and found in wrong order, but it doesn't matter. Matter that where in expected NotNull true and in Found Notnull false. All I done is added to position and progess notNull true in prepopulated database, for match true=true.

Андрей
  • 166
  • 2
  • 7
0

In my case

Simple solve: On phone: long touch on app->App Info->Storage &memory-> Clear Storage

Fortran
  • 2,218
  • 2
  • 27
  • 33