Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Crystal Ball - End of Stage 1 ERROR

Hi All,

I'm new to Android development and am having some issues with the Crsytal ball app. It seems others are getting a very similar issue and no one has been provided a solution yet. I have pasted the information Ben asked for in another thread from LogCat so hopefully we can find an issues to this once and for all....

Thanks and look forward to contributing on this forum once I learn a little more! Kent

06-02 21:53:00.913: E/AndroidRuntime(1262): FATAL EXCEPTION: main 06-02 21:53:00.913: E/AndroidRuntime(1262): Process: com.example.crystalballx, PID: 1262 06-02 21:53:00.913: E/AndroidRuntime(1262): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalballx/com.example.crystalballx.MainActivity}: java.lang.NullPointerException 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.os.Handler.dispatchMessage(Handler.java:102) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.os.Looper.loop(Looper.java:136) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-02 21:53:00.913: E/AndroidRuntime(1262): at java.lang.reflect.Method.invokeNative(Native Method) 06-02 21:53:00.913: E/AndroidRuntime(1262): at java.lang.reflect.Method.invoke(Method.java:515) 06-02 21:53:00.913: E/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-02 21:53:00.913: E/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-02 21:53:00.913: E/AndroidRuntime(1262): at dalvik.system.NativeStart.main(Native Method) 06-02 21:53:00.913: E/AndroidRuntime(1262): Caused by: java.lang.NullPointerException 06-02 21:53:00.913: E/AndroidRuntime(1262): at com.example.crystalballx.MainActivity.onCreate(MainActivity.java:21) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.app.Activity.performCreate(Activity.java:5231) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-02 21:53:00.913: E/AndroidRuntime(1262): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-02 21:53:00.913: E/AndroidRuntime(1262): ... 11 more 06-02 21:53:04.133: I/Process(1262): Sending signal. PID: 1262 SIG: 9

8 Answers

Nick Edwards
PLUS
Nick Edwards
Courses Plus Student 15,766 Points

Ah! That is indeed the problem. This is very similar to the problem I had when I started this course - it was written before Eclipse was updated so that Android projects automatically use Fragments (which you'll learn about later in the course).

The work-around for now would be to delete everything that's in activity_main.xml, move all of the contents from fragment_main to activity_main, and delete the fragment_main file.

Your MainActivity.java is already set up not to use fragments, so no changes need to be made there. You may find in subsequent projects that MainActivity.java will come with a bunch of code designed for using Fragments, but the video here at 2:00 explains how to remove it.

As to your other question - yes, Java conventions call for variables to always start with lowercase, with subsequent words being capitalised. Class names, on the other hand, are always capitalised (eg. the variable randomGeno vs the class Random).

Yep, after not being able to post the xml I went ahead and recreated the textview and button in the activity_main.xml

Works flawlessly now :D Thanks for being patient and helping me through Nick!

Nick Edwards
Nick Edwards
Courses Plus Student 15,766 Points

Awesome! Glad you got it working - it taught me a bit more about interpreting the LogCat which is immensely useful.

You might find the answer in this post useful as a future reference as it explains quite a bit about the stacktrace.

Nick Edwards
PLUS
Nick Edwards
Courses Plus Student 15,766 Points

Seems to be a NullPointer exception, which according to the Java Docs is:

Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object. Taking the length of null as if it were an array. Accessing or modifying the slots of null as if it were an array. Throwing null as if it were a Throwable value.

The lines 'Caused by: java.lang.NullPointerException 06-02 21:53:00.913: E/AndroidRuntime(1262): at com.example.crystalballx.MainActivity.onCreate(MainActivity.java:21)' provide a clue as to where the problem originates.

Could you post the content of your MainActivity file? Don't forget to use the Markdown formatting so it's easier to read :)

Hi Nick,

Thanks for your response! Here is my MainActivity.java file I'm hoping the Markdown formatting works!!

package com.example.crystalballx;

import java.util.Random;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

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

        final TextView answerLabel = (TextView) findViewById(R.id.textView1);
        Button getAnswerButton = (Button) findViewById(R.id.button1);

        getAnswerButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String answer = "";

                Random RandomGeno = new Random();
                int randomNum = RandomGeno.nextInt(3);
                answer = Integer.toString(randomNum);

                answerLabel.setText(answer);
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Nick Edwards
PLUS
Nick Edwards
Courses Plus Student 15,766 Points

Hmm, nothing strikes me as fundamentally wrong with your code - the only exception is that you've capitalised a variable declaration (RandomGeno should be randomGeno). I don't think that would cause a NullPointerException... But it's worth changing regardless.

Nick, Thank you very much for your response. However changing it to randomGeno still didn't work. Any other ideas? Really want to get this working!

Nick Edwards
Nick Edwards
Courses Plus Student 15,766 Points

Okay, could you post your activity_main.xml file here too? I'll see if I can spot anything in there that might be causing a problem.

Well... This made me think this may be the issue. My textView1 and button1 are in my fragment_main.xml and nothing is in my activity_main.xml

I have posted both below, hopefully you could lead me to the exact issue / what to move where.

(Also quick side question - I seem to notice a trend, should everything within Eclipse/Java always start with a lower case and then every other word thats apart of the string be uppercase? For example you pointed our that instead of "RandomGeno" it should be "randomGeno"?)

Below is the Activity_Main.xml

``xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.crystalballx.MainActivity" tools:ignore="MergeRootFrame" />

Below is the fragment_main.xml
``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="com.example.crystalballx.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="32sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="Enlighten Me!" />

</RelativeLayout>
``

Any ideas how to post XML so it shows all text?

Have repasted below not in markdown due to rendering issues:

Activity_main XML below:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.crystalballx.MainActivity" tools:ignore="MergeRootFrame" />

Fragment_main XML below:

<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="com.example.crystalballx.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="32sp" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:text="Enlighten Me!" />

</RelativeLayout>