1

i am studying computer science and my homework was finding the next day.

The method getNextDay() returns null and doesn't go through my If statements.

Here are the two classes that are used:

public class NextDay {

           public static void main(String args[]) throws Exception {
              Date dateObj = new Date(30, 3, 2016);
              System.out.println("Altes Datum: " + dateObj.gibTag() + "." + dateObj.gibMonat() + "." + dateObj.gibJahr() + ".");
              System.out.println("Der naechste Tag ist der " + dateObj.getNextDay().toString() + ".");
           }
}



public class Date {
    int tag;
    int monat;
    int jahr;

    public Date(int tag, int monat, int jahr){
        this.tag = tag;
        this.monat = monat;
        this.jahr = jahr;
    }

    public Date getNextDay() throws Exception{  

        if(monat == 2){
        // wenn Februar 29 Tage hat.
        if(jahr % 4 == 0 | jahr % 400 == 0){
            if(tag > 0 & tag < 29){
                return new Date(tag + 1, monat, jahr);  
            }
            else if(tag == 29 & monat == 2){
                return new Date(tag - tag + 1, monat + 1, jahr);
            }else{
                throw new Exception("Im Februar können Sie nur bis 28 schreiben!");
            }   
        }
        // wenn Februar 28 Tage hat.
        if(jahr % 4 != 0 | jahr % 100 != 0 & monat == 2){
            if(tag > 0 & tag < 28){
                return new Date(tag + 1, monat, jahr);  
            }else if(tag == 28 & monat == 2){
                return new Date(tag - tag + 1, monat + 1, jahr);
            }           
            else{
                throw new Exception("Im Februar können Sie nur bis 28 schreiben!");
            }   
        }
        }


        if(monat != 2 & monat == 1 & monat == 3 & monat == 5 & monat == 7 & monat == 8 & monat == 10 & monat == 12 ){
        if(tag <= 31 & monat <= 12 & jahr > 0){
            if(tag < 31){
                return new Date(tag + 1, monat, jahr);  
            }
            else if(tag == 31 & monat < 12){
                return new Date(tag - tag + 1, monat + 1, jahr);
            }
            else if(monat == 12){
                if(tag == 31 & monat == 12){
                    return new Date(tag - tag + 1, monat - monat + 1, jahr + 1);
                }else{
                    return new Date(tag, monat, jahr + 1);
                }
            }

        }
            else{
                throw new Exception("Sie haben ein ungültiges Datum eingegeben!");
            }

        }


        if(monat != 2 & monat == 4 & monat == 6 & monat == 9 & monat == 11){
            if(tag <= 30 & monat <= 12 & jahr > 0){
                if(tag < 30){
                    return new Date(tag + 1, monat, jahr);  
                }
                else if(tag == 30 & monat < 12){
                    return new Date(tag - tag + 1, monat + 1, jahr);
                }
                else if(monat == 12){
                    if(tag == 31 & monat == 12){
                        return new Date(tag - tag + 1, monat - monat + 1, jahr + 1);
                    }else{
                        return new Date(tag, monat, jahr + 1);
                    }
                }

            }
                else{
                    throw new Exception("Sie haben ein ungültiges Datum eingegeben!");
                }

            }
        return null;


    }



    public int gibTag(){
        return tag;
    }

    public int gibMonat(){
        return monat;
    }

    public int gibJahr(){
        return jahr;
    }       


    public String toString(){
        return this.tag + "." + this.monat + "." + this.jahr;
    }
}
nick zoum
  • 7,216
  • 7
  • 36
  • 80
  • I would recommend translating your code to English before posting it. So it is easier for people to understand it and help you. – nick zoum Oct 08 '16 at 12:41
  • one simple reason could be , no condition block get executed so eventually you get null in return hence null pointer plus don't confuse your naming convention with API (Date is an API) – Pavneet_Singh Oct 08 '16 at 12:43
  • I think he not need to convert codes to English. Because only three tag used in code block. Tag = Day, Monat = Month, Jahre=Year and gibJahr etc. means of gib = get. There is no reason to translate. – Ivan Barayev Oct 08 '16 at 12:47
  • When would the month be 1 and 3 and 5 and 7 and 8 and 10 and 12 at the same time? – Peter Lawrey Oct 08 '16 at 13:13
  • @tkausl I believe that this question was just not asked properly and now that it has been edited. You should cast a vote to re - open it. Unfortunately I do not have the necessary 3000 reputation to cast a vote. – nick zoum Oct 08 '16 at 13:14
  • @PeterLawrey OP made a mistake and was using bit wise operators instead of conditional operators. Just check my answer below. – nick zoum Oct 08 '16 at 13:16
  • 1
    @nickzoum the problem was using AND instead of OR, and whether the OP used bit-wise or logical operators doesn't matter, reopened. – Peter Lawrey Oct 08 '16 at 13:20
  • 1
    @PeterLawrey The problem was using bit wise operators when || was what was needed. The bit wise operator "|" would have probably also worked, and that is a mistake but I think the main error is using the wrong kind of operators. Why confuse a person who is new to programming with things that he does not yet need to know. – nick zoum Oct 08 '16 at 13:25
  • @nickzoum yes, wrong operator but the first line of your answer is incorrect. – Peter Lawrey Oct 08 '16 at 13:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125235/discussion-between-nick-zoum-and-peter-lawrey). – nick zoum Oct 08 '16 at 13:28

1 Answers1

0

The problem is that you are using wrong bit-wise operator (&). Instead you should you use this conditional operator ||.

As a result in the getNextDay() method you none of the if statements are met and therefore the only line of code that is being read is return null;

And when you try to call a method on an object that is null then you get NullPointerException.

As a start i would recommend changing the getNextDay() method to:

public Date getNextDay() throws Exception {
    if (monat == 2) {
        // wenn Februar 29 Tage hat.
        if (jahr % 4 == 0 || jahr % 400 == 0) {
            if (tag > 0 & tag < 29) {
                return new Date(tag + 1, monat, jahr);
            } else if (tag == 29 & monat == 2) {
                return new Date(tag - tag + 1, monat + 1, jahr);
            } else {
                throw new Exception("Im Februar können Sie nur bis 28 schreiben!");
            }
        }
        // wenn Februar 28 Tage hat.
        if (jahr % 4 != 0 || jahr % 100 != 0 && monat == 2) {
            if (tag > 0 & tag < 28) {
                return new Date(tag + 1, monat, jahr);
            } else if (tag == 28 & monat == 2) {
                return new Date(tag - tag + 1, monat + 1, jahr);
            } else {
                throw new Exception("Im Februar können Sie nur bis 28 schreiben!");
            }
        }
    } else if (monat == 1 || monat == 3 || monat == 5 || monat == 7 || monat == 8 || monat == 10 || monat == 12) {
        if (tag <= 31 & monat <= 12 & jahr > 0) {
            if (tag < 31) {
                return new Date(tag + 1, monat, jahr);
            } else if (tag == 31 & monat < 12) {
                return new Date(tag - tag + 1, monat + 1, jahr);
            } else if (monat == 12) {
                if (tag == 31 & monat == 12) {
                    return new Date(tag - tag + 1, monat - monat + 1, jahr + 1);
                } else {
                    return new Date(tag, monat, jahr + 1);
                }
            }

        } else {
            throw new Exception("Sie haben ein ungültiges Datum eingegeben!");
        }

    } else if (monat == 4 || monat == 6 || monat == 9 || monat == 11) {
        if (tag <= 30 & monat <= 12 & jahr > 0) {
            if (tag < 30) {
                return new Date(tag + 1, monat, jahr);
            } else if (tag == 30 & monat < 12) {
                return new Date(tag - tag + 1, monat + 1, jahr);
            } else if (monat == 12) {
                if (tag == 31 & monat == 12) {
                    return new Date(tag - tag + 1, monat - monat + 1, jahr + 1);
                } else {
                    return new Date(tag, monat, jahr + 1);
                }
            }

        } else {
            throw new Exception("Sie haben ein ungültiges Datum eingegeben!");
        }

    }
    return null;
}

See how the if statements are now different. Try it for yourself.

nick zoum
  • 7,216
  • 7
  • 36
  • 80
  • thank you very much :) i get it now i used all the time & but you did it with ||. –  Oct 08 '16 at 13:07
  • @meert I think you also have a look at this: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – nick zoum Oct 08 '16 at 13:08
  • @meert And I thank you too. Since you have now joined the stack overflow community, I would recommend going through this page http://stackoverflow.com/tour (You will also get a badge) – nick zoum Oct 08 '16 at 13:18
  • 1
    You should note that using logical operators vs bitwise made no difference in this case. – Peter Lawrey Oct 08 '16 at 13:21