1

This question has been asked previously and I tried all solutions and it is still not working. I am new to android and can do silly mistakes. In my java file when i try to find a control(Edittext) it gives me nullpointer exception. Layout xml file is as below:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/floorwood" >


    <ScrollView android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    <LinearLayout android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical">

      <View
         android:layout_width="fill_parent"
         android:layout_height="4dp"
         android:background="#c0c0c0"/>

       <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#ffcc33"
        android:background="@null"
        android:text="Deduction" />


      <View
         android:layout_width="fill_parent"
         android:layout_height="4dp"
         android:background="#c0c0c0"/>


 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="Actual House Rent Paid" ></TextView>

    <EditText
        android:id="@+id/hrp"
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"
         android:text="0"/>


 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="HRA given by the employee" ></TextView>

      <EditText
        android:id="@+id/hraemp" 
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"/>

    <View
         android:layout_width="fill_parent"
         android:layout_height="2dp"
         android:background="#c0c0c0"/>




 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="80C Deduction" />


   <EditText
        android:id="@+id/dedu_80c"
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"/>



 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="Home Loan Interest" />


    <EditText
        android:id="@+id/homeloaninterest"
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"/>



     <View
         android:layout_width="fill_parent"
         android:layout_height="2dp"
         android:background="#c0c0c0"/>



 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="Medical Insurance(self)" />


    <EditText
        android:id="@+id/medi_ins_self"
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"/>





 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="Medical Insurance(Dependents)" />

     <EditText
        android:id="@+id/medi_ins_depe"
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"/>

     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="Dependents Age" />


    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF" >

        <RadioButton
            android:id="@+id/radiobtndepen1"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:ems="15"
            android:padding="2dp"
            android:background="@null"
            android:textSize="15dp"
            android:checked="true"
            android:text="[1-59]" />

        <RadioButton
            android:id="@+id/radiobtndepen2"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:ems="15"
            android:padding="2dp"
            android:background="@null"
            android:textSize="15dp"
            android:checked="true"
            android:text="[>59]" />
        </RadioGroup>


      <View
         android:layout_width="fill_parent"
         android:layout_height="2dp"
         android:background="#c0c0c0"/>


 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="Medical Reimbursement" />

        <EditText
        android:id="@+id/medi_reim"
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"/>



 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="Conveyance Allowance" />

        <EditText
        android:id="@+id/conv_allo"
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"/>



 <TextView 
        android:id="@+id/lbl1"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:ems="20"
        android:textSize="15dp"
        android:textColor="#FFF"
        android:background="@null"
        android:text="Professional Tax" />

        <EditText
         android:id="@+id/prof_tax"
         android:layout_width="wrap_content" 
         android:layout_height="20dp" 
         android:ems="10"
         android:layout_gravity="right"
         android:padding="2dp"
         android:background="@null"
         android:textSize="15dp"
         android:textColor="#FFF"
         android:inputType="number"
         android:paddingLeft="30dp"/>


     <View
         android:layout_width="fill_parent"
         android:layout_height="2dp"
         android:background="#c0c0c0"/>

     </LinearLayout>
     </ScrollView>


</LinearLayout>

Java code is as below:

   public class Deduction extends Activity {

    /** Called when the activity is first created. */
    EditText hrp;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.deduction);
          hrp = (EditText) findViewById(R.id.hrp);
        }

        String shrp=hrp.getText().toString();
}

I could not identify the mistake, why it is giving NullPointerException

Rajen Raiyarela
  • 5,050
  • 4
  • 18
  • 39
  • 1
    1) Are you sure that your edittext is in `deduction.xml` file? 2) Move this line `String shrp=hrp.getText().toString();` to somewhere else on click listener because if you move to `onCreate()` then it will be empty. – Piyush May 05 '15 at 04:38
  • Actually, it won't be empty if rad in onCreate(), it will be "0" because that value is pre-filled in the layout. But you are right that a user-entered value would only be available later. – Chris Stratton May 05 '15 at 13:17

2 Answers2

2

The answer will be found by indenting your code to match the brackets:

public class Deduction extends Activity {

    /** Called when the activity is first created. */
    EditText hrp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deduction);
        hrp = (EditText) findViewById(R.id.hrp);
    }

    String shrp=hrp.getText().toString();
}

Then we can notice that you try to use the hrp EditText outside of any of the methods, which is to say before it has been assigned a value in onCreate().

Move this code into onCreate() or a lifecycle or UI method called subsequently, for example:

public class Deduction extends Activity {

    String shrp; //careful, this will be null until assigned!

    /** Called when the activity is first created. */
    EditText hrp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deduction);
        hrp = (EditText) findViewById(R.id.hrp);

        shrp=hrp.getText().toString();
    }
}

Of course, if you don't have an edit text with that identity in the layout, it will still NPE where it attempts to use that - in that case, check and make sure that you set the layout xml you posted as your content view, and that you have done a full clean and re-build since you last edited it.

Chris Stratton
  • 38,489
  • 6
  • 80
  • 115
0

Try to get EditText value on particular listener, add a button in layout then use the below code:

    public class Deduction extends Activity {

        String shrp; 
        EditText hrp;
        Button btnGetValue;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.deduction);
            hrp = (EditText) findViewById(R.id.hrp);
            btnGetValue= (Button) findViewById(R.id.btnGetValue);

           btnGetValue.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // Perform action on click   
                     shrp=hrp.getText().toString();
                    }
                });
        }
    }
AmDroid
  • 131
  • 5