1
votes

I'm making an application where i'm trying to get object of edittext but it is returning null and fatal exception is raised because of this.

translateActivity.java

package diverse.technologies.transcriber;

public class translateActivity extends AppCompatActivity {

Button go;
TextView tv;
EditText et;
TableLayout tl,tml;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    tl = (TableLayout) findViewById(R.id.tablelayouttexthistory);
    tml = (TableLayout) findViewById(R.id.tablemainlayout);
    et = (EditText) findViewById(R.id.entertext);
    tv = (TextView) findViewById(R.id.textView2);
    go = (Button) findViewById(R.id.button);
    et.clearFocus();
    if(tml.requestFocus())
        Log.d("focus","got on table");
    else
        Log.d("focus","couldn't get");
    showhistory();   //showing history

    //other code
}

activity_translate.xml

<?xml version="1.0" encoding="utf-8"?>
<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="diverse.technologies.transcriber.translateActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tablemainlayout">

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <EditText
                    android:layout_height="89dp"
                    android:id="@+id/entertext"
                    android:text="Enter Text(English)"
                    android:layout_marginTop="20dp"
                    android:layout_width="206dp" />

                <Button
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Go"
                    android:id="@+id/button"
                    android:gravity="left|center_vertical"
                    android:layout_marginTop="22dp" />
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="280dp"
                    android:layout_height="wrap_content"
                    android:text="Translated Text(Gujarati)"
                    android:id="@+id/textView2"
                    android:layout_row="1"
                    android:layout_columnSpan="2"
                    android:layout_marginTop="7dp"
                    android:textSize="25dp"
                    android:layout_span="2" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TableLayout
                    android:id="@+id/tablelayouttexthistory"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_span="2">
                </TableLayout>
            </TableRow>

        </TableLayout>

    </ScrollView>

</RelativeLayout>

Exception that I'm getting:

E/AndroidRuntime: FATAL EXCEPTION: main Process: diverse.technologies.transcriber, PID: 4447 java.lang.RuntimeException: Unable to start activity ComponentInfo{diverse.technologies.transcriber/diverse.technologies.transcriber.translateActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.clearFocus()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569) at android.app.ActivityThread.access$900(ActivityThread.java:150) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:168) at android.app.ActivityThread.main(ActivityThread.java:5885) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.clearFocus()' on a null object reference at diverse.technologies.transcriber.translateActivity.onCreate(translateActivity.java:59) at android.app.Activity.performCreate(Activity.java:6262) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)  at android.app.ActivityThread.access$900(ActivityThread.java:150)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:168)  at android.app.ActivityThread.main(ActivityThread.java:5885)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

Please help. I'm so stuck here..

2
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.clearFocus()' on a null object referenceCharuක
but why it is returning a null pointer when i'm getting it correctly with the corrrect id ?This method works fine in other activities. @CharuකAbhishek Panjabi

2 Answers

3
votes

You are not setting the content view of the activity and directly finding the view.

Set it using

setContentView(R.layout.activity_translate);
0
votes

You forgot to add the layout for your activity, add setContentView(R.layout.activity_translate); in your onCreate() method after super.onCreate(savedInstanceState); line