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 8658677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:49:39+00:00 2026-06-12T15:49:39+00:00

I get a null pointer exception and the program crash on each time I

  • 0

I get a null pointer exception and the program crash on each time I want to update the highscore text using setText(). what causes this problem?
this code is when i set my layout, the layout is a part of the gameView using opengl, and I put the highscore textview on the upper left corner

public void onCreate(Bundle savedInstanceState) {
    SFEngine.display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();//ambl ukuran width height layar
    super.onCreate(savedInstanceState);
    gameView = new SFGameView(this);
    gameView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


    TextView textBox = new TextView(this);
    textBox.setId(1);
    textBox.setText("HIGH SCORE");
    textBox.setBackgroundColor(Color.BLUE);
    textBox.setWidth(SFEngine.display.getWidth()/2);
    textBox.setHeight(50);
    Button pauseButton = new Button(this);
    pauseButton.setText("PAUSE");
    pauseButton.setHeight(50);
    pauseButton.setWidth(SFEngine.display.getWidth()/2);
    pauseButton.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent e) {
            //pause game
            SFEngine.isPlaying = false;
            Intent i1 = new Intent(SFGames.this, pause.class);
            gameView.onPause();
            startActivityForResult(i1,0);//hrs pk result soalny mw blk lg
            return true;
        }
    });


    RelativeLayout.LayoutParams lp_pause = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    RelativeLayout.LayoutParams lp_hs = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    lp_hs.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    lp_pause.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    lp_pause.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    textBox.setLayoutParams(lp_hs);
    pauseButton.setLayoutParams(lp_pause);

    layout.addView(gameView);
    layout.addView(textBox);        
    layout.addView(pauseButton);
    setContentView(layout);

and here is the setText code

public boolean onTouchEvent (MotionEvent event){//buat nerima input user
    if(!SFEngine.isPlaying){
        finish();
    }
    textBox.setText("High Score :" + SFEngine.score);//here is the source of the prob
.....

error log:

 10-13 22:38:34.207: E/AndroidRuntime(528): FATAL EXCEPTION: main
10-13 22:38:34.207: E/AndroidRuntime(528): java.lang.NullPointerException
10-13 22:38:34.207: E/AndroidRuntime(528):  at com.starfighter.SFGames.onTouchEvent(SFGames.java:136)
10-13 22:38:34.207: E/AndroidRuntime(528):  at  android.app.Activity.dispatchTouchEvent(Activity.java:2367)

UPDATE :
i’ve updated the source code so the textview declaration is outside of the onCreate, it seems to be working normally now..

  • 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-12T15:49:40+00:00Added an answer on June 12, 2026 at 3:49 pm

    You should define the text view as a class field in the activity, outside the onCreate:

    TextView textBox;
    
    public void onCreate(Bundle savedInstanceState) {
        ....
        textBox = new TextView(this);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# application..I continuously get a null reference exception..I manage to catch
I need to read a text file when I start my program. I'm using
I am getting a null pointer exception in the line where setJavaScriptEnabled(true) is set
I get a null - reference in this Code-line: **DataRow dr = tableSelectedItems.NewRow();** I
Can someone tell me why I get the null value for activationLink variable in
In Java, is it possible to attempt a cast and get back null if
Can get all triples with value null in specific field? All people with date_of_birth
I get the following error on firebug: this._processor is null and the error is
I have coded a gui for my CRUD Program, and when I want to
I'm working on this program to get all the files in the directory. For

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.