开发者

Compilation error when comparing two strings with startsWith

I am developing for google app engine with JSP. And I need to compare two strings by startsWith() method in String class.

Here is the code I am working on.

    <% 
            String artist = "Surendra Pere开发者_运维知识库ra";

            DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
            Key songKey = KeyFactory.createKey("songs", 123454);
            // Run an ancestor query to ensure we see the most up-to-date
            // view of the songs.
            Query query = new Query("Song", songKey).addSort("Artist");
            //query.addFilter("Artist", Query.FilterOperator.IN, "Milton Mallawarachchi");
            List<Entity> songsList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(30));
            if(songsList.isEmpty()){
            %>
            <p>There are no songs</p>
            <%
            }else{
%>
<ul class="playlist">
<%
            for(Entity song : songsList){
                if(artist.startsWith(song.getProperty("Artist"))){
%>

 <li><a href="<%= song.getProperty("Link") %>"><%= song.getProperty("Title") %>&#160;&#160;<span class="comment"><%= song.getProperty("Artist") %></span></a></li>

<% }}} %>   

And here is the error i am getting....

HTTP ERROR 500

Problem accessing /search.jsp. Reason: 
    Unable to compile class for JSP: 

An error occurred at line: -1 in the generated java file
    [javac] C:\DOCUME~1\SILICO~1\LOCALS~1\Temp\Jetty_127_0_0_1_8888_war____.g0qk00\jsp\org\apache\jsp\search_jsp.java:178: cannot find symbol
    [javac] symbol  : method startsWith(java.lang.Object)
    [javac] location: class java.lang.String
    [javac]                 if(artist.startsWith(song.getProperty("Artist"))){
    [javac]                          ^
    [javac] 1 error



Stacktrace:

Caused by:
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: -1 in the generated java file
    [javac] C:\DOCUME~1\SILICO~1\LOCALS~1\Temp\Jetty_127_0_0_1_8888_war____.g0qk00\jsp\org\apache\jsp\search_jsp.java:178: cannot find symbol
    [javac] symbol  : method startsWith(java.lang.Object)
    [javac] location: class java.lang.String
    [javac]                 if(artist.startsWith(song.getProperty("Artist"))){
    [javac]                          ^
    [javac] 1 error

Thanks in advance!


The error is telling that it cannot find the given method method startsWith(java.lang.Object) in the String class. Please note that the error says java.lang.Object as method argument. This is indeed wrong. It has to be java.lang.String, see also the javadoc.

You have to change the return type of song.getProperty() from Object to String:

public String getProperty(String name) {
    // ...
}

Or to add a cast on (String) if it is actually of String type:

if (artist.startsWith((String) song.getProperty("Artist"))) {
    // ...
}

Or to use a fullworthy Javabean:

public String getArtist() {
    return artist;
}

with

if (artist.startsWith(song.getArtist())) {
    // ...
}

Last but not least, this problem is unrelated to JSP. You would have exactly the same problem when doing so in a normal Java class. Writing Java code in a JSP file is not the best way to get the basic concepts right.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜