servlet中不允许使用void类型

时间:2014-10-06 14:52:29

标签: java

这是我的java类:

   public String year, title,detail;
public League(String year,String title,String detail)
{
    this.year=year;
    this.title=title;
    this.detail=detail;
}
public void getTitle()
{
    System.out.println(""+year+""+title+""+detail+"");
}

这是我的servlet:

     protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
    //Creats the set of league
    leagueList=new LinkedList();
    leagueList.add(new League("2003","Spring","Soccer League(Spring`03)"));
    leagueList.add(new League("2003","Summer","Summer Soccer Fest"));
    leagueList.add(new League("2003","Fall","Fall Soccer League"));
    leagueList.add(new League("2004","Spring","Summer Soccer Fest"));
    leagueList.add(new League("2004","Summer","Soccer League(Spring`03)"));
    leagueList.add(new League("2004","Fall","Fall Soccer League"));
}

这是我的processRequest()方法

    out.println("<ul>");
        Iterator items=leagueList.iterator();
        while(items.hasNext()){
            out.println("<li>"+League.getTitle()+"</li>");
        }
        out.println("</ul>");

错误是:League.getTitle(); //不允许使用void类型

2 个答案:

答案 0 :(得分:1)

错误非常具有描述性。您返回void或换句话说,getTitle方法不返回任何内容。 Java无法将void(无)的结果连接到String

将方法更改为适当的吸气剂:

public String getTitle() {
    return title;
}

此外,还为其他字段创建正确的getter方法:

//year field should be an int rather than a String
public String getYear() {
    return year;
}

public String getDetail() {
    return detail;
}

答案 1 :(得分:1)

getTitle()不返回任何内容(void),您尝试将其添加到字符串中。

使用此:

public String getTitle()
{
    return year + title + detail;
}