• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Failed Android Test case with nullpointer

Hi,

I am adding an android testcase for the UI. I have been following the tuto for the Spinner Test.
Unfortunately I have a nullpointerexception on a object medtSalePercent.setText("10"); which has been initialised.

here the code :

Code:
import jle.base.WorkActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;
import android.widget.TextView;


public class WorkActivityTest extends ActivityInstrumentationTestCase2<WorkActivity> {

    // The Application object for the application under test

    private WorkActivity mActivity;
    private EditText medtPriceValue;
    private EditText medtSalePercent;
    //private TextView medtResult;
    
    public WorkActivityTest() {
        super("jle.base", WorkActivity.class);
        // TODO Auto-generated constructor stub
    }
    
      /*
     * Sets up the test environment before each test.
     * @see android.test.ActivityInstrumentationTestCase2#setUp()
     */
    @Override
    protected void setUp() throws Exception {

        /*
         * Call the super constructor (required by JUnit)
         */

        super.setUp();
        
        setActivityInitialTouchMode(false);
        
        mActivity = this.getActivity();
        
        medtPriceValue =  (EditText)mActivity.findViewById(jle.base.R.id.edtPriceValue);
        medtSalePercent= (EditText)mActivity.findViewById(jle.base.R.id.edtSaleDiscount);
      /*  medtResult= (TextView)mActivity.findViewById(jle.base.R.id.txvSalePrice);*/
        
    }
    
    public void testPriceValueUI() {
        mActivity.runOnUiThread(
                new Runnable() {
                    public void run() {
                        medtPriceValue.requestFocus();
                        medtPriceValue.setText("50");
                        medtSalePercent.setText("10");
                        /*medtSalePercent.requestFocus();
                        medtSalePercent.setText("10");*/
                    }
                }
            );
        
        
        
    }
    
    
    
    public void testStateDestroy() {

        // Halt the Activity by calling Activity.finish() on it

        mActivity.finish();

 
    }

}


There is no exception on medtPriceValue.setText("50");.
but there is one on the screen for medtSalePercent.setText("10");

Why is that ?


Thanks
 
You haven't attached the XML layout file so it is hard to tell exactly what is wrong.

However, since medtPriceValue and medtSalePercent are very similar (both are text fields), it should be pretty easy to compare and find out what is wrong.

Also for percentage display, you need to set the Max value as well.

Hope this helps.

Shengxin
 
Upvote 0
Thank you for the answer.

I did not see lot of differences between the 2 EditText.
Here is the main.xml file :

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="500dp"
    android:background="@drawable/wooman_bag"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/rlaMainLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/txvOriginalPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="26dp"
            android:text="@string/PriceTest"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="25dp" />




        <EditText
            android:id="@+id/edtPriceValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/txvOriginalPrice"
            android:layout_below="@+id/txvOriginalPrice"
            android:inputType="numberDecimal"
            android:maxLength="7"
            android:textSize="25dp" >

        </EditText>

        <TextView
            android:id="@+id/txvSalePercentage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/edtPriceValue"
            android:layout_marginTop="54dp"
            android:text="@string/SaleDiscount"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="25dp" />




        <EditText
            android:id="@+id/edtSaleDiscount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/edtPriceValue"
            android:layout_below="@+id/txvSalePercentage"
            android:inputType="numberDecimal"
            android:maxLength="7"
            android:textSize="25dp" />

        <Button
            android:id="@+id/btnCalculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/edtSaleDiscount"
            android:layout_marginTop="68dp"
            android:text="@string/CalculatePrice"
            android:textSize="25dp" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/btnCalculate"
            android:layout_alignBottom="@+id/btnCalculate"
            android:layout_toRightOf="@+id/btnCalculate"
            android:text="@string/Reset"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/txvSalePrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/SalePrice"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="25dp" android:visibility="invisible" android:layout_below="@+id/btnReset" android:layout_centerHorizontal="true"/>






        <TextView
            android:id="@+id/txvResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_below="@+id/txvSalePrice"
            android:layout_marginTop="14dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="40dp" android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</LinearLayout>

Do you have an idea of the problem ?
 
Upvote 0
Sorry that I cannot tell for sure from the code/XML segments you posted since these two fields are almost identical. I think the problem is caused by something not in the posted code/XML segments. Some generic testing techniques I can think about now to pinpoint the problem are:
1. Comment out medtPriceValue to see if you still have the NullPointerException, this will rule out the possibility that the problem is related to the other field
2. Switch the order of medtPriceValue or medtSalePercent to see if the issue is related to field initialization
3. Run the original examples from Google, make it work then change the code piece by piece. The original examples must work.

Hope this helps.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones