2 Replies - 6128 Views - Last Post: 24 September 2012 - 05:03 PM

#1 emyjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-August 12

EditText string validation in androids

Posted 22 September 2012 - 07:44 AM

Hello again.

i have currently started to learn how to create apps for android phones using java and it been great i tell you.i used to develop for desktops and now i'm laying my hands on mobile as part time.i am creating an app in android which has an edit text some kind of a text field and i'm trying to validate the text string inputted by the user to ensure that a string text is inputted.but the problem is the app crashes when i press a button e.g log in button which check for validation and when i check log cat,there's this NullPointerException it's throwing at the particular line i commented in this code.please can someone with better eyes look at the codes for me and point out my mistakes.i'd appreciate it

public class LoginFragmentActivity extends SherlockFragment {

        // creating text fields with a log in button

	EditText e1 = null;
	EditText e2 = null;
	Button b;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInatanceState) {
		View view = inflater.inflate(R.layout.login_layout, container, false);

                   // instantiating each text field and button

		e1 = (EditText) view.findViewById(R.id.reg_email);
		e2 = (EditText) view.findViewById(R.id.reg_password);

		b = (Button) view.findViewById(R.id.btnLogin);

		b.setonclickListener(new View.onclickListener() {

			@Override
			public void onclick(View arg0) {

                                // assigning the text obtained form the text fields to a string variable

				String email = e1.getText().toString(); // NullPointerException is raised at this line
				String pname = e2.getText().toString();

                                // validating inputs.

				if ((email.equals("")) || (pname.equals(""))) {

					AlertDialog.Builder builder = new AlertDialog.Builder(
							getActivity());
					builder.setMessage("Please fill in all fields");
					builder.setCancelable(false);

					builder.setPositiveButton("Ok",
							new DialogInterface.onclickListener() {

								@Override
								public void onclick(DialogInterface dialog,
										int which) {

									dialog.cancel();
								}
							});
					AlertDialog alert = builder.create();
					alert.show();

				} else {
					Toast.makeText(getActivity(), "Welcome",
							Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(getActivity(), Home.class);
					startActivity(intent);
				}

			}
		});
		return view;
	}

}




The funny thing is that this is the same line of codes i wrote for another activity and it's perfectly working with no errors.i'm confused!!.

Is This A Good Question/Topic? 0
  • +

Replies To: EditText string validation in androids

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Re: EditText string validation in androids

Posted 22 September 2012 - 08:27 AM

My assumption would be that e1 and e2 are not being instantiated before you call on them. That would also mean that the ids that you specified are not being included into the resources correctly. You should use the Eclipse debugger and put a breakpoints right after you assign values to e1 and e2 to see if they remain null.

Moved to Android Forum!
Was This Post Helpful? 0
  • +
  • -

#3 emyjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-August 12

Re: EditText string validation in androids

Posted 24 September 2012 - 05:03 PM

if it were so,this is the same code i wrote for another activity called RegisterFragment and it worked like magic.now how come that didn't raise any flags,i wonder?

i even had to copy and paste just to be sure i make no mistakes since i was going to re-write the same thing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1