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?
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_datis declared, but I suspect it’s got a value ofnullif the parse fails… hence theNullPointerException. 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…