2

I keep getting this error when making a search method in my code to search through string. I have gone through alot of examples trying to fix this but I cant find any. Thank you for any help and advise you can give.

public class runNote {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Notebook note = new Notebook();
    note.storeNote("happy");
    note.storeNote("hello there");
    note.storeNote("work at 5");
    note.storeNote("BBQ Time");
    note.storeNote("UNI!!!!");
    note.storeNote("Dont miss lecture at 9:15");
    System.out.println(note.numberOfNotes());
    note.showNote(1);
    note.searchNotes("hap");

}}




public class Notebook{


/**
 * Perform any initialization that is required for the
 * notebook.
 */
public Notebook()
{
    notes = new ArrayList();
}

/**
 * Store a new note into the notebook.
 * @param note The note to be stored.
 */
public void storeNote(String note)
{
    notes.add(note);
}

/**
 * @return The number of notes currently in the notebook.
 */
public int numberOfNotes()
{
    return notes.size();
}

/**
 * A simple search engine to find the correct notes.
 */
public void searchNotes(String search){ 
    for (String item : notes){
        if (item.contains(search)){
        System.out.println(item);
        }
    }
}

/**
 * Show a note.
 * @param noteNumber The number of the note to be shown.
 */
public void showNote(int noteNumber)
{
    if(noteNumber < 0) {
        // This is not a valid note number, so do nothing.
    }
    else if(noteNumber < numberOfNotes()) {
        // This is a valid note number, so we can print it.
        System.out.println(notes.get(noteNumber));
    }
    else {
        // This is not a valid note number, so do nothing.
    }
}
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
user202051
  • 173
  • 2
  • 5
  • 17
  • 1
    Paste the exact and complete error message, and tell us which line it refers to. – JB Nizet Dec 07 '13 at 19:16
  • 2
    Where is the declaration for `notes`? Are you declaring it as `ArrayList` when you should declare it as `ArrayList`? – Code-Apprentice Dec 07 '13 at 19:24
  • Error: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from element type Object to String at lab8.Notebook.searchNotes(Notebook.java:47) at lab8.runNote.main(runNote.java:16) – user202051 Dec 07 '13 at 19:37
  • The declaration for notes is in the public Notebook() { notes = new ArrayList(); } – user202051 Dec 07 '13 at 19:37

4 Answers4

5

You are having exception at this line :

 for (String item : notes)

notes is an ArrayList declared as raw type, although i don't see the declaration i can see this line of initialization:

notes = new ArrayList();

which sees it's element as of type Object You need to declare notes with ArrayList<String> type such as:

  ArrayList<String> notes = new ArrayList<>();
Sage
  • 15,290
  • 3
  • 33
  • 38
  • If I change the declaration I get this error: Exception in thread "main" java.lang.NullPointerException at lab8.Notebook.storeNote(Notebook.java:32) at lab8.runNote.main(runNote.java:8) – user202051 Dec 07 '13 at 19:39
  • have you initialized `notes` list as i described above ? – Sage Dec 07 '13 at 19:46
4

Add notes as a global variable to your class

public class Notebook{
ArrayList<String> notes = null;

....
}

In the constructor, do:

public Notebook()
{
    notes = new ArrayList<String>();
}
openmike
  • 272
  • 1
  • 9
0

try this: notes = new ArrayList<String>();

Palejandro
  • 2,092
  • 5
  • 26
  • 38
0

You don't declare the notes variable

public class Notebook{
  private ArrayList<String> notes;
...
hrv
  • 925
  • 6
  • 13