2

Trying to get data from website through HTML parsing.

java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

Why is the array empty?

I included only the class that contains Jsoup because im sure the error is in it. And also i included the HTML part of the website in case you guys wanna take a look at it.

Jsoup Class

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;


public class contentExcluding {



    public String[] createData(final String [] text, final String [] pictures)
    {

        Runnable rMain = new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect("http://egyptianstreets.com/").get();
                    Element content = doc.getElementById("featured-multi-main-img");
                    Elements headlines = content.getElementsByTag("img");
                    Elements img = content.getElementsByAttribute("src");
                    String x = img.toString();
                    pictures[0] = x;
                    text[0] = x;

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
        Thread t = new Thread(rMain);
        t.start();

        return pictures;
    }

}

HTML

<div id="head-wrap" class="left relative">
        <div class="head-wrap-out">
            <div class="head-wrap-in">
                                <div id="featured-multi-wrap" class="left relative">
                                    <div id="featured-multi-main" class="left relative">
                        <a href="http://egyptianstreets.com/2016/08/06/egyptian-rowers-nadia-negm-and-abdel-khalek-elbana-reach-quarter-finals-at-olympics/" rel="bookmark">
                        <div id="featured-multi-main-img" class="left relative">
                                                            <img width="1000" height="512" src="http://egyptianstreets.com/wp-content/uploads/2016/08/pablo-2-1000x512.png" class="unlazy reg-img wp-post-image" alt="pablo (2)" />                             <img width="400" height="240" src="http://egyptianstreets.com/wp-content/uploads/2016/08/pablo-2-400x240.png" class="unlazy mob-img wp-post-image" alt="pablo (2)" />   
Andre
  • 26,751
  • 7
  • 36
  • 80
STOPIMACODER
  • 822
  • 2
  • 7
  • 19

1 Answers1

2

It looks like pictures or text are 0 length arrays.

Check that you initialized them (before call createData method) with a higher size of 0.

Example:

String[] pictures = contentExcluding.createData(new String[1], new String[1]);
Community
  • 1
  • 1
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
  • I understand the error but the problem is that the Jsoup class returns null to the array so its empty. – STOPIMACODER Aug 07 '16 at 01:15
  • 1
    IndexOutOfBoundsException has nothing to do with whether the content is null or not. @Davide is right, you need to initialize your arrays correctly. – Zack Aug 10 '16 at 17:13