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 6591291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:24:21+00:00 2026-05-25T17:24:21+00:00

I am using this function to insert into the database. I’d like to validate

  • 0

I am using this function to insert into the database. I’d like to validate inputs from two edittext fields. Whenever I push ok button without giving any inputs, the program crashes.I tried to print the values as well, but it didnt display in logcat.Am i doing anything wrong?

    private void add() {
    LayoutInflater inflater=LayoutInflater.from(this);
    final View addView=inflater.inflate(R.layout.add_country, null);

    new AlertDialog.Builder(this)
        .setTitle("Add new country/year")
        .setView(addView)
        .setPositiveButton("OK", 
                new DialogInterface.OnClickListener() {
                                                public void onClickDialogInterface                                                       dialog,int whichButton) {
                        /* Read alert input */
                        EditText editCountry =(EditText)addView.findViewById(R.id.editCountry);
                        String country = editCountry.getText().toString();
                        EditText editYear =(EditText)addView.findViewById(R.id.editYear);
                        int year = Integer.parseInt( editYear.getText().toString() );
                        if(editCountry.getText().toString().trim().length()>0 && editYear.getText().toString().trim().length()>0){
                            /* Open DB and add new entry */
                            db.open(); 
                            db.insertEntry(country,year);

                            /* Create new cursor to update list. Must be possible to handle
                             * in a better way. */
                            entryCursor = db.fetchAllEntries();    // all country/year pairs                            
                            adapter = new SimpleCursorAdapter(CountryEditor.this,
                                    R.layout.country_row,entryCursor,
                                    new String[] {"country", "year"},
                                    new int[] {R.id.country, R.id.year});
                            setListAdapter(adapter);
                            db.close();
                        }
                        else{
                            Toast.makeText(CountryEditor.this,
                                    "You need to enter Country AND Year.", Toast.LENGTH_LONG).show();
                        }
                    }
                })
        .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int whichButton) {
                        // ignore, just dismiss
                    }
                })
        .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-05-25T17:24:21+00:00Added an answer on May 25, 2026 at 5:24 pm

    You are calling editBlah.getText().toString() which can return “”;

    When parsing this to an integer an Exception will be thrown.

    ( It could also be, if you call .getText() on a view which has initialised to null (ie, you have incorrectly specified the id for the ID you want) a NullPointerException will be thrown. Without the Stacktrace you wouldn’t be able to tell which – try and post your stack trace with the question where possible ).

    You’re question is correct – What you need to do is validate the input you’re getting: ie:

    int year = Integer.parseInt( editYear.getText().toString() );
    

    should be:

    if(editYear.getText().toString().equalsIgnoreCase("")) {
        // Cannot parse into an int therefore perform some action which will notify the 
        // user they haven't entered the correct value.
    }
    

    Or even the following if you are already going to be validating your int values:

    int year = Integer.parseInt( editYear.getText().toString().equalsIgnoreCase("") ? 
            "-1" : editYear.getText().toString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am protecting my string using this code to insert it into the database:
I'm trying to authenticate before using ajax to insert into a database $('#button').click(function ()
I've been using this function but I'd like to know what's the most efficient
I'm using this function to extract files from .zip archive and store it on
I am using QSqlQuery to insert data into a MySQL database. Currently all I
I am using this code to insert the data in my database but it
I am using this function to close existing form and open a new form.
so I'm deploying a shipping calculator using this function , and I'm getting this
i am making an upload with Ajaxupload plugin and i am using this function
I'm using this simple function: def print_players(players): tot = 1 for p in players:

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.