Java 类型不匹配无法从元素类型对象转换为字符串

Java 类型不匹配无法从元素类型对象转换为字符串,java,string,object,mismatch,Java,String,Object,Mismatch,当我在代码中创建搜索方法来搜索字符串时,我总是遇到这个错误。我已经通过了很多例子试图解决这个问题,但我找不到任何。感谢您提供的任何帮助和建议 public class runNote { public static void main(String[] args) { // TODO Auto-generated method stub Notebook note = new Notebook(); note.storeNote("happy"); note.s

当我在代码中创建搜索方法来搜索字符串时,我总是遇到这个错误。我已经通过了很多例子试图解决这个问题,但我找不到任何。感谢您提供的任何帮助和建议

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.
    }
}
}
公共类运行说明{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
笔记本笔记=新笔记本();
注:storeNote(“快乐”);
note.storeNote(“你好”);
注:storeNote(“5点工作”);
注:storeNote(“烧烤时间”);
注:storeNote(“UNI!!!!”);
note.storeNote(“不要错过9:15的讲座”);
System.out.println(note.numberOfNotes());
注.注释(1);
注:搜索注释(“hap”);
}}
公开课笔记本{
/**
*执行系统所需的任何初始化
*笔记本。
*/
公用记事本(
{
notes=新的ArrayList();
}
/**
*将新便笺存入笔记本。
*@param note要存储的注释。
*/
公共无效存储注释(字符串注释)
{
注:增加(注);
}
/**
*@返回笔记本中当前的笔记数。
*/
公共int numberOfNotes()
{
returnnotes.size();
}
/**
*一个简单的搜索引擎,可以找到正确的笔记。
*/
公共void searchNotes(字符串搜索){
用于(字符串项:注释){
如果(项包含(搜索)){
系统输出打印项次(项);
}
}
}
/**
*出示一张便条。
*@param noteNumber要显示的注释的编号。
*/
公共无效显示注释(内部注释编号)
{
如果(注意编号<0){
//这不是有效的便笺号码,因此请不要执行任何操作。
}
else if(noteNumber
将注释作为全局变量添加到类中

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

....
}
公共课堂笔记本{
ArrayList notes=null;
....
}
在构造函数中,执行以下操作:

public Notebook()
{
    notes = new ArrayList<String>();
}
公共笔记本()
{
notes=新的ArrayList();
}

您在这一行遇到异常:

 for (String item : notes)
notes
是一个声明为原始类型的
ArrayList
,虽然我没有看到声明,但我可以看到这一行初始化:

notes = new ArrayList();
它将其元素视为
对象的类型
您需要使用
ArrayList
类型声明
notes
,例如:

  ArrayList<String> notes = new ArrayList<>();
ArrayList notes=new ArrayList();

试试这个:
notes=newarraylist()

您没有声明notes变量

public class Notebook{
  private ArrayList<String> notes;
...
公共课堂笔记本{
私人数组列表注释;
...

粘贴准确完整的错误消息,并告诉我们它指的是哪一行。
notes
的声明在哪里?当您应该将其声明为
ArrayList
时,您是否将其声明为
ArrayList
?错误:线程“main”中出现异常java.lang.Error:未解决的编译问题:类型不匹配:无法将lab8.Notebook.searchNotes(Notebook.java:47)中的元素类型对象转换为lab8.runNote.main(runNote.java:16)中的字符串。notes的声明位于公用笔记本中(){notes=new ArrayList();}这是一个成员变量,不是全局变量。这是Java 7中的默认构造函数,您不需要使用new@Code-Guru,谢谢,实际上完成了不同的异常:);)如果我更改声明,我会得到以下错误:线程“main”java.lang.NullPointerException中的异常位于lab8.Notebook.storeNote(Notebook.java:32)在lab8.runNote.main(runNote.java:8)中,您是否已初始化了如上所述的
notes
列表?