Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Android Day of Week Calculator

Android Day of Week Calculator

Scheduled Pinned Locked Moved C#
androidquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pavlex4
    wrote on last edited by
    #1

    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
    
    L 4 Replies Last reply
    0
    • P Pavlex4

      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P Pavlex4

        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
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • P Pavlex4

          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
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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 simple

          value = spinner2.getSelectedItemPosition() + 1;

          As to your actual problem, it is quite simple. You create a new Date variable inside your try block, but then try to use the one declared at class level to extract the various fields.

          1 Reply Last reply
          0
          • P Pavlex4

            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
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            C#:

                 DayOfWeek dow = Convert.ToDateTime( "January 7 2000" ).DayOfWeek;
            

            Time to find a new hobby.

            "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups