Faizan
Faizan

Reputation: 11

Cannot resolve symbol error | Android Studio Error | ROOM Database

I am working on a notepad application in java. After initializing the database in RoomDB.java class, i am importing the values in my MainDAO.java interface to perform Datebase logics. However, i get following errors in my Problems box of my MainDAO.java file: Here Cannot resolve symbol 'ID' Cannot resolve symbol 'notes' Cannot resolve symbol 'title' Cannot resolve symbol 'notes'

I am still learning to develop apps in android studio so please guide me and let me know the cause of the error. Following are the code files for your reference, let me know if you need anything else to debug this error.

MainDAO.java

package com.example.notepad_colornotes.Database;

import static androidx.room.OnConflictStrategy.REPLACE;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;

import com.example.notepad_colornotes.Models.Notes;


import java.util.List;

@Dao
public interface MainDAO {
    //method to insert data into our database
    @Insert(onConflict = REPLACE)
    void insert(Notes notes);

    //method to get all the data, datatype is list
    @Query("SELECT * FROM notes ORDER BY ID DESC") //this query arranges the items in the notes in descending order. newly added ones will be at the top
    List<Notes> getAll();

    //method to update the data
    @Query("UPDATE notes SET title = :Title, notes = :Notes WHERE ID = :ID") //takes the value from the model and that of the update method parameter
    void update(int ID, String Title, String Notes);

    @Delete
    void delete(Notes notes);
}

RoomDB.java

package com.example.notepad_colornotes.Database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.example.notepad_colornotes.Models.Notes;

@Database(entities = Notes.class, version = 1, exportSchema = false)
public abstract class RoomDB extends RoomDatabase {
    private static RoomDB database;
    private static String DATABASE_NAME = "NoteApp";

    //method to get the instance of the database
    public synchronized static RoomDB getInstance(Context context){
        if (database == null){
            database = Room.databaseBuilder(context.getApplicationContext(),
                    RoomDB.class, DATABASE_NAME)
                    .allowMainThreadQueries()
                    .fallbackToDestructiveMigration()
                    .build();
        }
        return database;
    }

    //instance of DAO
    public abstract MainDAO mainDAO();
}

Notes.java

package com.example.notepad_colornotes.Models;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

import java.io.Serializable;
//Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage

@Entity(tableName = "notes")
@SuppressWarnings("unused")
public class Notes implements Serializable {

    @PrimaryKey(autoGenerate = true) //automatically generates an ID whenever we add an item
    int ID = 0;

    @ColumnInfo(name = "title")
    String Title = "";

    @ColumnInfo(name = "notes")
    String Notes = "";

    @ColumnInfo(name = "date")
    String Date = "";

    @ColumnInfo(name = "pinned")
    boolean Pinned = false;

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        this.Title = title;
    }

    public String getNotes() {
        return Notes;
    }

    public void setNotes(String notes) {
        this.Notes = notes;
    }

    public String getDate() {
        return Date;
    }

    public void setDate(String date) {
        this.Date = date;
    }

    public boolean isPinned() {
        return Pinned;
    }

    public void setPinned(boolean pinned) {
        this.Pinned = pinned;
    }
}

i have tried making the compilersdkversion to 33 as well and i have also rebuilt the project also i have checked the imports and the case sensitivity of the values. still no use

Upvotes: 0

Views: 821

Answers (2)

MikeT
MikeT

Reputation: 57043

I believe that the problems are a red herring i.e. there is no issue. If you compile (Ctrl + F9) you should see that the project builds with no issues e.g.

BUILD SUCCESSFUL in 283ms
31 actionable tasks: 31 up-to-date

Even though:-

enter image description here

To further prove the point consider the following activity code:-

public class MainActivity extends AppCompatActivity {

    RoomDB db;
    MainDAO dao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = RoomDB.getInstance(this);
        dao = db.mainDAO();

        Notes n1 = new Notes();
        n1.setDate("2023-03-31");
        n1.setNotes("Blah");
        n1.setPinned(false);
        n1.setTitle("Note001");

        dao.insert(n1);
        dao.insert(n1);
        dao.update(666,"Not a valid Notes","nothing");
        dao.update(1,"Probably a valid ID for update","something that is not blah!!!!");
        dao.delete(n1);
        for (Notes n: dao.getAll()) {
            Log.d("DBINFO","NoteTitle=" + n.getTitle() + " ID is " + n.getID() + " Date is " + n.getDate() + " Pinned=" + n.isPinned() + " Notes are\n\t" + n.getNotes());
        }
    }
}

When run, then the log includes:-

D/DBINFO: NoteTitle=Note001 ID is 2 Date is 2023-03-31 Pinned=false Notes are
        Blah
D/DBINFO: NoteTitle=Probably a valid ID for update ID is 1 Date is 2023-03-31 Pinned=false Notes are
        something that is not blah!!!!

And viewing the database via App Inspection shows:-

enter image description here

i.e.

  • The two rows have been inserted.
  • The update for the row with an id of 666 (not a row that exists) has not caused an issue.
  • The update for the row with an id of 1 (row does exist) has changed the values acordingly
  • The delete, using the n1 object, has not deleted a row; noting that as the id wasn't set that the id is 0 and thus not an existing row even though n1 was successfully used to insert 2 rows (where the id was generated as the value of the id member of the n1 object was 0).
  • The 2 rows were extracted and confirm that the row with an id of 1 was updated.

Personally I tend to rely upon the build/compile and the resultant build log, rather than the often deceptive problems highlighted by the editor. In addition to errors this will also display warnings

  • note that no warnings were issued for the above.

Upvotes: 0

Nick Pap
Nick Pap

Reputation: 100

I'm not very familiar with the code you used in RoomDB.java since I have only used a very specific way of doing those things. However, not sure if it will change anything, but there are some things you could change in your DAO.

First and foremost, there is a an Update notation in Room, so you don't have to use a Query in order to update your tables.

import androidx.room.Update; //this should go where your other import statements are

@Update
public void updateAgent(Agent agent); //example code from one of my projects - this should go where you currently have the Query for updating the tables

Secondly, as you can see above, I'm declaring public and void for each method. Not sure if this will fix your problem, but it won't hurt to give it a try. (Example code from my project below)

package com.example.excursiontime;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface MyDAO {

    //Agents
    @Insert
    public void addAgent(Agent agent);

    @Query("SELECT * FROM agents")
    public List<Agent> getAgents();

    @Delete
    public void deleteAgent(Agent agent);

    @Update
    public void updateAgent(Agent agent);
}

PS: Sorry for making this an answer. I don't have enough reputation to ask you all of this in a comment. And might as well have it here as an answer so other people can see it, if this indeed fixes your problem.

Upvotes: 0

Related Questions