Thread: [jnc-users] NullPointerException in HTTPUrlConnection
Status: Beta
Brought to you by:
soapy
From: Avi C. <av...@gm...> - 2008-05-27 18:31:30
|
Trying to compile a simple code that creates an HTTP connection: * import* java.net.URL; * import* java.net.URLConnection; * public* *class* PTest { *static* *public* *void* main(String[] args) { URLConnection conn = *null*; *try* { URL url = *new* URL("http://www.apache.org/"); conn = url.openConnection(); *if* (conn == *null*) { System.*out*.println("conn is null"); } conn.connect(); } *catch* (Exception ex) { ex.printStackTrace(); } } } Getting NPE in the connect() part: java.lang.NullPointerException at gnu.java.net.protocol.http.HTTPURLConnection.connect(PTest.exe) at PTest.main(PTest.exe) Any ideas ? using version 1.1.1, standard installation (other stuff work well). Running on XP SP2 and Vista. Thx, Avi |
From: A. 's. A. <str...@gm...> - 2008-07-15 14:18:54
|
Hi, I just had the same problem Avi Cohen reported a couple months ago. Using a url that does not exist leads to a NPE. The code I used for testing is almost the same: import java.net.URL; import java.net.URLConnection; public class URLConnectionTest { static public void main(String[] args) { try { URL u = new URL("http://www.hasdklashdklj.com/"); System.out.println(u); URLConnection c = u.openConnection(); System.out.println(c); c.connect(); System.out.println("done"); } catch (Exception e) { System.out.println("nono"); e.printStackTrace(); } } } Here's the setup: JNC-1.1.1 gcc-122233 Using Windows XP Compiling for Windows Compiling from source Config: Not excluding JCE Not optimizing Not stripping Not packing All the other options are the default (new project) Here's the output: A. exclude JCE, add GNU regex http://www.hasdklashdklj.com/ nono java.lang.NullPointerException at java.net.URLConnection.toString(/usr/local/src/gcc/libjava/classpath/java/net/URLConnection.java:627) at java.io.PrintStream.println(/usr/local/src/gcc/libjava/java/io/PrintStream.java:473) at URLConnectionTest.main(C:/DOCUME~1/andre/CONFIG~1/Temp/ccOsbaaa.jar:0) B. not excluding JCE, not adding GNU regex (does not seem to interfer, tried with and without) http://www.hasdklashdklj.com/ gnu.java.net.protocol.http.HTTPURLConnection:http://www.hasdklashdklj.com/ nono java.lang.NullPointerException <<No stacktrace available>> The output in eclipse is http://www.hasdklashdklj.com/ sun.net.www.protocol.http.HttpURLConnection:http://www.hasdklashdklj.com/ nono java.net.UnknownHostException: www.hasdklashdklj.com at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177) (...) Anybody has a workaround? I can't find a way to check the URL without using URL.openConnection(). []s André. |
From: Marco T. <ma...@mt...> - 2008-07-15 14:24:24
|
Hello André André 'streeto' Amorim wrote: > Hi, I just had the same problem Avi Cohen reported a couple months ago. > Using a url that does not exist leads to a NPE. The code I used for > testing is almost the same: > > import java.net.URL; > import java.net.URLConnection; > > public class URLConnectionTest { > static public void main(String[] args) { > try { > URL u = new URL("http://www.hasdklashdklj.com/"); > System.out.println(u); > URLConnection c = u.openConnection(); > System.out.println(c); > c.connect(); > System.out.println("done"); > } catch (Exception e) { > System.out.println("nono"); > e.printStackTrace(); > } > } > } > > Here's the setup: > > JNC-1.1.1 > gcc-122233 > Using Windows XP > Compiling for Windows > Compiling from source > Config: > Not excluding JCE > Not optimizing > Not stripping > Not packing > All the other options are the default (new project) > > Here's the output: > > A. exclude JCE, add GNU regex > > http://www.hasdklashdklj.com/ > nono > java.lang.NullPointerException > at > java.net.URLConnection.toString(/usr/local/src/gcc/libjava/classpath/java/net/URLConnection.java:627) > at > java.io.PrintStream.println(/usr/local/src/gcc/libjava/java/io/PrintStream.java:473) > at URLConnectionTest.main(C:/DOCUME~1/andre/CONFIG~1/Temp/ccOsbaaa.jar:0) > > > B. not excluding JCE, not adding GNU regex (does not seem to interfer, > tried with and without) > > http://www.hasdklashdklj.com/ > gnu.java.net.protocol.http.HTTPURLConnection:http://www.hasdklashdklj.com/ > nono > java.lang.NullPointerException > <<No stacktrace available>> > > > The output in eclipse is > > http://www.hasdklashdklj.com/ > sun.net.www.protocol.http.HttpURLConnection:http://www.hasdklashdklj.com/ > nono > java.net.UnknownHostException: www.hasdklashdklj.com > <http://www.hasdklashdklj.com> > at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177) > (...) > > > Anybody has a workaround? I can't find a way to check the URL without > using URL.openConnection(). maybe: try { ... } catch(NullPointerException ex) { /* illegal url */ } ? Marco > []s > > André. > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > > > ------------------------------------------------------------------------ > > _______________________________________________ > javaCompiler-users mailing list > jav...@li... > https://lists.sourceforge.net/lists/listinfo/javacompiler-users |
From: A. 's. A. <str...@gm...> - 2008-07-15 21:03:25
|
Marco, catching the NPE will prevent the crash in c.connect(), but the application starts to behave strangely, and then just segfaults. I found a workaround, you can add this line: InetAddress addr = InetAddress.getByName(new URL(" http://bogusurl/asdf/").getHost()); This will cause an UnknownHostException if the host is unreachable. The snippet I sent before becomes, then, import java.net.InetAddress; import java.net.URL; import java.net.URLConnection; public class URLConnectionTest { static public void main(String[] args) { try { System.out.println("first try"); URL u = new URL("http://www.hasdklashdklj.com/"); System.out.println(u); // HACK (GCJ): will throw an UnknownHostException if host is unreachable. @SuppressWarnings("unused") InetAddress addr = InetAddress.getByName(u.getHost()); URLConnection c = u.openConnection(); System.out.println(c); c.connect(); System.out.println("done"); } catch (Exception e) { System.out.println("nono"); e.printStackTrace(); } } } Hope this helps. André. On Tue, Jul 15, 2008 at 11:24 AM, Marco Trudel <ma...@mt...> wrote: > Hello André > > André 'streeto' Amorim wrote: > >> Hi, I just had the same problem Avi Cohen reported a couple months ago. >> Using a url that does not exist leads to a NPE. The code I used for testing >> is almost the same: >> >> import java.net.URL; >> import java.net.URLConnection; >> >> public class URLConnectionTest { >> static public void main(String[] args) { >> try { >> URL u = new URL("http://www.hasdklashdklj.com/"); >> System.out.println(u); >> URLConnection c = u.openConnection(); >> System.out.println(c); >> c.connect(); >> System.out.println("done"); >> } catch (Exception e) { >> System.out.println("nono"); >> e.printStackTrace(); >> } >> } >> } >> >> Here's the setup: >> >> JNC-1.1.1 >> gcc-122233 >> Using Windows XP >> Compiling for Windows >> Compiling from source >> Config: >> Not excluding JCE >> Not optimizing >> Not stripping >> Not packing >> All the other options are the default (new project) >> >> Here's the output: >> >> A. exclude JCE, add GNU regex >> >> http://www.hasdklashdklj.com/ >> nono >> java.lang.NullPointerException >> at >> java.net.URLConnection.toString(/usr/local/src/gcc/libjava/classpath/java/net/URLConnection.java:627) >> at >> java.io.PrintStream.println(/usr/local/src/gcc/libjava/java/io/PrintStream.java:473) >> at >> URLConnectionTest.main(C:/DOCUME~1/andre/CONFIG~1/Temp/ccOsbaaa.jar:0) >> >> >> B. not excluding JCE, not adding GNU regex (does not seem to interfer, >> tried with and without) >> >> http://www.hasdklashdklj.com/ >> gnu.java.net.protocol.http.HTTPURLConnection: >> http://www.hasdklashdklj.com/ >> nono >> java.lang.NullPointerException >> <<No stacktrace available>> >> >> >> The output in eclipse is >> >> http://www.hasdklashdklj.com/ >> sun.net.www.protocol.http.HttpURLConnection:http://www.hasdklashdklj.com/ >> nono >> java.net.UnknownHostException: www.hasdklashdklj.com < >> http://www.hasdklashdklj.com> >> at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177) >> (...) >> >> >> Anybody has a workaround? I can't find a way to check the URL without >> using URL.openConnection(). >> > > maybe: > try { ... } catch(NullPointerException ex) { /* illegal url */ } > ? > > > Marco > > []s >> >> André. >> >> >> ------------------------------------------------------------------------ >> >> ------------------------------------------------------------------------- >> This SF.Net email is sponsored by the Moblin Your Move Developer's >> challenge >> Build the coolest Linux based applications with Moblin SDK & win great >> prizes >> Grand prize is a trip for two to an Open Source event anywhere in the >> world >> http://moblin-contest.org/redirect.php?banner_id=100&url=/ >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> javaCompiler-users mailing list >> jav...@li... >> https://lists.sourceforge.net/lists/listinfo/javacompiler-users >> > > |
From: Marco T. <ma...@mt...> - 2008-07-16 18:10:16
|
Hello André André 'streeto' Amorim wrote: > Marco, catching the NPE will prevent the crash in c.connect(), but the > application starts to behave strangely, and then just segfaults. Whew! In this case there's a serious problem, not just a non-Sun-JRE behavior as I suspected. > I found > a workaround, you can add this line: > > InetAddress addr = InetAddress.getByName(new > URL("http://bogusurl/asdf/").getHost()); Great, thank you! Marco > This will cause an UnknownHostException if the host is unreachable. The > snippet I sent before becomes, then, > > import java.net.InetAddress; > import java.net.URL; > import java.net.URLConnection; > > public class URLConnectionTest { > static public void main(String[] args) { > try { > System.out.println("first try"); > URL u = new URL("http://www.hasdklashdklj.com/"); > System.out.println(u); > > // HACK (GCJ): will throw an UnknownHostException if host is > unreachable. > @SuppressWarnings("unused") > InetAddress addr = InetAddress.getByName(u.getHost()); > > URLConnection c = u.openConnection(); > System.out.println(c); > c.connect(); > System.out.println("done"); > } catch (Exception e) { > System.out.println("nono"); > e.printStackTrace(); > } > } > } > > Hope this helps. > > André. > > On Tue, Jul 15, 2008 at 11:24 AM, Marco Trudel <ma...@mt...> wrote: > > Hello André > > André 'streeto' Amorim wrote: > > Hi, I just had the same problem Avi Cohen reported a couple > months ago. Using a url that does not exist leads to a NPE. The > code I used for testing is almost the same: > > import java.net.URL; > import java.net.URLConnection; > > public class URLConnectionTest { > static public void main(String[] args) { > try { > URL u = new URL("http://www.hasdklashdklj.com/"); > System.out.println(u); > URLConnection c = u.openConnection(); > System.out.println(c); > c.connect(); > System.out.println("done"); > } catch (Exception e) { > System.out.println("nono"); > e.printStackTrace(); > } > } > } > > Here's the setup: > > JNC-1.1.1 > gcc-122233 > Using Windows XP > Compiling for Windows > Compiling from source > Config: > Not excluding JCE > Not optimizing > Not stripping > Not packing > All the other options are the default (new project) > > Here's the output: > > A. exclude JCE, add GNU regex > > http://www.hasdklashdklj.com/ > nono > java.lang.NullPointerException > at > java.net.URLConnection.toString(/usr/local/src/gcc/libjava/classpath/java/net/URLConnection.java:627) > at > java.io.PrintStream.println(/usr/local/src/gcc/libjava/java/io/PrintStream.java:473) > at > URLConnectionTest.main(C:/DOCUME~1/andre/CONFIG~1/Temp/ccOsbaaa.jar:0) > > > B. not excluding JCE, not adding GNU regex (does not seem to > interfer, tried with and without) > > http://www.hasdklashdklj.com/ > gnu.java.net.protocol.http.HTTPURLConnection:http://www.hasdklashdklj.com/ > nono > java.lang.NullPointerException > <<No stacktrace available>> > > > The output in eclipse is > > http://www.hasdklashdklj.com/ > sun.net.www.protocol.http.HttpURLConnection:http://www.hasdklashdklj.com/ > nono > java.net.UnknownHostException: www.hasdklashdklj.com > <http://www.hasdklashdklj.com> <http://www.hasdklashdklj.com> > > at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177) > (...) > > > Anybody has a workaround? I can't find a way to check the URL > without using URL.openConnection(). > > > maybe: > try { ... } catch(NullPointerException ex) { /* illegal url */ } > ? > > > Marco > > []s > > André. > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move > Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win > great prizes > Grand prize is a trip for two to an Open Source event anywhere > in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > <http://moblin-contest.org/redirect.php?banner_id=100&url=/> > > > ------------------------------------------------------------------------ > > _______________________________________________ > javaCompiler-users mailing list > jav...@li... > <mailto:jav...@li...> > https://lists.sourceforge.net/lists/listinfo/javacompiler-users > > > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > > > ------------------------------------------------------------------------ > > _______________________________________________ > javaCompiler-users mailing list > jav...@li... > https://lists.sourceforge.net/lists/listinfo/javacompiler-users |
From: Marco T. <ma...@mt...> - 2008-05-28 08:32:07
|
Hey Avi Avi Cohen wrote: > Trying to compile a simple code that creates an HTTP connection: If you do not exclude JCE, your sample works. Hope that helps Marco > * > > import > > * java.net.URL;* > > import > > * java.net.URLConnection; > > * > > public > > * *class* PTest { > > *static* *public* *void* main(String[] args) { > > URLConnection conn = > > *null*; > > *try* { > > URL url = > > *new* URL("http://www.apache.org/"); > > conn = url.openConnection(); > > *if* (conn == *null*) { > > System. > > /out/.println("conn is null"); > > } > > conn.connect(); > > } > > *catch* (Exception ex) { > > ex.printStackTrace(); > > } > > } > > } > > Getting NPE in the connect() part: > > java.lang.NullPointerException > at gnu.java.net.protocol.http.HTTPURLConnection.connect(PTest.exe) > at PTest.main(PTest.exe) > > Any ideas ? > > using version 1.1.1, standard installation (other stuff work well). > Running on XP SP2 and Vista. > > Thx, > > Avi > > > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > > > ------------------------------------------------------------------------ > > _______________________________________________ > javaCompiler-users mailing list > jav...@li... > https://lists.sourceforge.net/lists/listinfo/javacompiler-users |