Play random sound file on button press - Android
<p>I am a beginner, I'm trying to make a very simple app, one button and 4 .ogg files in the assets folder, that button would play random file from the above mentioned.</p>

<p>I have only one main class and one layout.</p>

<p>Here is what I did, the code doesn't show any error but the app crash immediately when launching on emulator.</p>

<p>My main class:-</p>

<pre><code>    MediaPlayer player;
AssetFileDescriptor descriptor;
private String[] filelist;

@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);

player = new MediaPlayer();

Button playSound = (Button)findViewById(R.id.button1);

AssetManager aMan = this.getAssets();
try 
{
 filelist = aMan.list("/assets");
} catch (IOException e1) {
    e1.printStackTrace();
}

playSound.setOnClickListener(new View.OnClickListener() 
{           
    public void onClick(View v) 
    {
        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(filelist.length);

        try 
        {
            String mediaFile = filelist[randomInt];
            descriptor = getAssets().openFd(mediaFile);
                player.setDataSource(descriptor.getFileDescriptor(),descriptor.getStartOffset(),descriptor.getLength());
            descriptor.close();
            player.prepare();
                        player.start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}
</code></pre>

<p>My layout:-</p>

<pre><code>&lt;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"
tools:context=".MainActivity" &gt;

&lt;ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/background"
    android:contentDescription="@string/desc" /&gt;

&lt;Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="154dp"
    android:layout_marginRight="92dp"
    android:text="@string/button1" /&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p>Here is the logcat:-</p>

<pre><code>12-14 19:36:56.062: D/AndroidRuntime(540): Shutting down VM
12-14 19:36:56.062: W/dalvikvm(540): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 19:36:56.072: E/AndroidRuntime(540): FATAL EXCEPTION: main
12-14 19:36:56.072: E/AndroidRuntime(540): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.os.Looper.loop(Looper.java:137)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 19:36:56.072: E/AndroidRuntime(540):  at java.lang.reflect.Method.invokeNative(Native Method)
12-14 19:36:56.072: E/AndroidRuntime(540):  at java.lang.reflect.Method.invoke(Method.java:511)
12-14 19:36:56.072: E/AndroidRuntime(540):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 19:36:56.072: E/AndroidRuntime(540):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 19:36:56.072: E/AndroidRuntime(540):  at dalvik.system.NativeStart.main(Native Method)
12-14 19:36:56.072: E/AndroidRuntime(540): Caused by: java.lang.NullPointerException
12-14 19:36:56.072: E/AndroidRuntime(540):  at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:39)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.Activity.performCreate(Activity.java:4465)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 19:36:56.072: E/AndroidRuntime(540):  ... 11 more
12-14 19:36:59.042: I/Process(540): Sending signal. PID: 540 SIG: 9
12-14 19:37:06.032: D/AndroidRuntime(556): Shutting down VM
12-14 19:37:06.032: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 19:37:06.042: E/AndroidRuntime(556): FATAL EXCEPTION: main
12-14 19:37:06.042: E/AndroidRuntime(556): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.os.Looper.loop(Looper.java:137)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 19:37:06.042: E/AndroidRuntime(556):  at java.lang.reflect.Method.invokeNative(Native Method)
12-14 19:37:06.042: E/AndroidRuntime(556):  at java.lang.reflect.Method.invoke(Method.java:511)
12-14 19:37:06.042: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 19:37:06.042: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 19:37:06.042: E/AndroidRuntime(556):  at dalvik.system.NativeStart.main(Native Method)
12-14 19:37:06.042: E/AndroidRuntime(556): Caused by: java.lang.NullPointerException
12-14 19:37:06.042: E/AndroidRuntime(556):  at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:39)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.Activity.performCreate(Activity.java:4465)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 19:37:06.042: E/AndroidRuntime(556):  ... 11 more
</code></pre>

<p>The second logcat after applying ρяσѕρєя K's solution:-</p>

<pre><code>12-14 20:19:02.227: D/AndroidRuntime(537): Shutting down VM
12-14 20:19:02.237: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 20:19:02.247: E/AndroidRuntime(537): FATAL EXCEPTION: main
12-14 20:19:02.247: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.os.Looper.loop(Looper.java:137)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 20:19:02.247: E/AndroidRuntime(537):  at java.lang.reflect.Method.invokeNative(Native Method)
12-14 20:19:02.247: E/AndroidRuntime(537):  at java.lang.reflect.Method.invoke(Method.java:511)
12-14 20:19:02.247: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 20:19:02.247: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 20:19:02.247: E/AndroidRuntime(537):  at dalvik.system.NativeStart.main(Native Method)
12-14 20:19:02.247: E/AndroidRuntime(537): Caused by: java.lang.NullPointerException
12-14 20:19:02.247: E/AndroidRuntime(537):  at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:42)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.Activity.performCreate(Activity.java:4465)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 20:19:02.247: E/AndroidRuntime(537):  ... 11 more
</code></pre>
