{"id":1436,"date":"2022-08-30T15:16:31","date_gmt":"2022-08-30T15:16:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/error-java-lang-nullpointer-exception-on-android-twitter-oauth-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:31","modified_gmt":"2022-08-30T15:16:31","slug":"error-java-lang-nullpointer-exception-on-android-twitter-oauth-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/error-java-lang-nullpointer-exception-on-android-twitter-oauth-collection-of-common-programming-errors\/","title":{"rendered":"Error Java.Lang.NullPointer Exception on Android Twitter OAuth-Collection of common programming errors"},"content":{"rendered":"<p>i have some problem, i want to make my application OAuth with twitter account<\/p>\n<p>This is my Main.xml file<\/p>\n<pre><code>\n\n    \n\n    \n\n\n<\/code><\/pre>\n<p>And this is my Main.java File<\/p>\n<pre><code>package com.test.twitteroauth;\n\nimport oauth.signpost.OAuth;\nimport oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;\nimport oauth.signpost.commonshttp.CommonsHttpOAuthProvider;\nimport oauth.signpost.exception.OAuthCommunicationException;\nimport oauth.signpost.exception.OAuthExpectationFailedException;\nimport oauth.signpost.exception.OAuthMessageSignerException;\nimport oauth.signpost.exception.OAuthNotAuthorizedException;\nimport android.net.Uri;\nimport android.os.Bundle;\nimport android.app.Activity;\nimport android.app.Dialog;\nimport android.content.Intent;\nimport android.util.Log;\nimport android.view.Menu;\nimport android.view.View;\nimport android.view.View.OnClickListener;\nimport android.widget.ImageButton;\n\n\n\npublic class MainActivity extends Activity {\n\n    private static final String CONSUMER_KEY = \"CONSUMER KEY GOES HERE\";\n    private static final String CONSUMER_SECRET = \"CONSUMER SECRET GOES HERE\";\n\n    private static String ACCESS_KEY = null;\n    private static String ACCESS_SECRET = null;\n\n\n    private static final String REQUEST_URL = \"http:\/\/twitter.com\/oauth\/request_token\";\n    private static final String ACCESS_TOKEN_URL = \"http:\/\/twitter.com\/oauth\/access_token\";\n    private static final String AUTH_URL = \"http:\/\/twitter.com\/oauth\/authorize\";\n    private static final String CALLBACK_URL = \"OauthTwitter:\/\/twitt\";\n    private static final String PREFERENCE_FILE = \"twitter_oauth.prefs\";\n\n    private static CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);\n    private static CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);\n\n\n    @Override\n    public void onCreate(Bundle savedInstanceState) {\n\n        ImageButton btnTwitterConnect = (ImageButton) findViewById(R.id.btnTwitterConnect);\n        btnTwitterConnect.setOnClickListener(new OnClickListener() {\n\n            public void onClick(View v) {\n                \/\/ TODO Auto-generated method stub\n\n                try {\n                     String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);\n                     Log.d(\"OAuthTwitter\", authURL);\n                     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));\n                    } catch (OAuthMessageSignerException e) {\n                        e.printStackTrace();\n                    } catch (OAuthNotAuthorizedException e) {\n                        e.printStackTrace();\n                    } catch (OAuthExpectationFailedException e) {\n                        e.printStackTrace();\n                    } catch (OAuthCommunicationException e) {\n                        e.printStackTrace();\n                    }\n            }\n        });         \n    }\n\n        @Override\n        public void onResume() {\n            super.onResume();\n        Uri uri = this.getIntent().getData();\n\n        if (uri != null &amp;&amp; uri.toString().startsWith(CALLBACK_URL)) {\n            Log.d(\"OAuthTwitter\", uri.toString());\n        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);\n            Log.d(\"OAuthTwitter\", verifier);\n        try {\n        provider.retrieveAccessToken(consumer, verifier);\n        ACCESS_KEY = consumer.getToken();\n        ACCESS_SECRET = consumer.getTokenSecret();\n\n        Log.d(\"OAuthTwitter\", ACCESS_KEY);\n        Log.d(\"OAuthTwitter\", ACCESS_SECRET);\n\n        } catch (OAuthMessageSignerException e) {\n            e.printStackTrace();\n        } catch (OAuthNotAuthorizedException e) {\n            e.printStackTrace();\n        } catch (OAuthExpectationFailedException e) {\n            e.printStackTrace();\n        } catch (OAuthCommunicationException e) {\n            e.printStackTrace();\n        }\n       }\n      }\n\n    @Override\n    public boolean onCreateOptionsMenu(Menu menu) {\n        getMenuInflater().inflate(R.menu.main, menu);\n        return true;\n    }\n\n\n}\n<\/code><\/pre>\n<p>I just followed the tutorial here: http:\/\/blog.copyninja.info\/2010\/09\/android-oauth-authentication-with.html<\/p>\n<p>But the android application is always Crashed<\/p>\n<p>LogCat Details:<\/p>\n<pre><code>09-08 19:11:13.754: D\/AndroidRuntime(566): Shutting down VM\n09-08 19:11:13.754: W\/dalvikvm(566): threadid=1: thread exiting with uncaught exception (group=0x4001d800)\n09-08 19:11:13.785: E\/AndroidRuntime(566): FATAL EXCEPTION: main\n09-08 19:11:13.785: E\/AndroidRuntime(566): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.twitteroauth\/com.test.twitteroauth.MainActivity}: java.lang.NullPointerException\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.os.Handler.dispatchMessage(Handler.java:99)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.os.Looper.loop(Looper.java:123)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.app.ActivityThread.main(ActivityThread.java:4627)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at java.lang.reflect.Method.invokeNative(Native Method)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at java.lang.reflect.Method.invoke(Method.java:521)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at dalvik.system.NativeStart.main(Native Method)\n09-08 19:11:13.785: E\/AndroidRuntime(566): Caused by: java.lang.NullPointerException\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at com.test.twitteroauth.MainActivity.onCreate(MainActivity.java:44)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)\n09-08 19:11:13.785: E\/AndroidRuntime(566):  ... 11 more\n<\/code><\/pre>\n<p>I know it&#8217;s caused by java.Lang.NullPointerException but where is the errors?<\/p>\n<p>Thank you<\/p>\n<p>(Android 2.2, Eclipse 3.7, Java JDK 1.7)<\/p>\n<ol>\n<li>\n<p>In addition to the <code>setContentView(...)<\/code>, you should call <code>super.onCreate(savedInstanceState);<\/code> above it, otherwise you&#8217;ll end up with a <code>SuperNotCalledException<\/code><\/p>\n<pre><code>@Override\npublic void onCreate(Bundle savedInstanceState) {\n   super.onCreate(savedInstanceState);\n   setContentView(...);\n\n   \/\/ ...\n\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Please Add <code>setcontentview(R.layout.main)<\/code> line before <code>ImageButton btnTwitterConnect = (ImageButton) findViewById(R.id.btnTwitterConnect);<\/code>, it will solve your problem.<\/p>\n<\/li>\n<li>\n<p>You need to call <code>setContentView(layoutID)<\/code> and pass in the layout the button belongs too before you call <code>findViewById(viewId)<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:52:48. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i have some problem, i want to make my application OAuth with twitter account This is my Main.xml file And this is my Main.java File package com.test.twitteroauth; import oauth.signpost.OAuth; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; import oauth.signpost.exception.OAuthCommunicationException; import oauth.signpost.exception.OAuthExpectationFailedException; import oauth.signpost.exception.OAuthMessageSignerException; import oauth.signpost.exception.OAuthNotAuthorizedException; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.Dialog; import android.content.Intent; import android.util.Log; import android.view.Menu; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1436","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1436"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1436\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}