title: Search record and retrieve it from the database in Android
body: <p>In my activity, I have editText's <strong>(Username, Firstname, Lastname and Email Address)</strong>. When the user input an existing username, he can search it by clicking the <strong>Search Button</strong> and the data which the user have will appear to the editText's. </p>
  
  <p>MainActivity.java</p>
  
  <pre><code>btn_Search.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
          String searchableUser = txt_User.getText().toString();
  
          ConsUserRegistration consUserRegistration = db.searchUser(searchableUser);
  
          String searchUser = consUserRegistration.getUser().toString();
          String searchFirst = consUserRegistration.getFirstName().toString();
          String searchLast = consUserRegistration.getLastName().toString();
          String searchEmail = consUserRegistration.getEmail().toString();
  
          txt_User.setText(searchUser);
          txt_First.setText(searchFirst);
          txt_Last.setText(searchLast);
          txt_Email.setText(searchEmail);             
      }
  });
  </code></pre>
  
  <p>DatabaseHandler.java</p>
  
  <pre><code>public ConsUserRegistration searchUser(String username){
  
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor cursor = db.query(Constants.TABLE_USER, new String[] {Constants.KEY_USER, Constants.KEY_FIRST, 
              Constants.KEY_LAST, Constants.KEY_EMAIL}, Constants.KEY_USER + " =? ", 
              new String[] { String.valueOf(username) }, null, null, null);
      if (cursor != null)
          cursor.moveToFirst();
  
      ConsUserRegistration search = new ConsUserRegistration (cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3));
  
      return search;
  
  }
  </code></pre>
  
  <p>But when the user click the Search Button and didn't input any character from the Username EditText, I am getting a <code>CursorIndexOutOfBoundsException</code> error in the DatabaseHandler in line: ConsUserRegistration search = new ConsUserRegistration (cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3)); </p>
  
  <p>Also, I couldn't retrieve the data from the database since it's crashing.</p>