1

I'm getting an error "cannot find symbol method add(java.util.Date)", although what I'm passing it was declared a Date. What am I missing?

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.*;


class Entry {
    Date date;

    Entry(Date aDate) {
        date = aDate;
    }
}

public class td {
    public static void main(String[] args) { 

        List<Entry> entries = new ArrayList<Entry>();

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date aDate = df.parse("2011-02-27"); // Date aDate = new Date() also fails

        entries.add(aDate);

        System.out.println(entries.get(0));
    }
}
Raedwald
  • 46,613
  • 43
  • 151
  • 237
foosion
  • 7,619
  • 25
  • 65
  • 102
  • A more specific case of the general question http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean – Raedwald Feb 26 '16 at 20:09

2 Answers2

6

Are you sure you want not entries.add(new Entry(aDate)); ? It seems to be the purpose of Entry class.

And generally speaking, if you declare list as List<Entry>, you should store Entry instances in it, not Date.

Also, your error says "cannot find symbol method add(java.util.Date)" . So, it's not Date class that's missing. It's add(java.util.Date) method.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
  • Sigh. You're right. The compiler also insists that I wrap the df.parse statement with try & catch. While I'm at it, I should have entries.get() return something sensible. – foosion Feb 28 '11 at 19:30
  • @foosion Instead of wrapping, it might be easier to add `throws Exception` to the `main` method declaration. Since you can't do anything useful with error, it's better to just let it pass. – Nikita Rybak Feb 28 '11 at 19:32
  • That is easier. It also prevents "not initialized" errors if I don't declare and initialize aDate outside the try/catch – foosion Feb 28 '11 at 19:34
0

To re-iterate: List has add(Entry) method and doesn't have add(Date) method.

iluxa
  • 6,941
  • 18
  • 36