0

I'm new to Java, but I have done some PSP coding in C before. This seems to be quite different.

Below is the stack trace for what I'm seeing:

java.lang.RuntimeException: An error occured while executing doInBackground()
enter code hereenter code here`at android.os.AsyncTask$3.done(AsyncTask.java:299)
enter code here`at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:173)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

And here is the patch of code:

@Override
protected Void doInBackground(Void... arg0) {
    String searchQuery = title.replaceAll("[^ \\w]", "");
    searchQuery = searchQuery.replace(" ", "+");
    String searchResult = httpRun("http://search.azlyrics.com/search.php?q="+searchQuery).toString();
    System.out.println("http://search.azlyrics.com/search.php?q="+searchQuery);
    if(!searchResult.contains("Sorry, no results") && !searchResult.equals("")) {
        String link = searchResult.split("<table width=100%>")[1];
        link = link.substring(link.indexOf("<a href=\""), link.indexOf("\" rel"));
        link = link.replace("<a href=\"", "");

        String lR = httpRun(link).toString();
        if(lR != "" && lR.contains("<!-- start of lyrics -->")) {
            lR = lR.substring(lR.indexOf("<!-- start of lyrics -->"), lR.indexOf("<!-- end of lyrics -->"));
            lR = lR.replace("<!-- start of lyrics -->", "");
            lyrics = lR;
        } else {
            fail = 1;
            lyrics = "Couldn't find lyrics";
        }
    } else {
        fail = 1;
        lyrics = "Couldn't find lyrics";
    }
    return null;
}

I've done some research, and have read that you can't print to the screen within doInBackground - but I'm not sure where to go from here. Any help would be appreciated.

EDIT: java.lang.StringIndexOutOfBoundsException in java.lang.String.startEndAndLength

Heres the new stack:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.StringIndexOutOfBoundsException: length=7338; regionStart=537; regionLength=-538
at java.lang.String.startEndAndLength(String.java:593)
at java.lang.String.substring(String.java:1474)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:174)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
3
  • 3
    omgz! check out this great library for parsing html called jsoup . will save you tons of time! Commented Feb 26, 2013 at 20:57
  • change lR != "" to !lR.equal("") and u should really check out jsoup.org Commented Feb 26, 2013 at 21:16
  • It didnt work unfortunatly Shoshi. Thanks anyways! Commented Feb 26, 2013 at 21:30

2 Answers 2

3

Your split call is returning an array of size 1, but you're trying to get the 2nd item in it (with the [1]). You have to account for the possibility that your split string isn't in the input.

Sign up to request clarification or add additional context in comments.

2 Comments

Ive never had to work with split arrays before. So by changing [1] to 2 it should fix it?
No. The only valid index coming out of that call is 0- the split didn't actually split anything because the delimiter wasn't found. I think you're in for a lot of pain manipulating this one.
0
String link = searchResult.split("<table width=100%>")[1];

The error is occuring there. Indices start at 0, and your split is returning an array of size 1, so element 1 is out of bounds, and hence the exception is thrown. If you change it to String link = searchResult.split("<table width=100%>")[0]; the exception will no longer be thrown.

3 Comments

Its still throwing out an error, let me try and get the stack
Are you sure? If you changed it to: String link = searchResult.split("<table width=100%>")[0]; the following error Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 wouldn't be the same, at the bare minimum it would change the index= value.
Sorry! I copied the wrong apk over. See the new stack, new line of code aswell...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.