M
M
Myuji2015-03-19 13:49:33
Java
Myuji, 2015-03-19 13:49:33

Why does NPE appear?

package picparcer;
 
 
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import javax.net.ssl.HttpsURLConnection;
 
public class PicParcer {
    
    public static String strMainLink = "https://2ch.hk/b/res/88769997.html";        //начльная ссылка
    public static String strSite = strMainLink.substring(0, strMainLink.indexOf("/", 8));    //сам сайт
    
    public static final String strSourceTag = "<img";
    
    
    public static String strJPG = ".jpg";
    public static String strWEBM = ".webm";
    public static String strPNG = ".png";
    
    
    public static ArrayList<String> strHtml = new ArrayList<>();          //сайт в строках
    public static ArrayList<String> links = new ArrayList<>();            //ссылки на ресурсы
    
    public static Image jpegs;                                                  //файлы
    
    
    
 
    public ArrayList<String> getHTML(String urlToRead) {                                    //получаем сайт
        ArrayList<String> html = new ArrayList<String>();
        URL url;
        HttpsURLConnection conn;
        BufferedReader rd;
        try {
            url = new URL(urlToRead);
            conn = (HttpsURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while(rd.readLine() != null) {
            html.add(rd.readLine());
            }
         
         rd.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return html;
   }
   
   
   public ArrayList<String> poimeyArray(ArrayList<String> htmlArray){     //
       ArrayList<String> arrayPoimey = new ArrayList<String>();
       
       for(int i=0; i<htmlArray.size(); i++){ 
           if(htmlArray.get(i).contains(strSourceTag))                         ///вот тут нпе
               arrayPoimey.add(htmlArray.get(i));
       }
       return arrayPoimey;
   }
   
   
   /*
   public ArrayList<String> spaceDel(ArrayList<String> htmlArray){
        ArrayList<String> spaceDel = new ArrayList<String>();
        char s = ' ';
        char arr[];
        for(int i=0; i<htmlArray.size(); i++){ 
            for(int j=0; i<htmlArray.get(i).toCharArray().length; j++){ 
                arr = htmlArray.get(i).toCharArray().length;
            }
       }
       
   }*/
   
 
   public static void main(String args[]){
       
     PicParcer c = new PicParcer();
     strHtml = c.getHTML(strMainLink);
     links = c.poimeyArray(strHtml);
     
    /*
        for (String strHtml1 : strHtml) {
            System.out.println(strHtml1);
        }*/
        for (String link : links) {
            System.out.println(link);
        }
     
     System.out.println(strHtml.size());
     System.out.println(links.size());
   }
   
}

it starts literally every other time, I'm tired of looking for the cause, help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rainbird, 2015-03-19
@Myuji

And you're missing half the lines...

while(rd.readLine() != null) {    // прочитали 1-ю (3,5,7...) строку и сравнили с null
  html.add(rd.readLine());        // прочитали 2-ю (4,6,8...) строку и добавили в массив
}

And if there were an odd number of rows, the last element of the array will be null...

E
exenza, 2015-03-19
@exenza

Add a null check. JavaDoc , Stack Overflow

String htmlTag = htmlArray.get(i);
if (htmlTag != null && htmlTag.contains(strSourceTag))                         ///вот тут нпе
    arrayPoimey.add(htmlTag);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question