0

I have this code:

aktiv_date = dateaktiv.getText().toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
try {
     aktiv_dat = formatter.parse(aktiv_date);
    } catch (java.text.ParseException e) {
     e.printStackTrace();
    }

When I give it a number like: 12101989 or something similar, that does not have the format dd.MM.yyyy the program crashes because it says the data from aktiv_date is unparseable at line:

 aktiv_dat = formatter.parse(aktiv_date);

What could i do to make it not crash?

3
  • The Logcat log says the same thing i said up: javanullpointerexception and then it takes me to that line: aktiv_dat = formatter.parse(aktiv_date); Commented Oct 19, 2012 at 6:38
  • @rosualin: No, you said nothing about a NullPointerException in your post. It would really help if you'd post the information from the log. I've made a guess based on what you've told us so far, but it really is hard to work with such incomplete information. Commented Oct 19, 2012 at 6:43
  • @rosualin show your code about Calenedar programming formate.. Commented Oct 19, 2012 at 6:49

2 Answers 2

3

I suspect the problem is that you're catching the ParseException, dumping the exception, and then continuing as if nothing has gone wrong.

You haven't shown where aktiv_dat is declared, but I suspect it's got a value of null if the parse fails... hence the NullPointerException. Printing a stack trace and then continuing is almost never the right way of handling an exception. You should think about how you want your program to behave in the face of invalid data - do you want to use a default date, do you want to abandon just that operation, etc...

Sign up to request clarification or add additional context in comments.

1 Comment

10x, i'd changed the try catch to call the WS call into try, and if not to make a toast that the format is not correct
0

you can try edit like this:

SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.