简体   繁体   中英

Getting the value null of the variable

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();
             }

In the finally block, do

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

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

If eg 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 .

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 httpConn = (HttpConnection) Connector.open(requestTokenUrl); // kris connection is not working. Check in that

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