Find the all pdf's from webiste -error Exception in thread "main" java.lang.NullPointerException

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Find the all pdf's from webiste -error Exception in thread "main" java.lang.NullPointerException

Santosh Awalekar
package santosh_05_06_2017;
import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.net.MalformedURLException;  
import java.net.URL;  
//import java.util.ArrayList;  
//import java.util.ArrayList;
import java.util.List;  
import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.WebElement;  
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.htmlunit.HtmlUnitDriver; 
public class allpdf {

public static void main(String[] args) throws MalformedURLException, IOException {  
// List<WebElement>  uniqurl = new ArrayList();  
  
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
   driver.navigate().to("http://google.com/"); 
// get all page urls  
List<WebElement>  pdfurllist = driver.findElements(By.tagName("a"));  
  
for (WebElement elemnet : pdfurllist) {  
String pdfurl = elemnet.getAttribute("href").trim();  
//check pdf url  
if (pdfurl.contains(".pdf")) {  
// check one file download one time  
//if (!(uniqurl.contains(pdfurl))) {  
// uniqurl.add(pdfurl);  
//print file name and download url  
System.out.println(filename(pdfurl) + " ---&gt; " + pdfurl);  
//download file  
URL url = new URL(pdfurl);  
InputStream in = new BufferedInputStream(url.openStream());  
OutputStream out = new BufferedOutputStream(new FileOutputStream(filename(pdfurl)));  
  
for (int i; (i = in.read()) != -1;) {  
out.write(i);  
}  
in.close();  
out.close();  
            
 }  
 }  
}  
  
// close driver   
 
  
  
  
  // get file name. It depend on download url pattent.  
  private static String filename(String url) 
  {  
    String filename = "";  
    String file[];  
  
    file = url.split("\\/");  
    if (file[file.length - 1].contains("-")) 
    {  
      filename = file[file.length - 1].trim().replace("-", " ");  
    }
    else if (file[file.length - 1].contains("%20")) {  
      filename = file[file.length - 1].trim().replace("%20", " ");  
    }
    else
    {  
      filename = file[file.length - 1].trim();  
    }  
  
    return filename;  
  }  
  
 
}


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/bc0132cb-a9c0-4219-aa40-7c269895afd3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Find the all pdf's from webiste -error Exception in thread "main" java.lang.NullPointerException

thivyae
Which line of code is throwing null pointer exception?


On Wednesday, June 7, 2017 at 1:55:22 PM UTC+5:30, Santosh Awalekar wrote:
package santosh_05_06_2017;
import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.net.MalformedURLException;  
import java.net.URL;  
//import java.util.ArrayList;  
//import java.util.ArrayList;
import java.util.List;  
import <a href="http://org.openqa.selenium.By" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Forg.openqa.selenium.By\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEfZQPnvt4Ug_Aq50kJ-TWrQ8CZyg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Forg.openqa.selenium.By\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEfZQPnvt4Ug_Aq50kJ-TWrQ8CZyg&#39;;return true;">org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.WebElement;  
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.htmlunit.HtmlUnitDriver; 
public class allpdf {

public static void main(String[] args) throws MalformedURLException, IOException {  
// List<WebElement>  uniqurl = new ArrayList();  
  
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
   driver.navigate().to("<a href="http://google.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://google.com/&#39;;return true;" onclick="this.href=&#39;http://google.com/&#39;;return true;">http://google.com/"); 
// get all page urls  
List<WebElement>  pdfurllist = driver.findElements(By.tagName("a"));  
  
for (WebElement elemnet : pdfurllist) {  
String pdfurl = elemnet.getAttribute("href").trim();  
//check pdf url  
if (pdfurl.contains(".pdf")) {  
// check one file download one time  
//if (!(uniqurl.contains(pdfurl))) {  
// uniqurl.add(pdfurl);  
//print file name and download url  
System.out.println(filename(pdfurl) + " ---&gt; " + pdfurl);  
//download file  
URL url = new URL(pdfurl);  
InputStream in = new BufferedInputStream(url.openStream());  
OutputStream out = new BufferedOutputStream(new FileOutputStream(filename(pdfurl)));  
  
for (int i; (i = in.read()) != -1;) {  
out.write(i);  
}  
in.close();  
out.close();  
            
 }  
 }  
}  
  
// close driver   
 
  
  
  
  // get file name. It depend on download url pattent.  
  private static String filename(String url) 
  {  
    String filename = "";  
    String file[];  
  
    file = url.split("\\/");  
    if (file[file.length - 1].contains("-")) 
    {  
      filename = file[file.length - 1].trim().replace("-", " ");  
    }
    else if (file[file.length - 1].contains("%20")) {  
      filename = file[file.length - 1].trim().replace("%20", " ");  
    }
    else
    {  
      filename = file[file.length - 1].trim();  
    }  
  
    return filename;  
  }  
  
 
}


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/79ca11a5-dade-498f-9134-52fe651acf16%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.