-2

I'm getting Null point exception whenever I click the AWS button in the Front Page, I have brained stormed alot, I'm not able to understand why. I m going to paste the few lines of code that are relevant, please look at it, and let me know where i m going wrong. Thanks

Manifest file

     <activity
        android:name=".Front"
        android:label="@string/title_activity_front" >
        <intent-filter>
            <action android:name="com.example.getit.Front" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
   <activity
        android:name=".AwsStartAct"
        android:label="@string/title_activity_aws_start" >
        <intent-filter>
            <action android:name="com.example.getit.AwsStartAct" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Front.class

   public class Front extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_front);
    //Declaring all the buttons
    Button bAWS = (Button) findViewById(R.id.bAWS);
    Button bAzure = (Button) findViewById(R.id.bMAzure);
    Button bHadoop = (Button) findViewById(R.id.bHadoop);
    //creating on click listeners
    //for aws
    bAWS.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //go to aws option page

            Intent intent= new Intent(Front.this,AwsStartAct.class);
            startActivity(intent);
        }
    });
    //for azure
    bAzure.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //go to azure option page
            Intent intent= new Intent(Front.this,Azurestart.class);
            startActivity(intent);
        }
    });
    //for hadoop
    bHadoop.setOnClickListener(new View.OnClickListener() {

@Override
//this will have all the things that the button Compute will do on click
public void onClick(View v) {
    // TODO Auto-generated method stub

}
 });
}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.front, 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);
    }

}

AwsStartAct.class

  public class AwsStartAct extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aws_start);
    //Declaring buttons and linking it with the XML Button by ID
    Button Compute, Storage, Database;
    Compute=(Button) findViewById (R.id.bCompute);
    Storage=(Button) findViewById (R.id.bStorage);
    Database=(Button) findViewById (R.id.bHadoop);
    //Defining onClick listener for each button
    //Compute
    Compute.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //going to the compute page
            Intent intent = new Intent(AwsStartAct.this,awscompute.class);
            startActivity(intent);
        }
    });
    //Storage
    Storage.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
    //Database
    Database.setOnClickListener(new View.OnClickListener() {

        @Override
        //this will have all the things that the button Compute will do on click
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.aws_start, 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);
}
 }

activity xml layout

   <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.getit.Front"
android:background="#555" >
  <Button
    android:id="@+id/bAWS"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="132dp"
    android:text="AWS" />

<Button
    android:id="@+id/bMAzure"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bAWS"
    android:layout_below="@+id/bAWS"
    android:layout_marginTop="33dp"
    android:text="Microsoft Azure" />

<Button
    android:id="@+id/bHadoop"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bMAzure"
    android:layout_below="@+id/bMAzure"
    android:layout_marginTop="33dp"
    android:text="Hadoop" />

<TextView
    android:id="@+id/tWelcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="26dp"
    android:gravity="center"
    android:text="@string/Ref"
    android:textColor="#FFF"
    android:textSize="20sp" />

<TextView
    android:id="@+id/frontText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tWelcome"
    android:layout_centerHorizontal="true"
    android:textSize="18dp"
    android:layout_marginTop="16dp"
    android:text="Choose the IT Service provider"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFF" />

  </RelativeLayout>

Aws click layout

 <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.getit.AwsStartAct" 
android:background="#555" >


<TextView
    android:id="@+id/tChoose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tWelcome"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="11dp"
    android:text="Choose the type of Service you want"
    android:textColor="#FFF"
    android:textSize="18sp" />

<Button
    android:id="@+id/bCompute"
    android:layout_width="250sp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tChoose"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:gravity="center"
    android:text="Compute" />

<Button
    android:id="@+id/bStorage"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bCompute"
    android:layout_below="@+id/bCompute"
    android:layout_marginTop="41dp"
    android:text="Storage" />

<Button
    android:id="@+id/bDatabase"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bStorage"
    android:layout_below="@+id/bStorage"
    android:layout_marginTop="37dp"
    android:text="DataBase" />

    </RelativeLayout>

Stack Trace

07-23 22:09:28.518: E/AndroidRuntime(361): FATAL EXCEPTION: main
07-23 22:09:28.518: E/AndroidRuntime(361): java.lang.RuntimeException:    Unable to start activity      ComponentInfo{com.example.getit/com.example.getit.AwsStartAct}:    java.lang.NullPointerException
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.os.Looper.loop(Looper.java:123)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-23 22:09:28.518: E/AndroidRuntime(361):  at java.lang.reflect.Method.invokeNative(Native Method)
07-23 22:09:28.518: E/AndroidRuntime(361):  at java.lang.reflect.Method.invoke(Method.java:507)
07-23 22:09:28.518: E/AndroidRuntime(361):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-23 22:09:28.518: E/AndroidRuntime(361):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-23 22:09:28.518: E/AndroidRuntime(361):  at dalvik.system.NativeStart.main(Native Method)
07-23 22:09:28.518: E/AndroidRuntime(361): Caused by: java.lang.NullPointerException
07-23 22:09:28.518: E/AndroidRuntime(361):  at com.example.getit.AwsStartAct.onCreate(AwsStartAct.java:46)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-23 22:09:28.518: E/AndroidRuntime(361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-23 22:09:28.518: E/AndroidRuntime(361):  ... 11 more
Bajji
  • 2,243
  • 2
  • 22
  • 35
Sufyan Sher
  • 31
  • 2
  • 8

2 Answers2

3

In AwsStartAct onCreate() you're using the wrong id bHadoop for button Database. It's not in the layout, so findViewById() cannot find it and returns null.

This is the issue:

Database=(Button) findViewById (R.id.bHadoop);

Most likely it should be:

R.id.bDatabase
ci_
  • 8,594
  • 10
  • 39
  • 63
  • I m not working on data base now, I m jsut working on compute buttons, but that comes later, first page button are not working – Sufyan Sher Jul 23 '15 at 22:14
  • 1
    No it is working, but your second activity cannot be started because of the error I'm describing. It's completely obvious from your stack trace too. – ci_ Jul 23 '15 at 22:27
  • 1
    Yes @ci_ is correct, in the second activity you are setting R.layout.activity_aws_start as the contentView and that layout does not define bHadoop button, hence the NullPointerException. Your bHadoop button is in the first activity's layout. – Bajji Jul 23 '15 at 22:33
  • 1
    This line "07-23 22:09:28.518: E/AndroidRuntime(361): at com.example.getit.AwsStartAct.onCreate(AwsStartAct.java:46)" indicates that @ci_ is, indeed, correct about the location of the error. – David Berry Jul 23 '15 at 22:50
  • alright thanks guys.. really appricaitated. i will make these changes and let you guys know this weekend :) – Sufyan Sher Jul 24 '15 at 13:44
0

you call Database=(Button) findViewById (R.id.bHadoop); in the layout named activity_aws_start but in this layout do not exist any button whit id bHadoop, in this layout you named bCompute, bCompute and bDatabase this is the error your try Database=(Button) findViewById (R.id.bDatabase);