Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7984449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:16:19+00:00 2026-06-04T11:16:19+00:00

I am trying to access textview from layout in java. While accessing it I

  • 0

I am trying to access textview from layout in java. While accessing it I am getting NullPointerException in my logcat. Here is my code

public class GameActivity extends Activity {
TextView tv = (TextView)findViewById(R.id.ctry);

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv.setText("Text");
}
}

Here is my logcat

05-20 12:32:57.808: D/AndroidRuntime(18776): Shutting down VM
05-20 12:32:57.808: W/dalvikvm(18776): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-20 12:32:57.808: E/AndroidRuntime(18776): FATAL EXCEPTION: main
05-20 12:32:57.808: E/AndroidRuntime(18776): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.zafar.game/com.zafar.game.GameActivity}: java.lang.NullPointerException
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.os.Looper.loop(Looper.java:130)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at java.lang.reflect.Method.invokeNative(Native Method)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at java.lang.reflect.Method.invoke(Method.java:507)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at dalvik.system.NativeStart.main(Native Method)
05-20 12:32:57.808: E/AndroidRuntime(18776): Caused by: java.lang.NullPointerException
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.app.Activity.findViewById(Activity.java:1653)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at com.zafar.game.GameActivity.<init>(GameActivity.java:11)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at java.lang.Class.newInstanceImpl(Native Method)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at java.lang.Class.newInstance(Class.java:1409)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-20 12:32:57.808: E/AndroidRuntime(18776):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
05-20 12:32:57.808: E/AndroidRuntime(18776):    ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:16:20+00:00Added an answer on June 4, 2026 at 11:16 am

    You should call findViewById(R.id.ctry) only after you call setContentView(R.layout.main) in your onCreate() code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to access AS400/DB2 stored procedure from Java with Spring JdbcTemplate, XmlBeanFactory beanFactory2 =
i am trying access an asp label within an asp repeater from my code
I m trying to access a rdf file from the google nexus.....(now this code
Trying to access a Servlet from a button on HTML page //Html Page FORM
I am trying to access member variables of a class without using object. please
I'm trying to access the current hibernate session in a test case, and getting
Im trying to access some variables from the parent in a child mc. Parent
I'm trying to access YouTube from within an iOS app, but the iOS SDK
I am trying to Set Location but getting NullPointerException , i have Provided android.permission.ACCESS_COARSE_LOCATION
i cannot get current content page html. when i trying access from script in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.