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

  • Home
  • SEARCH
  • 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 8324289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:00:50+00:00 2026-06-09T00:00:50+00:00

I’m new to Java/android so a lot of these terms are foreign but am

  • 0

I’m new to Java/android so a lot of these terms are foreign but am willing to learn. I’m not gonna go into detail on the app as I dont think it’s relevant. My issue as it stands, I’ve used tutorials and pieces of code from a blog and have gotten my code to work. Trying to clean up and organize my code I get a nullpoiner exception when I move one line (creating my autocompletetextview). Below is the code I’ve used. My 1 line of code that’s giving me an issue is

AutoCompleteTextView companyAutoComplete = (AutoCompleteTextView) addAddressDialog.findViewById(R.id.add_record_dialog_autocomplete);

When I move it to right under the start of my function it errors out but when left in place it works like a charm. I’d like to understand why this is.

public void addAddress() {
    final Dialog addAddressDialog = new Dialog(this);
    final int[] to = new int[] { android.R.id.text1 };
    final String[] from = new String[] { "CompanyName" };

    // Create a SimpleCursorAdapter for the CompanyName field.
    SimpleCursorAdapter adapter =  new SimpleCursorAdapter(this, android.R.layout. select_dialog_item, null, from, to);

    addAddressDialog.setContentView(R.layout.add_record_dialog);
    addAddressDialog.setTitle(getString(R.string.add_record_dialog_address_title));
    addAddressDialog.setCancelable(true);

    final EditText text1 = (EditText) addAddressDialog.findViewById(R.id.add_record_dialog_edittext);
    text1.setHint(getString(R.string.add_record_dialog_company_hint));

    Button buttonOK1 = (Button) addAddressDialog.findViewById(R.id.add_record_dialog_ok);
    buttonOK1.setText(getString(R.string.add_record_dialog_ok_button));

    Button buttonCancel1 = (Button) addAddressDialog.findViewById(R.id.add_record_dialog_cancel);
    buttonCancel1.setText(getString(R.string.add_record_dialog_cancel_button));

    buttonOK1.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            Bundle addressBundle = new Bundle();
            addressBundle.putString("CompanyName", text1.getText().toString());

            Intent intent = new Intent(MenuActivity.this, AddAddressActivity.class);
            intent.putExtras(addressBundle);
            startActivity(intent);

            addAddressDialog.dismiss();
        }   
    });

    buttonCancel1.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            Toast.makeText(getBaseContext(), "Cancel button clicked", Toast.LENGTH_SHORT).show();
            addAddressDialog.dismiss();
        }   
    });
    AutoCompleteTextView companyAutoComplete = (AutoCompleteTextView) addAddressDialog.findViewById(R.id.add_record_dialog_autocomplete);

    companyAutoComplete.setAdapter(adapter);

    // Set an OnItemClickListener, to update dependent fields when
    // a choice is made in the AutoCompleteTextView.
    companyAutoComplete.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> listView, View view,
                    int position, long id) {
            // Get the cursor, positioned to the corresponding row in the
            // result set
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);

            // Get the CompanyID from this row in the database.
            String companyID = cursor.getString(cursor.getColumnIndexOrThrow("_id"));

            // test to make sure CompanyID returned
            Toast.makeText(getBaseContext(), companyID, Toast.LENGTH_SHORT).show();
        }
    });

    // Set the CursorToStringConverter, to provide the labels for the
    // choices to be displayed in the AutoCompleteTextView.
    adapter.setCursorToStringConverter(new CursorToStringConverter() {
        public String convertToString(android.database.Cursor cursor) {
            // Get the label for this row out of the "CompanyName" column
            final int columnIndex = cursor.getColumnIndexOrThrow("CompanyName");
            final String str = cursor.getString(columnIndex);
            return str;
        }
    });

    // Set the FilterQueryProvider, to run queries for choices
    // that match the specified input.
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            Cursor cursorReturn = dbAdapter.getCompanies(constraint != null ? constraint.toString() : null);

            startManagingCursor(cursorReturn);
            return cursorReturn;
        }
        });

    addAddressDialog.show();
}
  • 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-09T00:00:51+00:00Added an answer on June 9, 2026 at 12:00 am

    This happens because you call setContentView later.

    setContentView sets up the layout for the addAddressDialog dialog. If you don’t call setContentView, it has no layout items, therefore addAddressDialog.findViewById(...); will be null, and, obviously you cannot cast that to anything, nor can you call setHint on it.

    It shouldn’t matter where this line of code is in your method, as long as your line with setContentView is called before it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.