Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:14:45+00:00 2026-06-17T05:14:45+00:00

I am encountering a null pointer exception whenever i click the OK button in

  • 0

I am encountering a null pointer exception whenever i click the “OK” button in my dialog box.

public class TestActivity extends ListActivity {

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_bucket_crud);

    }

    public void addTestItem(View view) {
        LayoutInflater inflater = getLayoutInflater();
        new AlertDialog.Builder(this)
            .setTitle("Add Item")
            .setIcon(R.drawable.add)
            .setView(inflater.inflate(R.layout.activity_bucket_crudadd, null))
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    EditText text = (EditText)findViewById(R.id.editTextAdd);
                    DatabaseHandler myDbHelper = new DatabaseHandler(context);
                    myDbHelper.insertItem(text.getText().toString());
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            }).show();
    }

}

Here is the error

01-05 09:20:35.052: E/AndroidRuntime(761): FATAL EXCEPTION: main
01-05 09:20:35.052: E/AndroidRuntime(761): java.lang.NullPointerException
01-05 09:20:35.052: E/AndroidRuntime(761):  at com.example.test.TestActivity$1.onClick(TestActivity.java:65)
01-05 09:20:35.052: E/AndroidRuntime(761):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)

My layout it this

<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="150dp">

    <EditText
        android:id="@+id/editTextAdd"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textMultiLine"
        android:layout_margin="5dp" 
        android:padding="3dp">

        <requestFocus />
    </EditText>

</RelativeLayout>

It seems like when i enter the text inside the EditText, the value is not being read..

EditText text = (EditText)findViewById(R.id.editTextAdd);

Is this the cause of the null pointer because the line points to (text.getText().toString()? If it is, how can i get the value of the text?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:14:46+00:00Added an answer on June 17, 2026 at 5:14 am

    Because EditText text is NULL. You are not referring EditText text from Dialog Layout.

    Just Change your line something like, and let me know what happen..

     EditText text = (EditText) dialog.findViewById(R.id.editTextAdd);
    

    To get EditText reference from Dialog Layout you have to use dialog reference with findViewById() method.

    Update:

    LayoutInflater inflater = getLayoutInflater();
    View view = null;
    view  = inflater.inflate(R.layout.activity_bucket_crudadd, null);
            new AlertDialog.Builder(this)
                .setTitle("Add Item")
                .setIcon(R.drawable.add)
                .setView(view)
    

    And now access edit text like,

    EditText text = (EditText) view.findViewById(R.id.editTextAdd);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm encountering some difficulty with some CSS I'm coding. Whenever I minimise the window
I'm encountering some problem with my jsp code. Here is the code [Get_Values.java]: public
I am encountering cursor index out of bounds exception. My code is as follows.
I am encountering an exception when trying to execute following java code: package testSikuliPackage;
I've encountering an unusual problem with .Net Framework 3.5 and the System.Xml.XmlReader class. Before
I am encountering this error whenever i do a mp.stop in android, basically an
I am encountering this following CXF exception: warning: Interceptor for {http://example.com/wsdl/esc/2011-12-12/}AmazonEC2#{http://example.com/wsdl/esc/2011-12-12/}NewDescribeImages has thrown exception,
I was encountering issues on a page with a XHTML doctype sent with MIME
I am encountering problems writing XML files using PHP. The following is my php
I'm encountering a strange issue, where sqlsrv_query() returns a valid statement resource, but sqlsrv_fetch_array()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.