简体   繁体   中英

NullPointerException when making HTTPS call

I've been scratching my head on this issue for the last few days now. I'm trying to POST data to a server which has an SSL certificate installed.

I've been using the EasySSLSocketFactory and the EasyX509TrustManager classes found here ( http://code.google.com/p/transdroid/source/browse/trunk/src/org/xmlrpc/android/?r=103 ) in conjunction with the following code:

StringEntity se = null;
try {
    se = new StringEntity(tripArray.toString());
} catch (UnsupportedEncodingException e) { }

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

HttpParams params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);
this.httpClient = new DefaultHttpClient(cm, params); 

HttpPost httpPost = new HttpPost("https://xxx");
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

ResponseHandler responseHandler = new BasicResponseHandler();
try {
    String response = this.httpClient.execute(httpPost, responseHandler).toString();
} catch (ClientProtocolException e) {
    tripFail = true;
} catch (IOException e) {
    tripFail = true;
}

But I seem to be getting the following back from my requests:

Thread [<10> Timer-1] (Suspended (exception NullPointerException))  
    Class.forName(String, boolean, ClassLoader) line: 234   
    Class.forName(String) line: 181 
    X509Certificate.<clinit>() line: 60 
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).verifyCertificateChain(byte[][], String) line: 658  
    NativeCrypto.SSL_do_handshake(int, FileDescriptor, NativeCrypto$SSLHandshakeCallbacks, int, boolean) line: not available [native method]    
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).startHandshake(boolean) line: 474   
    OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl) line: 750    
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).getInputStream() line: 692  
    SocketInputBuffer.<init>(Socket, int, HttpParams) line: 98  
    DefaultClientConnection(SocketHttpClientConnection).createSessionInputBuffer(Socket, int, HttpParams) line: 83  
    DefaultClientConnection.createSessionInputBuffer(Socket, int, HttpParams) line: 170 
    DefaultClientConnection(SocketHttpClientConnection).bind(Socket, HttpParams) line: 106  
    DefaultClientConnection.openCompleted(boolean, HttpParams) line: 129    
    DefaultClientConnectionOperator.openConnection(OperatedClientConnection, HttpHost, InetAddress, HttpContext, HttpParams) line: 171  
    SingleClientConnManager$PoolEntry(AbstractPoolEntry).open(HttpRoute, HttpContext, HttpParams) line: 164 
    SingleClientConnManager$ConnAdapter(AbstractPooledConnAdapter).open(HttpRoute, HttpContext, HttpParams) line: 119   
    DefaultRequestDirector.execute(HttpHost, HttpRequest, HttpContext) line: 359    
    DefaultHttpClient(AbstractHttpClient).execute(HttpHost, HttpRequest, HttpContext) line: 555 
    DefaultHttpClient(AbstractHttpClient).execute(HttpHost, HttpRequest, ResponseHandler, HttpContext) line: 653    
    DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest, ResponseHandler, HttpContext) line: 627   
    DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest, ResponseHandler) line: 616    
    DataSender.sendData() line: 151 
    DataSender$1.run() line: 202    
    Timer$TimerImpl.run() line: 284 

Does anyone know what could be causing this? I'm a bit stumped on this..

Edit:

I think the issue lies here

Class.forName(String, boolean, ClassLoader) line: 234   
    Class.forName(String) line: 181 

The code seems to be passing a null string to the Class.forName() function, causing it to bail out.. But I'm not sure what's calling that and why.

Thanks, Oli

After a long discussion the solution is

use IntelliJ IDEA ;-)

Looks like Eclipse was causing the issue for me. As gabe suggested, Intellij idea community edition compiles the code fine and doesn't cause the NullPointerException. I have no idea what could be causing it and no idea how to fix it. I'm just going to use a different IDE from now on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM