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
Debug your code and find out why date is null. I can't help you any more than that. If this was C# like this forum is for, I might have been able to help more but this is not C# code.
Speed of sound - 1100 ft/sec Speed of light - 186,000 mi/sec Speed of stupid - instantaneous.
-
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
Pavlex4 wrote:
if (spinner2.getSelectedItemPosition() == 0) value = 1; if (spinner2.getSelectedItemPosition() == 1) value = 2; if (spinner2.getSelectedItemPosition() == 2) value = 3; if (spinner2.getSelectedItemPosition() == 3) value = 4; if (spinner2.getSelectedItemPosition() == 4) value = 5; if (spinner2.getSelectedItemPosition() == 5) value = 6; if (spinner2.getSelectedItemPosition() == 6) value = 7; if (spinner2.getSelectedItemPosition() == 7) value = 8; if (spinner2.getSelectedItemPosition() == 8) value = 9; if (spinner2.getSelectedItemPosition() == 9) value = 10; if (spinner2.getSelectedItemPosition() == 10) value = 11; if (spinner2.getSelectedItemPosition() == 11) value = 12;
value = spinner2.getSelectedItemPosition() + 1;
Pavlex4 wrote:
if (h == 0) { dayOfWeek = "Subota"; } else
-
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
Ignoring for a moment that this is the C# forum, not the Android one.
if (spinner2.getSelectedItemPosition() == 0) value = 1; if (spinner2.getSelectedItemPosition() == 1) value = 2;
Can you see any logic in all these
if
statements? Particularly that they could be replaced by the simplevalue = spinner2.getSelectedItemPosition() + 1;
As to your actual problem, it is quite simple. You create a new
Date
variable inside yourtry
block, but then try to use the one declared at class level to extract the various fields. -
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