Android Day of Week Calculator
-
I have created program that calculates day of week when entered date in format "January 7 2000" .Why do I get NullPointerException exception when I click button to calculate day of week? Here is source code of app:
public class MainActivity extends AppCompatActivity
{
private int position;
private int value;
private Button button;
private EditText editText;
private TextView textView1,textView2;
private Spinner spinner,spinner2;
private ArrayAdapter adapter;private boolean isValid; private Date date; private int inMonth, inDay, inYear; static boolean isDateValid(int month, int day, int year) { boolean validation = true; int\[\] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month < 1 || month > 12) { validation = false; } if (day < 1 || day > daysInMonth\[month - 1\]) { validation = false; } if (year < 1700 || year > 3000) { validation = false; } return validation; } static String zellerCalc(int month, int day, int year) { String dayOfWeek; int m = -1; int h = -1; int q = day; int k; int j; if (month == 1) { m = 13; } else if (month == 2) { m = 14; } else { m = month; } if (m == 13 || m == 14) { year--; } k = year % 100; j = year / 100; h = (q + (int)((13 \* (m + 1)) / 5.0) + k + (int)(k / 4.0) + (int)(j / 4.0) + (5 \* j)) % 7; if (h == 0) { dayOfWeek = "Subota"; } else if (h == 1) { dayOfWeek = "Nedelja"; } else if (h == 2) { dayOfWeek = "Ponedeljak"; } else if (h == 3) { dayOfWeek = "Utorak"; } else if (h == 4) { dayOfWeek = "Sreda"; } else if (h == 5) { dayOfWeek = "Četvrtak"; } else dayOfWeek = "Petak"; return dayOfWeek; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity\_main); Too
-
I have created program that calculates day of week when entered date in format "January 7 2000" .Why do I get NullPointerException exception when I click button to calculate day of week? Here is source code of app:
public class MainActivity extends AppCompatActivity
{
private int position;
private int value;
private Button button;
private EditText editText;
private TextView textView1,textView2;
private Spinner spinner,spinner2;
private ArrayAdapter adapter;private boolean isValid; private Date date; private int inMonth, inDay, inYear; static boolean isDateValid(int month, int day, int year) { boolean validation = true; int\[\] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month < 1 || month > 12) { validation = false; } if (day < 1 || day > daysInMonth\[month - 1\]) { validation = false; } if (year < 1700 || year > 3000) { validation = false; } return validation; } static String zellerCalc(int month, int day, int year) { String dayOfWeek; int m = -1; int h = -1; int q = day; int k; int j; if (month == 1) { m = 13; } else if (month == 2) { m = 14; } else { m = month; } if (m == 13 || m == 14) { year--; } k = year % 100; j = year / 100; h = (q + (int)((13 \* (m + 1)) / 5.0) + k + (int)(k / 4.0) + (int)(j / 4.0) + (5 \* j)) % 7; if (h == 0) { dayOfWeek = "Subota"; } else if (h == 1) { dayOfWeek = "Nedelja"; } else if (h == 2) { dayOfWeek = "Ponedeljak"; } else if (h == 3) { dayOfWeek = "Utorak"; } else if (h == 4) { dayOfWeek = "Sreda"; } else if (h == 5) { dayOfWeek = "Četvrtak"; } else dayOfWeek = "Petak"; return dayOfWeek; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity\_main); Too
Seriously? A
NullPointerException
is being thrown on line 230 becausedate
isnull
. A simple walkthrough with the debugger would have revealed this. :doh:"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Seriously? A
NullPointerException
is being thrown on line 230 becausedate
isnull
. A simple walkthrough with the debugger would have revealed this. :doh:"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
I have initialized Date date = new Date(); but now I get application has stopped working and error is still in the same line!!!
try
{
Date date = new Date();
String getdate = spinner.getItemAtPosition(position).toString() + value
+ textView2.getText().toString();
date = sdf.parse(getdate);
} catch (ParseException ex)
{
// handle parsing exception if date string was different from the pattern applying into the SimpleDateFormat contructor
}
// isDateValid function call
isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear()); -
I have initialized Date date = new Date(); but now I get application has stopped working and error is still in the same line!!!
try
{
Date date = new Date();
String getdate = spinner.getItemAtPosition(position).toString() + value
+ textView2.getText().toString();
date = sdf.parse(getdate);
} catch (ParseException ex)
{
// handle parsing exception if date string was different from the pattern applying into the SimpleDateFormat contructor
}
// isDateValid function call
isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear());Pavlex4 wrote:
I have initialized Date date = new Date();
Which is not the one being passed to
isDateValid()
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Pavlex4 wrote:
I have initialized Date date = new Date();
Which is not the one being passed to
isDateValid()
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Maybe look at (or around) line 12.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Maybe look at (or around) line 12.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Correct.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Correct.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I added date = new Date(); to button onclicklistener but when I click button inside app it gets pressed and stays like that and nothing happens!!!
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String dayOfWeek;
boolean isValid;**date = new Date();** Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd\_HHmmss"); String strDate = sdf.format(c.getTime());
-
I added date = new Date(); to button onclicklistener but when I click button inside app it gets pressed and stays like that and nothing happens!!!
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String dayOfWeek;
boolean isValid;**date = new Date();** Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd\_HHmmss"); String strDate = sdf.format(c.getTime());
You really need to make more effort to do some debugging of your code first. You have a problem which could be caused by any part of your code, so you need to do the troubleshooting to collect more information. Also you have a try/catch block in your code that reads:
try { String getdate = spinner.getItemAtPosition(position).toString() + value + textView2.getText().toString(); Date date = sdf.parse(getdate); } catch (ParseException ex) { }
Do not do this, you are just hiding any problems that may occur. Put some proper code in the catch block to help you find errors.
-
Can you not see that you have two
Date
objects (in separate scopes)?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Can you not see that you have two
Date
objects (in separate scopes)?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles