-4

Please find below the main class

package com.example.myfirstapp;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

public void addListenerOnSpinnerItemSelection() {
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}

@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);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

Button buttonSend;
EditText textTo;
EditText textSubject;
EditText textMessageContact;
EditText textMessageEmail;
EditText textMessageAmount;
Spinner spinner;

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new    PlaceholderFragment()).commit();
    }

    buttonSend = (Button) findViewById(R.id.buttonSend);
    textTo = (EditText) findViewById(R.id.editTextSendTo);
    textSubject = (EditText) findViewById(R.id.editTextName);
    textMessageContact = (EditText) findViewById(R.id.editTextContact);
    textMessageEmail = (EditText) findViewById(R.id.editTextEmail);
    textMessageAmount = (EditText) findViewById(R.id.editTextAmount);
    spinner = (Spinner) findViewById(R.id.spinner);
    buttonSend.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.setType("message/rfc822");
            startActivity(Intent.createChooser(email,
                    "Choose an Email client :"));

        }
    });
}

  }

Also please find below the xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- android:background="@drawable/mmm_bg" -->

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="40dip" >

    <TableRow>

        <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Name:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextName"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="enter the name"
            android:textColor="#000000"
            android:textStyle="bold" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewContact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Contact:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextContact"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="enter the contact no."
            android:textStyle="bold" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Email: "
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextEmail"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="enter the email address"
            android:textColorHint="#008080" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewProduct"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Product:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/product_array" />

        <requestFocus />

        <EditText>
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewAmount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Amount:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextAmount"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="enter the amount"
            android:textStyle="bold" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewSendTo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Send To:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextSendTo"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="reciever&apos;s email address"
            android:textStyle="bold" >

            <requestFocus />
        </EditText>
    </TableRow>
  </TableLayout>

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Send" />
   </LinearLayout>

  </LinearLayout>

please find below the log cat

05-13 09:03:49.935: E/AndroidRuntime(14062): Caused by: java.lang.NullPointerException
05-13 09:03:49.935: E/AndroidRuntime(14062):    at           com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:89)
05-13 09:03:49.935: E/AndroidRuntime(14062):    at   android.app.Activity.performCreate(Activity.java:5231)
05-13 09:03:49.935: E/AndroidRuntime(14062):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-13 09:03:49.935: E/AndroidRuntime(14062):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-13 09:03:49.935: E/AndroidRuntime(14062):    ... 11 more
05-13 09:08:50.395: I/Process(14062): Sending signal. PID: 14062 SIG: 9

I am trying to create an app which has 4 to 5 textbox and spinner and send button. On clicking the send button the email should go to the recipient with the details entered in text boxes. but there occurs null pointer exception. I am unable to solve the problem. Why is the NPE happening and how to resolve it .

Thanks in advance. Please help me to get through this.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102

2 Answers2

0

A null pointer exception is very common and means you're trying to access a method or property on an object that is null. Obviously it would not be possible to call such a method (as it doesn't exist).

I suspect that either textTo or textSubject is null, because those are user defined objects. If you set a breakpoint on the line where you set the variable textTo (for example), you should notice that it is null.

When you find the value that is null, then you can figure out how to remedy the situation. If it turns out to be a variable like 'textTo', then check that the resource actually exists, or that you are casting it to the correct object

Shane
  • 150
  • 6
-4

Just write your if loop after declaration of your all the views in your onCreate() as below:

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

     buttonSend = (Button) findViewById(R.id.buttonSend);
    textTo = (EditText) findViewById(R.id.editTextSendTo);
    textSubject = (EditText) findViewById(R.id.editTextName);
    textMessageContact = (EditText) findViewById(R.id.editTextContact);
    textMessageEmail = (EditText) findViewById(R.id.editTextEmail);
    textMessageAmount = (EditText) findViewById(R.id.editTextAmount);
    spinner = (Spinner) findViewById(R.id.spinner);
    buttonSend.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.setType("message/rfc822");
            startActivity(Intent.createChooser(email,
                    "Choose an Email client :"));

        }
    });

 if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new    PlaceholderFragment()).commit();
    }
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Why down voted though the OP have already mark it as correct?? Answer please downvoters – GrIsHu May 14 '14 at 04:25
  • 1
    I'm one of the downvoters but I can only speak for myself. I downvoted because this isn't a solution to OP's problem. Moving the if block around changes really nothing. Also, code dumps without explanations are not really helpful in learning. As for the accept: looks like it's the first accepted answer for @user3588838 and was accepted for some other reason than solving the problem (such as by accident or ignorance). – laalto May 14 '14 at 06:56