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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:18:17+00:00 2026-06-14T17:18:17+00:00

I am getting nullpointerexception when trying to reach my activity and change some ints.

  • 0

I am getting nullpointerexception when trying to reach my activity and change some ints.
This is what it looks like:
MainActivity.java

    public class MainActivity extends Activity {

    public static SoundManager mSoundManager = new SoundManager();

    private static final String TAG = MainActivity.class.getSimpleName();


     private MainGamePanel gamePanel;
     SharedPreferences myPrefs;

     public int win = 0;
     public int fail = 0;

     int wins = 0;
     int fails = 0;



    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // set our MainGamePanel as the View
    setContentView(new MainGamePanel(this));

    myPrefs = this.getSharedPreferences("hgbdata", 0);

        try {
        wins = myPrefs.getInt("wins", 0);
        fails = myPrefs.getInt("fails", 0);
        if ((wins != 0) && (fails != 0)) {
        gamePanel.winn = wins;
        gamePanel.failn = fails;
        }
        } catch (NullPointerException npe) {
            Log.d(TAG, "Nothing to load");
        }

    //INIT SOUND
    mSoundManager.initSounds(getBaseContext());
    //SOUNDS
    mSoundManager.addSound(1, R.raw.draw);
    mSoundManager.addSound(2, R.raw.cheer);
    mSoundManager.addSound(3, R.raw.boo);

    }

. . .


public void win() {
    win++;
}

public void fail() {
    fail++;
}



}

This is where I am trying to reach the activity from:
MainGamePanel.java

public class MainGamePanel extends SurfaceView implements
SurfaceHolder.Callback {

private static final String TAG = MainGamePanel.class.getSimpleName();

private MainThread thread;
private MainActivity activity;

. . .

public void update() {

//somewhere inside the update()
activity.win();

. . .

activity.fail();

}

}

This is the log:

11-18 10:23:47.336: D/MainThread(1190): Starting game loop
11-18 10:23:47.336: D/MainThread.initTimingElements()(1190): Timing elements for stats initialised
11-18 10:23:47.386: D/gralloc_goldfish(1190): Emulator without GPU emulation detected.
11-18 10:24:19.925: D/dalvikvm(1190): GC_CONCURRENT freed 356K, 2% free 28540K/28999K, paused 132ms+30ms, total 441ms
11-18 10:24:20.965: W/dalvikvm(1190): threadid=13: thread exiting with uncaught exception (group=0x40a13300)
11-18 10:24:20.965: E/AndroidRuntime(1190): FATAL EXCEPTION: Thread-105
11-18 10:24:20.965: E/AndroidRuntime(1190): java.lang.NullPointerException
11-18 10:24:20.965: E/AndroidRuntime(1190):     at com.nti.hanga.gubbe.MainGamePanel.update(MainGamePanel.java:591)
11-18 10:24:20.965: E/AndroidRuntime(1190):     at com.nti.hanga.gubbe.MainThread.run(MainThread.java:92)
11-18 10:24:21.235: I/AndroidRuntime(1190): VM exiting with result code 0, cleanup skipped.

MainGamePanel.java:591 is activity.win();

Thank you for your help.

  • 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-14T17:18:18+00:00Added an answer on June 14, 2026 at 5:18 pm

    You shouldn’t even try to reference your activity from the SurfaceView.

    Define an interface and use a callback to let the activity know that the game is over.

    public Interface GameOverListener {
        void onGameOver(boolean won);
    }
    

    In your custom surface view

    ArrayList<GameOverListener > listeners = new ArrayList<GameOverListener >();
    
    ...
    
    public void setGameOverListener(GameOverListener listener){
        listeners.add(listener);
    }
    

    In your update event

    for (GameOverListener listener:listeners){
       listener.onGameOver(gameWon?true:false); // here you pass true if they won, false if they lost
    }
    

    In your Activity:

    public class MainActivity extends Activity implements GameOverListener {
    
    ...
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        ... 
        gamePanel.setGameOverListener(this);
        ...
    }
    
    public void onGameOver(boolean won){
       if (won){
          // they won!
       } else {
          // they lost!
       }
    }
    

    You could improve the surface view class by adding removeGameOverListener and checking that you do not add the same listener twice in setGameOverListener.

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

Sidebar

Related Questions

I'm getting this NullPointerException for no reason. I am trying to send accelerometer 'x'
I am getting the java.lang.NullPointerException and i am not sure why. I am trying
i am trying to use Console class in java. with this code import java.io.Console;
I'm trying to parse some xml and i'm getting a NullPointerException but i can't
I keep getting 21002 'java.lang.NullPointerException' errors from Apple when I try to test my
I'm getting a java.lang.NullPointerException right when the app launches and it shuts down. The
Trying to get text value of child in expandableListView. Getting a nullpointerexception in the
I trying to call some ejb bean method from tread. and getting error :
I keep getting NullPointerException trying to deflate or make invisible the ViewStub from my
I'm now trying to learn Oracle ADF and I'm getting a NullPointerException while running

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.