KangarooRIOT
KangarooRIOT

Reputation: 631

Null Pointer Exception Log Cat Error

In my project file and source-code there are no errors. No build errors, no project errors, no syntax errors... and for some reason when i run my application on the emulator it crashes. HERE is the Log Cat information - I've searched everything but with no errors being flagged once it is compiled I'm having a hard time figuring out why the application is crashing.

Thanks in advance!

This is the source code for activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="com.overworldinnovations.datatool.MainActivity"
    tools:ignore="MergeRootFrame" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <RelativeLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/buttonConvert"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="49dp"
                    android:text="Convert" />

                <EditText
                    android:id="@+id/editDecimal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/textView3"
                    android:layout_marginBottom="52dp"
                    android:layout_toRightOf="@+id/textView2"
                    android:ems="10"
                    android:gravity="center_vertical|right"
                    android:inputType="numberSigned"
                    android:maxLength="9" >

                    <requestFocus />
                </EditText>

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="50dp"
                    android:text="Enter the decimal value to be converted :)"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/editDecimal"
                    android:layout_alignParentLeft="true"
                    android:text="Decimal"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/buttonConvert"
                    android:layout_alignParentLeft="true"
                    android:layout_marginBottom="94dp"
                    android:text="Binary"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <EditText
                    android:id="@+id/editBinary"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/textView3"
                    android:layout_alignLeft="@+id/editDecimal"
                    android:ems="10"
                    android:inputType="number" />

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:orientation="vertical">
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:orientation="vertical">
            </RelativeLayout>
        </FrameLayout>
    </RelativeLayout>
</TabHost>

</RelativeLayout>

This is the source code for MainActivity.java

package com.overworldinnovations.datatool;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {


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


    // create the TabHost that will contain the Tabs
    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);


    TabSpec tab1 = tabHost.newTabSpec("First Tab");
    TabSpec tab2 = tabHost.newTabSpec("Second Tab");
    TabSpec tab3 = tabHost.newTabSpec("Third tab");

   // Set the Tab name and Activity
   // that will be opened when particular Tab will be selected
    tab1.setIndicator("Tab1");
    tab1.setContent(new Intent(this,Tab1Activity.class));

    tab2.setIndicator("Tab2");
    tab2.setContent(new Intent(this,Tab2Activity.class));

    tab3.setIndicator("Tab3");
    tab3.setContent(new Intent(this,Tab3Activity.class));

    /** Add the tabs  to the TabHost to display. */
    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}
}

LOG CAT

09-14 00:39:23.213: D/AndroidRuntime(774): Shutting down VM
09-14 00:39:23.213: W/dalvikvm(774): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-14 00:39:23.333: E/AndroidRuntime(774): FATAL EXCEPTION: main
09-14 00:39:23.333: E/AndroidRuntime(774): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.overworldinnovations.datatool/com.overworldinnovations.datatool.MainActivity}: java.lang.NullPointerException
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.os.Looper.loop(Looper.java:137)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 00:39:23.333: E/AndroidRuntime(774):  at java.lang.reflect.Method.invokeNative(Native Method)
09-14 00:39:23.333: E/AndroidRuntime(774):  at java.lang.reflect.Method.invoke(Method.java:511)
09-14 00:39:23.333: E/AndroidRuntime(774):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 00:39:23.333: E/AndroidRuntime(774):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 00:39:23.333: E/AndroidRuntime(774):  at dalvik.system.NativeStart.main(Native Method)
09-14 00:39:23.333: E/AndroidRuntime(774): Caused by: java.lang.NullPointerException
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.widget.TabHost.addTab(TabHost.java:236)
09-14 00:39:23.333: E/AndroidRuntime(774):  at com.overworldinnovations.datatool.MainActivity.onCreate(MainActivity.java:41)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.app.Activity.performCreate(Activity.java:5104)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-14 00:39:23.333: E/AndroidRuntime(774):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-14 00:39:23.333: E/AndroidRuntime(774):  ... 11 more
09-14 00:39:23.353: D/dalvikvm(774): GC_CONCURRENT freed 118K, 9% free 2704K/2944K, paused 8ms+65ms, total 166ms
09-14 00:43:31.776: D/AndroidRuntime(817): Shutting down VM
09-14 00:43:31.776: W/dalvikvm(817): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-14 00:43:31.836: E/AndroidRuntime(817): FATAL EXCEPTION: main
09-14 00:43:31.836: E/AndroidRuntime(817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.overworldinnovations.datatool/com.overworldinnovations.datatool.MainActivity}: java.lang.NullPointerException
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.os.Looper.loop(Looper.java:137)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 00:43:31.836: E/AndroidRuntime(817):  at java.lang.reflect.Method.invokeNative(Native Method)
09-14 00:43:31.836: E/AndroidRuntime(817):  at java.lang.reflect.Method.invoke(Method.java:511)
09-14 00:43:31.836: E/AndroidRuntime(817):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 00:43:31.836: E/AndroidRuntime(817):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 00:43:31.836: E/AndroidRuntime(817):  at dalvik.system.NativeStart.main(Native Method)
09-14 00:43:31.836: E/AndroidRuntime(817): Caused by: java.lang.NullPointerException
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.widget.TabHost.addTab(TabHost.java:236)
09-14 00:43:31.836: E/AndroidRuntime(817):  at com.overworldinnovations.datatool.MainActivity.onCreate(MainActivity.java:41)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.app.Activity.performCreate(Activity.java:5104)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-14 00:43:31.836: E/AndroidRuntime(817):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-14 00:43:31.836: E/AndroidRuntime(817):  ... 11 more
09-14 00:43:31.976: D/dalvikvm(817): GC_CONCURRENT freed 109K, 8% free 2678K/2908K, paused 9ms+43ms, total 187ms

Upvotes: 2

Views: 432

Answers (2)

Wakim
Wakim

Reputation: 1706

Based on your stacktrace and the TabHost.addTab source code. You are missing a TabWidget with id @android:id/tabs inside your TabHost.

If you want to use a TabHost you must have a layout with this prototype:

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

     <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

Don't forget to call mTabHost.setup(); before adding tabs! Like:

TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
tabHost.setup();
// Add your tabs now

Edit: Because you are using Tab.setContent(Intent i) it's mandatory to call TabHost.setup(LocalActivityManager activityGroup) instead of TabHost.setup().

So it i'll be:

TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
tabHost.setup(this);
// Add your tabs now

More details: http://blog.vogella.com/2011/05/17/android-tabs/

But TabHost is "deprecated", i recommend you to use the ActionBar.NAVIGATION_MODE_TABS with ActionBarActivity and using Fragments.

Take a look at this tutorial if you want to migrate: http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

Upvotes: 4

erad
erad

Reputation: 1786

It looks like a Null Pointer Exception.

Based on your log, if I were you, I'd take a good look at com.overworldinnovations.datatool.MainActivity.onCreate(MainActivity.java:41) (the onCreate() method of your MainActivity on line 41)

There is likely some issue in your onCreate() method where some variable you are trying to use is null. (I can't be for certain though since I don't know what is on line 41 of your onCreate())

According to the Java Documentation for Null Pointer Exceptions:

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.

Upvotes: 0

Related Questions