开发者

How to use a for loop inside a try catch block

开发者 https://www.devze.com 2023-04-06 02:18 出处:网络
Hello I am trying to use Jsoup to create an ArrayList of images pulled from all jpgs on a site. I am encountering nullPointerException as if the for loop is not there.

Hello I am trying to use Jsoup to create an ArrayList of images pulled from all jpgs on a site. I am encountering nullPointerException as if the for loop is not there.

try {
    Document doc = Jsoup.connect("www.enterwebsitehere.com").get();
    jpgs = doc.select("img[src$=.jpg]");

    for (int countPics = 0; countPics > 10; countPics++) {
        Element currentPic = jpgs.get(countPics);

        String currentPicString = currentPic.toString();
        int startofAddress = currentPicString.indexOf("http:");
        int endofAddress = (currentPicString.indexOf(".jpg") + 4);
        String urlOfImage = currentPicString.substring(startofAddress, endofAddress);

        URL url = new URL(urlOfImage);
  开发者_Go百科      Image currentImage = ImageIO.read(url);
        imageList.add(currentImage);
    }


} catch (MalformedURLException e) {
} catch (Exception e) {
    e.printStackTrace();
}

I believe I need to bring some variables outside of the catch block?

Thankyou for any help you can offer.

EHarpham


CountPics >10 is evaluating to false. (0 > 10) is false.


I believe you meant : for (int countPics = 0; countPics < 10; countPics++)

Note the reversal of countPics > 10 to countPics < 10

0

精彩评论

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

关注公众号