Re: [jnc-users] NullPointerException in HTTPUrlConnection
Status: Beta
Brought to you by:
soapy
|
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
>>
>
>
|