this code generally works (accesses thousands of urls successfully), but once in a while i get a null pointer exception on particular HttpURLConnections, and its always the same urls
for conciseness ive trunkated the code to only up to where the exception was thrown.
why could this possibly be happening?
//takes an array of urls as strings, makes the requests and sends the results into a specified folder import java.net.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.util.regex.Pattern; import java.util.regex.Matcher; public class URLArrayToFiles { public static void main (String[] args) throws Exception { //enter urls here to test String test[] = {"http://www.google.com/images/nav_logo8.png","http://patiscool.000space.com/","http://www.google.com/intl/en_ALL/images/logo.gif","http://www.google.com/#hl=en&source=hp&q=unreal&aq=f&aqi=g10&aql=&oq=&gs_rfai=&fp=84c7fb41710deb10"}; new URLArrayToFiles ("run_from_main\\",test); } //first paramater saving directory, 2nd paramater array of urls as strings URLArrayToFiles (String dirNoSlashes, String[] inAddr) throws Exception { //make directories if not already exist String dirText = "text"; String dirTarget = "target"; (new File(dirNoSlashes + dirTarget + "\\")).mkdirs(); (new File(dirNoSlashes + dirText + "\\")).mkdirs(); (new File(dirNoSlashes + dirTarget + "\\stats\\")).mkdirs(); (new File(dirNoSlashes + dirText + "\\stats\\")).mkdirs(); BufferedWriter targetLog = new BufferedWriter(new FileWriter(dirNoSlashes + dirTarget + "\\" + "stats\\log.txt")); BufferedWriter textLog = new BufferedWriter(new FileWriter(dirNoSlashes + dirText + "\\" + "stats\\log.txt")); //loop encloses whole program, loops through array of addresses for (int i = 0; i < inAddr.length; i++) { //create prefix for file name for chronological order and for results that do not return a name java.text.DecimalFormat nft = new java.text.DecimalFormat("#0000.###"); nft.setDecimalSeparatorAlwaysShown(false); System.out.println("result number: " + nft.format(i)); //try encloses whole loop, many io exceptions possible try { //connect URL u = new URL(inAddr[i]); HttpURLConnection uc = (HttpURLConnection)u.openConnection( ); //get data for verbose output longest wait in program is here int code = uc.getResponseCode( );
java.lang.NullPointerException at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So urce) at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(Unknown So urce) at java.net.HttpURLConnection.getResponseCode(Unknown Source) at URLArrayToFiles.<init>(URLArrayToFiles.java:51) at InputScreen.actionPerformed(InputScreen.java:189) at java.awt.Button.processActionEvent(Unknown Source) at java.awt.Button.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.lang.NullPointerException at sun.net.www.ParseUtil.toURI(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Sour ce) at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So urce) ... 14 more