Hollis Scriber
Hollis Scriber

Reputation: 159

syntax error on tokens, variableDeclarator expected instead

SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
  cv.put(colDeptID, 1);
  cv.put(colDeptName, "Sales");
  db.insert(deptTable, colDeptID, cv);

  cv.put(colDeptID, 2);
  cv.put(colDeptName, "IT");
  db.insert(deptTable, colDeptID, cv);
                    db.close();

With this code, I am getting a red underline under the parenthesis after put in every line and getting this error: Syntax error on token "(", delete this token.

I also get an error that says: Syntax error on tokens, variableDeclarator expected instead. Any ideas? I've tried everything I know to do, and none of it worked.

Edit: I also get Syntax error on "close", identifier expected after this token

I copy and pasted this code from a tutorial and all the comments said it worked, but I've had quite a few problems out of it. Thanks for such quick replies.

Upvotes: 5

Views: 23179

Answers (3)

Akhil Nambiar
Akhil Nambiar

Reputation: 315

Put the code inside a method and not directly inside the class.

Like

SomeRandomMethod(){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
  cv.put(colDeptID, 1);
  cv.put(colDeptName, "Sales");
  db.insert(deptTable, colDeptID, cv);

  cv.put(colDeptID, 2);
  cv.put(colDeptName, "IT");
  db.insert(deptTable, colDeptID, cv);
                    db.close();
}

Upvotes: 0

user2352923
user2352923

Reputation: 47

By Variable Declarator ID, it means something like int or string. Example: int randomnum = 9 or something like that.

Hope this helps!

Upvotes: 0

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

Only declarations, such as:

SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();

can be directly inside a class declaration. Non-declarations, such as:

  cv.put(colDeptID, 1);
  cv.put(colDeptName, "Sales");
  db.insert(deptTable, colDeptID, cv);

must be inside a method, constructor, initializer block, or static initializer block. In this case they should probably be in a constructor or method.

Upvotes: 7

Related Questions