获取变量的值null
这是我在此代码中的代码,try 块内的值我获取了 httpconn = null 的值,但在第一行中我将该值存储到变量 httpconn 中在第一行为什么会这样。我收到空指针异常。
public static String requestToken() { String url = Const.REQUEST_TOKEN_URL; String header = oauth_header(url, HttpProtocolConstants.HTTP_METHOD_GET); String requestTokenUrl = concatURL(url, header); HttpConnection httpConn = null; InputStream input = null; try { httpConn = (HttpConnection) Connector.open(requestTokenUrl); // kris connection httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_GET); httpConn.setRequestProperty("WWW-Authenticate","OAuth realm=http://twitter.com/"); httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); input = httpConn.openDataInputStream(); int resp = httpConn.getResponseCode(); if (resp == HttpConnection.HTTP_OK) { StringBuffer buffer = new StringBuffer(); int ch; while ( (ch = input.read()) != -1) { buffer.append( (char) ch); } String content = buffer.toString(); Const.token = content.substring(content.indexOf((Const.OAUTH_TOKEN+"="))+(Const.OAUTH_TOKEN+"=").length(), content.indexOf('&')); Const.tokenSecret = content.substring(content.indexOf((Const.OAUTH_TOKEN_SECRET+"="))+(Const.OAUTH_TOKEN_SECRET+"=").length(), content.length()); message = httpConn.getResponseMessage(); } return message;//(getTwitterMessage(httpConn.getResponseCode())); } catch (IOException e) { return "exception"; } catch (Exception nc) { return "noConnection"; } finally { try { httpConn.close(); input.close(); } catch (IOException e) { e.printStackTrace(); }
Here is my code in this code the value inside the try block I am getting the value of httpconn = null but in the first line I am storing the value into the variable httpconn in the first line why it so. I am getting nullpointerexception.
public static String requestToken() { String url = Const.REQUEST_TOKEN_URL; String header = oauth_header(url, HttpProtocolConstants.HTTP_METHOD_GET); String requestTokenUrl = concatURL(url, header); HttpConnection httpConn = null; InputStream input = null; try { httpConn = (HttpConnection) Connector.open(requestTokenUrl); // kris connection httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_GET); httpConn.setRequestProperty("WWW-Authenticate","OAuth realm=http://twitter.com/"); httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); input = httpConn.openDataInputStream(); int resp = httpConn.getResponseCode(); if (resp == HttpConnection.HTTP_OK) { StringBuffer buffer = new StringBuffer(); int ch; while ( (ch = input.read()) != -1) { buffer.append( (char) ch); } String content = buffer.toString(); Const.token = content.substring(content.indexOf((Const.OAUTH_TOKEN+"="))+(Const.OAUTH_TOKEN+"=").length(), content.indexOf('&')); Const.tokenSecret = content.substring(content.indexOf((Const.OAUTH_TOKEN_SECRET+"="))+(Const.OAUTH_TOKEN_SECRET+"=").length(), content.length()); message = httpConn.getResponseMessage(); } return message;//(getTwitterMessage(httpConn.getResponseCode())); } catch (IOException e) { return "exception"; } catch (Exception nc) { return "noConnection"; } finally { try { httpConn.close(); input.close(); } catch (IOException e) { e.printStackTrace(); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
finally
块中,如果例如在您发布的代码中
Connector.open()
抛出异常,httpConn
将不会被初始化为任何内容除了null
之外。您将捕获该异常并希望返回与其相关的信息,但在返回之前,您尝试访问空指针(在finally
块中),这会引发NullPointerException
。In the
finally
block, doIf e.g. in the code you posted
Connector.open()
throws an exception,httpConn
will not be initialized to anything other thannull
. You will catch that exception and want to return information related to it but before returning, you try to access a null pointer (in thefinally
block) which raises theNullPointerException
.当出现以下情况时会抛出空指针异常:
空对象。
是一个数组。
null 就好像它是一个数组一样。
可抛出的值。
所以这表明您的 httpConn = (HttpConnection) Connector.open(requestTokenUrl); // kris 连接 不起作用。检查一下
Null pointer exception is thrown when:
null object.
were an array.
null as if it were an array.
Throwable value.
So this indicates that your
httpConn = (HttpConnection) Connector.open(requestTokenUrl); // kris connection
is not working. Check in that