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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:28:56+00:00 2026-06-16T05:28:56+00:00

I’m writing a little Android game. It should display a random sequence of 4

  • 0

I’m writing a little Android game. It should display a random sequence of 4 animals which i implemented as ImageButtons. The user has to remember this sequence and repeat it afterwards.

My problem now is the right timing how the Imagebuttons get visible.

I got the following NullPointerException and couldn’t figure out why. Maybe anyone can help!?

Heres my Main Activity:

package lichtenberger.paul;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageButton;
import android.widget.TextView;

public class Game extends Activity {

    int Reihenfolge[] = new int[40];

    Random generator = new Random();


    public final int CAT = 0;
    public final int MAN = 1;
    public final int BIRD = 2;
    public final int SHEEP = 3;
    public Handler handler;
    public Thread AnimalThread;
    {for(int i = 0; i<40; i++)Reihenfolge[i]=generator.nextInt(4);}


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        final ImageButton cat = (ImageButton)findViewById(R.id.catButton);
        final ImageButton sheep = (ImageButton)findViewById(R.id.sheepButton);
        final ImageButton man = (ImageButton)findViewById(R.id.manButton);
        final ImageButton bird = (ImageButton)findViewById(R.id.birdButton);
        final TextView score = (TextView)findViewById(R.id.scoreNTV);

        handler = new Handler(){

            @Override

            public void handleMessage(Message msg){

                switch (msg.what) {
                case 0:
                    cat.setVisibility(1);
                    man.setVisibility(0);
                    bird.setVisibility(0);
                    sheep.setVisibility(0);
                    break;
                case 1:
                    man.setVisibility(1);
                    bird.setVisibility(0);
                    sheep.setVisibility(0);
                    cat.setVisibility(0);
                    break;
                case 2:
                    bird.setVisibility(1);
                    sheep.setVisibility(0);
                    cat.setVisibility(0);
                    man.setVisibility(0);
                    break;
                case 3:
                    sheep.setVisibility(1);
                    cat.setVisibility(0);
                    man.setVisibility(0);
                    bird.setVisibility(0);
                    break;
                }

            }
        };          
            ShowSequence show = new ShowSequence();
            Thread showSeq = new Thread(show);
            showSeq.start();

        };

}

My Thread Class:

package lichtenberger.paul;   

public class ShowSequence extends Game implements Runnable{    
    @Override
    public void run() {                 
        show();
    }

    private void show() {

        for(int i = 0; i<40; i++){
        switch (Reihenfolge[i]) {
        case 0:
                try {
                // this pauses the Thread: Alternative to doing stuff...
                Thread.sleep(2000);

                handler.sendEmptyMessage(CAT);
                } catch (InterruptedException e) {
                e.printStackTrace();}
                break;

        case 1:
            try {
                // this pauses the Thread: Alternative to doing stuff...
                Thread.sleep(2000);

                handler.sendEmptyMessage(MAN);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;

        case 2:
            try {
                // this pauses the Thread: Alternative to doing stuff...
                Thread.sleep(2000);

                handler.sendEmptyMessage(BIRD);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;

        case 3: 
            try {
                // this pauses the Thread: Alternative to doing stuff...
                Thread.sleep(2000);

                handler.sendEmptyMessage(SHEEP);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        default:
            break;
        }
    }
}}

and my LogCat:

12-20 15:20:34.974: ERROR/AndroidRuntime(598): FATAL EXCEPTION: Thread-75
12-20 15:20:34.974: ERROR/AndroidRuntime(598): java.lang.NullPointerException
12-20 15:20:34.974: ERROR/AndroidRuntime(598):     at lichtenberger.paul.ShowSequence.show(ShowSequence.java:50)
12-20 15:20:34.974: ERROR/AndroidRuntime(598):     at lichtenberger.paul.ShowSequence.run(ShowSequence.java:10)
12-20 15:20:34.974: ERROR/AndroidRuntime(598):     at java.lang.Thread.run(Thread.java:856)

EDIT:

the whole code in one Activity which didnt work:

package lichtenberger.paul;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageButton;
import android.widget.TextView;

public class Game extends Activity {

    int Reihenfolge[] = new int[40];

    Random generator = new Random();
    public ImageButton cat;
    public ImageButton man;
    public ImageButton bird;
    public ImageButton sheep;
    public TextView score;
    private static Handler handler;
    public final int CAT = 0;
    public final int MAN = 1;
    public final int BIRD = 2;
    public final int SHEEP = 3;
    public Runnable showAnimal;
    public Thread AnimalThread;
    {for(int i = 0; i<40; i++)Reihenfolge[i]=generator.nextInt(4);}


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        setupUI();
        showSequence();
        initHandler();
    }



    public void showSequence() {

                showAnimal = new showAnimal();
                AnimalThread = new Thread(showAnimal);
                AnimalThread.start();


    }



    public void setupUI() {

        cat = (ImageButton)findViewById(R.id.catButton);
        sheep = (ImageButton)findViewById(R.id.sheepButton);
        man = (ImageButton)findViewById(R.id.manButton);
        bird = (ImageButton)findViewById(R.id.birdButton);
        score = (TextView)findViewById(R.id.scoreNTV);

    }

    private void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case CAT:
                    cat.setVisibility(1);
                    man.setVisibility(0);
                    bird.setVisibility(0);
                    sheep.setVisibility(0);
                    break;
                case MAN:
                    man.setVisibility(1);
                    bird.setVisibility(0);
                    sheep.setVisibility(0);
                    cat.setVisibility(0);
                    break;
                case BIRD:
                    bird.setVisibility(1);
                    sheep.setVisibility(0);
                    cat.setVisibility(0);
                    man.setVisibility(0);
                    break;
                case SHEEP:
                    sheep.setVisibility(1);
                    cat.setVisibility(0);
                    man.setVisibility(0);
                    bird.setVisibility(0);
                    break;
                }
            }
        };

    }

    class showAnimal implements Runnable {
        public void run() {
            show();
        }

        private void show() {

            for(int i = 0;i<40;i++){

            switch (Reihenfolge[i]) {

            case 0:
                try {
                    // this pauses the Thread: Alternative to doing stuff...
                    Thread.sleep(2000);

                    handler.sendEmptyMessage(CAT);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;

            case 1:
                try {
                    // this pauses the Thread: Alternative to doing stuff...
                    Thread.sleep(2000);

                    handler.sendEmptyMessage(MAN);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;

            case 2:
                try {
                    // this pauses the Thread: Alternative to doing stuff...
                    Thread.sleep(2000);

                    handler.sendEmptyMessage(SHEEP);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            case 3:
                try {
                    // this pauses the Thread: Alternative to doing stuff...
                    Thread.sleep(2000);

                    handler.sendEmptyMessage(BIRD);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }



}
}

Thanks for 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-16T05:28:57+00:00Added an answer on June 16, 2026 at 5:28 am

    The problem is that you have 2 separate objects – instance of Game that has non-null handler. And instance of ShowSequence with null handler. You shouldn’t inherit ShowSequecne from Game. It doesn’t get all Game‘s variables automically, you should make ShowSequence inner class of in Game as @harism suggested.
    Or you have to set all variables in ShowSequence explicitly in constructor or immediately after you created it.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

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.