获取变量的值null

发布于 2024-11-04 00:04:09 字数 2171 浏览 5 评论 0原文

这是我在此代码中的代码,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2024-11-11 00:04:09

finally块中,

if (httpConn != null)
    httpConn.close();

if (input != null)
    input.close();

如果例如在您发布的代码中Connector.open()抛出异常,httpConn将不会被初始化为任何内容除了 null 之外。您将捕获该异常并希望返回与其相关的信息,但在返回之前,您尝试访问空指针(在finally 块中),这会引发NullPointerException

In the finally block, do

if (httpConn != null)
    httpConn.close();

if (input != null)
    input.close();

If e.g. in the code you posted Connector.open() throws an exception, httpConn will not be initialized to anything other than null. You will catch that exception and want to return information related to it but before returning, you try to access a null pointer (in the finally block) which raises the NullPointerException.

长发绾君心 2024-11-11 00:04:09

当出现以下情况时会抛出空指针异常:

<块引用>

当应用程序在对象的情况下尝试使用 null 时抛出
是必须的。其中包括:

  • 调用空对象的实例方法。
  • 访问或修改a的字段
    空对象。
  • 将 null 的长度视为
    是一个数组。
  • 访问或修改插槽
    null 就好像它是一个数组一样。
  • 抛出 null 就好像它是一个
    可抛出的值。

所以这表明您的 httpConn = (HttpConnection) Connector.open(requestTokenUrl); // kris 连接 不起作用。检查一下

Null pointer exception is thrown when:

Thrown when an application attempts to use null in a case where an object
is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a
    null object.
  • Taking the length of null as if it
    were an array.
  • Accessing or modifying the slots of
    null as if it were an array.
  • Throwing null as if it were a
    Throwable value.

So this indicates that your httpConn = (HttpConnection) Connector.open(requestTokenUrl); // kris connection is not working. Check in that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文