ok i was working on simon says app...here is the codes ...something is giving me nullpointerexception...i have trying to think for hours and cant seem to find anything to correct on...please help

Here is the Main.java

package com.gagan.simonsaysreplica;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



    public class Main extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


    Button b= (Button) findViewById(R.id.button5);
        b.setOnClickListener(new OnClickListener(){
        public void onClick(View v){    

        startActivity(new Intent(Main.this,Game.class ));
        }
        });     

    }
    }

AND HERE IS THE GAME CLASS

package com.gagan.simonsaysreplica;

import java.util.Random;

import com.gagan.simonsaysreplica.R.id;

import android.R.array;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Game extends Activity {


    public int seq_pos = 0; 
    public int seq[] = new int[25];
    final Button b1= (Button) findViewById(R.id.button5);
    final Button b2= (Button) findViewById(R.id.button2);
    final Button b3= (Button) findViewById(R.id.button3);
    final Button b4= (Button) findViewById(R.id.button4);



    @Override 
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);       

       for(int i=0;i<25;i++)         
        {
        seq[i] = ((int) (Math.random() * 4) + 1);
        }        
       seq_show(0); 
             b1.setOnClickListener(new OnClickListener(){
                public void onClick(View v){
                     getButton(1);
                 }});

         b2.setOnClickListener(new OnClickListener(){
                public void onClick(View v){
                     getButton(2);
                 }});

         b3.setOnClickListener(new OnClickListener(){
                public void onClick(View v){
                    getButton(3);
                }});

            b4.setOnClickListener(new OnClickListener(){
                public void onClick(View v){
                getButton(4);
            }});
            }
    // functions

    public void seq_show(int seq_end) {
        for (int i=0; i <= seq_end; i++){

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            but_show(seq[i]);

        }
     }

    public void getButton (int but_pushed){


        for(int i=0;i<=seq_pos;i++)
        {if (seq[seq_pos] == but_pushed) {
            seq_pos++;
            but_show(but_pushed);
            }else{
            MediaPlayer errorSound = MediaPlayer.create(this, R.raw.wrong);
            errorSound.start();
            Handler handler= new Handler();
             handler.postDelayed(new Runnable() { 
                public void run() {
                    }
            }, 1000);
            break;
        }

        }

        seq_show(seq_pos);
    }   
    public void but_show(int but_num){
        if (but_num == 1) {
            b1.setBackgroundResource(R.drawable.red1);
         Handler handler= new Handler();
         handler.postDelayed(new Runnable() { 
             public void run() { 
                  b1.setBackgroundResource(R.drawable.red); 
             } 
        }, 200); 

        }
        else if (but_num == 2) {
            b2.setBackgroundResource(R.drawable.blue1);
         Handler handler= new Handler();
         handler.postDelayed(new Runnable() { 
             public void run() { 
                  b2.setBackgroundResource(R.drawable.blue); 
             } 
        }, 200); 

        }
        else if (but_num == 3) {
            b3.setBackgroundResource(R.drawable.yellow1);
         Handler handler= new Handler();
         handler.postDelayed(new Runnable() { 
             public void run() { 
                  b3.setBackgroundResource(R.drawable.yellow); 
             } 
        }, 200); 
        }
        else if (but_num == 4) {
             b4.setBackgroundResource(R.drawable.green1);
             Handler handler= new Handler();
             handler.postDelayed(new Runnable() { 
                 public void run() { 
                      b4.setBackgroundResource(R.drawable.green); 
                 } 
            }, 200); 
        }

       }
    }

HERE IS THE MAIN.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/play" />

</RelativeLayout>

HERE IS THE GAME.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="25dp"
        android:layout_toLeftOf="@+id/button2"
        android:background="@drawable/yellow" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignLeft="@+id/button3"
        android:layout_marginBottom="17dp"
        android:background="@drawable/red" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button5"
        android:layout_alignBottom="@+id/button5"
        android:layout_alignLeft="@+id/button4"
        android:background="@drawable/blue" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="74dp"
        android:background="@drawable/green" />

</RelativeLayout>

HERE IS THE MANIFEST FILE

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gagan.simonsaysreplica"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gagan.simonsaysreplica.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Game"></activity>
    </application>

</manifest>

AND THE LOG.CAT

03-06 08:03:00.028: D/AndroidRuntime(2004): Shutting down VM
03-06 08:03:00.038: W/dalvikvm(2004): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-06 08:03:00.058: E/AndroidRuntime(2004): FATAL EXCEPTION: main
03-06 08:03:00.058: E/AndroidRuntime(2004): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gagan.simonsaysreplica/com.gagan.simonsaysreplica.Game}: java.lang.NullPointerException
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.os.Looper.loop(Looper.java:123)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at java.lang.reflect.Method.invoke(Method.java:507)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at dalvik.system.NativeStart.main(Native Method)
03-06 08:03:00.058: E/AndroidRuntime(2004): Caused by: java.lang.NullPointerException
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.app.Activity.findViewById(Activity.java:1647)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at com.gagan.simonsaysreplica.Game.<init>(Game.java:23)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at java.lang.Class.newInstanceImpl(Native Method)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at java.lang.Class.newInstance(Class.java:1409)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-06 08:03:00.058: E/AndroidRuntime(2004):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-06 08:03:00.058: E/AndroidRuntime(2004):     ... 11 more
03-06 08:03:03.748: I/Process(2004): Sending signal. PID: 2004 SIG: 9
03-06 08:12:56.609: D/AndroidRuntime(2038): Shutting down VM
03-06 08:12:56.609: W/dalvikvm(2038): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-06 08:12:56.618: E/AndroidRuntime(2038): FATAL EXCEPTION: main
03-06 08:12:56.618: E/AndroidRuntime(2038): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gagan.simonsaysreplica/com.gagan.simonsaysreplica.Game}: java.lang.NullPointerException
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.os.Looper.loop(Looper.java:123)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at java.lang.reflect.Method.invoke(Method.java:507)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at dalvik.system.NativeStart.main(Native Method)
03-06 08:12:56.618: E/AndroidRuntime(2038): Caused by: java.lang.NullPointerException
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.app.Activity.findViewById(Activity.java:1647)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at com.gagan.simonsaysreplica.Game.<init>(Game.java:23)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at java.lang.Class.newInstanceImpl(Native Method)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at java.lang.Class.newInstance(Class.java:1409)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-06 08:12:56.618: E/AndroidRuntime(2038):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-06 08:12:56.618: E/AndroidRuntime(2038):     ... 11 more
03-06 08:26:43.097: D/AndroidRuntime(2073): Shutting down VM
03-06 08:26:43.097: W/dalvikvm(2073): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-06 08:26:43.117: E/AndroidRuntime(2073): FATAL EXCEPTION: main
03-06 08:26:43.117: E/AndroidRuntime(2073): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gagan.simonsaysreplica/com.gagan.simonsaysreplica.Game}: java.lang.NullPointerException
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.os.Looper.loop(Looper.java:123)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at java.lang.reflect.Method.invoke(Method.java:507)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at dalvik.system.NativeStart.main(Native Method)
03-06 08:26:43.117: E/AndroidRuntime(2073): Caused by: java.lang.NullPointerException
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.app.Activity.findViewById(Activity.java:1647)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at com.gagan.simonsaysreplica.Game.<init>(Game.java:23)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at java.lang.Class.newInstanceImpl(Native Method)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at java.lang.Class.newInstance(Class.java:1409)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-06 08:26:43.117: E/AndroidRuntime(2073):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-06 08:26:43.117: E/AndroidRuntime(2073):     ... 11 more
03-06 08:26:45.318: I/Process(2073): Sending signal. PID: 2073 SIG: 9

I'm certainly not an expert, but it looks like you are calling findViewById before it is ready. You should probably avoid calling it until after you have called setContentView in onCreate.

From the documentation for findViewById: "Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle)."

From the documentation for onCreate: "Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI, ..."

Of course, I am not an expert, but it looks like before you call findViewById, it has been ready. You should avoid calling it until you setContentView in onCreate.

thanks a lot

gagun can I ask you something ?

seems something wrong with xml..name the buttons properly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.