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. Mobile Development
  3. Android
  4. Android Day of Week Calculator

Android Day of Week Calculator

Scheduled Pinned Locked Moved Android
androidquestion
14 Posts 3 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
    
    D 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
      
      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Seriously? A NullPointerException is being thrown on line 230 because date is null. 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

      P 1 Reply Last reply
      0
      • D David Crow

        Seriously? A NullPointerException is being thrown on line 230 because date is null. 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

        P Offline
        P Offline
        Pavlex4
        wrote on last edited by
        #3

        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());

        D 1 Reply Last reply
        0
        • P Pavlex4

          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());

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          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

          P 1 Reply Last reply
          0
          • D David Crow

            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

            P Offline
            P Offline
            Pavlex4
            wrote on last edited by
            #5

            So,what should I do?

            D 1 Reply Last reply
            0
            • P Pavlex4

              So,what should I do?

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              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

              P 1 Reply Last reply
              0
              • D David Crow

                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

                P Offline
                P Offline
                Pavlex4
                wrote on last edited by
                #7

                You mean this "private Date date;" ?

                D 1 Reply Last reply
                0
                • P Pavlex4

                  You mean this "private Date date;" ?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  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

                  P 1 Reply Last reply
                  0
                  • D David Crow

                    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

                    P Offline
                    P Offline
                    Pavlex4
                    wrote on last edited by
                    #9

                    What is problem with that line?

                    L D 2 Replies Last reply
                    0
                    • P Pavlex4

                      What is problem with that line?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Nothing.

                      P 1 Reply Last reply
                      0
                      • L Lost User

                        Nothing.

                        P Offline
                        P Offline
                        Pavlex4
                        wrote on last edited by
                        #11

                        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());
                        
                        L 1 Reply Last reply
                        0
                        • P Pavlex4

                          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());
                          
                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • P Pavlex4

                            What is problem with that line?

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #13

                            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

                            L 1 Reply Last reply
                            0
                            • D David Crow

                              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

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Why check your own code when you can get it done for free at CodeProject? ;P

                              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