title: Flutter: What does the NoSuchMethodError: "The getter 'month' was called on null" mean? (DateTime package)
body: <p>I've started with flutter a couple of days ago and I am facing some issues here with the <code>DateTime</code> package. I am trying to implement a method which has to check if the actual day is monday or thursday etc. and then it should return a value which is getting added to a <code>_actualDatetimeUnformatted.add</code> function. Could it be a problem with the <code>add</code> function of DateTime, so it cannot receive a method's return value as <code>days</code>?</p>
  
  <p>Here is the full code. The problem is in the <code>static var _weekEndDatetimeUnformatted = _actualDatetimeUnformatted.add(new Duration(days: getWeekday(_actualDatetimeUnformatted)));</code> variable. When I change the the value to lets say 50 instead it works.</p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  import 'package:intl/intl.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
      return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: new ThemeData(
                brightness: Brightness.dark,
                primaryColorBrightness: Brightness.dark,
        ),
        home: MyHomePage(),
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    @override
    _MyHomePageState createState() =&gt; _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
  
    // GETTING ACTUAL DATE
    static var _actualDatetimeUnformatted = new DateTime.now();
  
    // DATETIME OBJECT TOO, LIKE _actualDatetimeUnformatted BUT WITH A FEW DAYS ADDED TO IT (TO GET THE END OF THE WEEK!)
    static var _weekEndDatetimeUnformatted = _actualDatetimeUnformatted.add(
        new Duration(days: getWeekday(_actualDatetimeUnformatted)));  // RED SCREEN MISTAKE SOMEWHERE HERE IN "getWeekday" method!
  
    // GET MONTH, DAY, YEAR (AS END OF WEEK)
    static getWeekday(_actualDatetimeUnformatted) {
      if (_actualDatetimeUnformatted.weekday == DateTime.monday) {
        return 6;
      } else if (_actualDatetimeUnformatted.weekday == DateTime.tuesday) {
        return 5;
      } else if (_actualDatetimeUnformatted.weekday == DateTime.wednesday) {
        return 4;
      } else if (_actualDatetimeUnformatted.weekday == DateTime.thursday) {
        return 3;
      } else if (_actualDatetimeUnformatted.weekday == DateTime.friday) {
        return 2;
      } else if (_actualDatetimeUnformatted.weekday == DateTime.saturday) {
        return 1;
      } else if (_actualDatetimeUnformatted.weekday == DateTime.sunday) {
        return 0;
      }
    }
  
    // GET MONTH, DAY (AS START OF WEEK)
    static var formatterWeekdayAll = new DateFormat("MMM d");
  
    // GET WEEKDAY
    static var formatterDay = new DateFormat("EEEE");
  
    // GETTING WEEKDAY FOR SELECTED DAY (above the weekdays)
    String _selectedDay = formatterDay.format(_actualDatetimeUnformatted);
  
    // RETURNING WEEKDAY FOR APP TEXT AS WELL AS END OF WEEK CALCULATED FROM THE DAY OF _WEEKDAY (above the current day (_selectedDay))
    String _Weekday = formatterWeekdayAll.format(_actualDatetimeUnformatted); //WORKS
    String _WeekdayEnd = formatterWeekdayAll.format(_weekEndDatetimeUnformatted);
  
    String text = "Some Note...";
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          backgroundColor: Colors.black,
          body: ListView(
              children: &lt;Widget&gt;[
                Stack(
                  children: &lt;Widget&gt;[
                    Container(
                      height: 380.0,
                    ),
                    ShaderMask(
                        shaderCallback: (rect) {
                          return LinearGradient(
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter,
                              colors: [Colors.black, Colors.transparent])
                              .createShader(
                              Rect.fromLTRB(0, 0, rect.width, rect.height));
                        },
                        blendMode: BlendMode.dstIn,
                        child: Image.asset('assets/milchstraße.jpg',
                            height: 300.0, fit: BoxFit.cover)),
                    RotatedBox(
                      quarterTurns: 0,
                      child: Text('Weekly Planner',
                          style: TextStyle(
                              fontSize: 55,
                              fontWeight: FontWeight.bold,
                              color: Colors.white.withOpacity(0.35),
                              letterSpacing: 10.0)),
                    ),
                    Positioned(
                      top: 4.0,
                      right: 4.0,
                      child: Container(
                        height: 40.0,
                        width: 40.0,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20.0),
                            color: Colors.white),
                        child: Center(
                          child: Icon(Icons.menu),
                        ),
                      ),
                    ),
                    Positioned(
                      top: 2.0,
                      right: 5.0,
                      child: Container(
                        height: 12.0,
                        width: 12.0,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(6.0),
                            color: Color(0xFFFD3664)),
                      ),
                    ),
                    Positioned(
                        top: 170.0,
                        left: 10.0,
                        child: Column(
                          children: &lt;Widget&gt;[
                            Text("It's week:",
                                style: TextStyle(
                                    fontFamily: 'Oswald-Light',
                                    fontSize: 25.0,
                                    fontWeight: FontWeight.w500,
                                    color: Colors.white))
                          ],
                        )),
                    Positioned(
                        top: 280.0,
                        left: 120.0,
                        child: Column(
                          children: &lt;Widget&gt;[
                            Text("$_selectedDay",
                                style: TextStyle(
                                    fontFamily: 'Oswald-Light',
                                    fontSize: 40.0,
                                    fontWeight: FontWeight.w500,
                                    color: Colors.white))
                          ],
                        )),
                    Positioned(
                        top: 210.0,
                        left: 10.0,
                        child: Column(
                          children: &lt;Widget&gt;[
                            Row(
                              children: &lt;Widget&gt;[
                                Text('From $_Weekday. to $_WeekdayEnd.',
                                    style: TextStyle(
                                        fontFamily: 'Oswald-Light',
                                        fontSize: 35.0,
                                        fontWeight: FontWeight.bold,
                                        color: Color(0xFFFD3664))),
                              ],
                            )
                          ],
                        )),
                  ],
                ),
                Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
                  _buildWeekDays('Monday', Icons.calendar_today, text),
                  _buildWeekDays('Tuesday', Icons.calendar_today, text),
                  _buildWeekDays('Wednesday', Icons.calendar_today, text),
                  _buildWeekDays('Thursday', Icons.calendar_today, text),
                ]),
                Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
                  _buildWeekDays('Friday', Icons.calendar_today, text),
                  _buildWeekDays('Saturday', Icons.calendar_today, text),
                  _buildWeekDays('Sunday', Icons.calendar_today, text),
                ]),
              ]));
    }
  
    Widget _buildWeekDays(String day, iconData, text) {
      return InkWell(
        splashColor: Colors.transparent,
        onTap: () {
          _WeekPlanRoute(day, text); // MISTAKE SOMEWHERE IN THIS METHOD!!!
        },
        child: AnimatedContainer(
            curve: Curves.easeIn,
            duration: Duration(microseconds: 300),
            height: _selectedDay == 'day' ? 100.0 : 100.0,
            width: _selectedDay == 'day' ? 100.0 : 75.0,
            color: _selectedDay == 'day' ? Color(0xFFFD3566) : Colors.transparent,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  iconData,
                  color: _selectedDay == day ? Colors.white : Colors.grey,
                  size: 40.0,
                ),
                SizedBox(height: 10.0),
                Text(day,
                    style: TextStyle(
                        fontFamily: 'Oswald-Light',
                        color: _selectedDay == day ? Colors.white : Colors.grey,
                        fontSize: 15.0))
              ],
            )),
      );
    }
  
    void _WeekPlanRoute(day, text) {
      selectMenuOption(day);
      Navigator.of(context).push(
        MaterialPageRoute&lt;void&gt;(
            builder: (BuildContext context) {
              return Scaffold(
                appBar: AppBar(
                  title: Text(
                    '$day',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontFamily: 'Oswald-Light',
                      color: Color(0xFFFD3566),
                      fontWeight: FontWeight.bold,
                      fontSize: 35,
                    ),
                  ),
                ),
                body: new Container(
                  margin: const EdgeInsets.symmetric(
                      horizontal: 10.0, vertical: 5.0),
                  padding: const EdgeInsets.symmetric(
                      horizontal: 15.0, vertical: 10.0),
                  decoration: new BoxDecoration(
                    color: Colors.green.shade200.withOpacity(0.3),
                    borderRadius: new BorderRadius.circular(5.0),
                  ),
                  child: new IntrinsicHeight(
                    child: new Row(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: &lt;Widget&gt;[
                        new Container(
                          margin: const EdgeInsets.only(
                              top: 4.0, bottom: 4.0, right: 10.0),
                          child: Text(
                            text, // DYNAMICALLY FOR USERS!
                            textAlign: TextAlign.justify,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 3,
                            style: TextStyle(
                              fontFamily: 'Oswald-Light',
                              fontSize: 20,
                              fontWeight: FontWeight.w200,
                              color: Colors.white,
                            ),
                          ),
                        ),
                        new Container(
                          margin: EdgeInsets.symmetric(horizontal: 5.0),
                          child: InkWell(
                            child: Icon(
                              Icons.star_border,
                              size: 40,
                            ),
                            onTap: () {
                              setToFav(text);
                            },
                            onDoubleTap: () {
                              // DELETE COLOR FROM THE STAR
                              // DELETE IT FROM ARRAY OF IMPORTANT NOTES!
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              );
            }
        ),
      );
    }
  
    selectMenuOption(String day) {
      setState(() {
        _selectedDay = day;
      });
    }
  
    setToFav(String text) {
      setState(() {
        //final bool favourized = _saved.contains(pair)
      });
  
    }
  }
  </code></pre>
  
  <p>I've read myself through the error message but it's not really helpful for me xD. I'm coming from the lands of Python which means I am familiar with short, helpful error messages :D.</p>
  
  <pre><code>I/flutter (28798): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
  I/flutter (28798): The following NoSuchMethodError was thrown building Builder:
  I/flutter (28798): The getter 'month' was called on null.
  I/flutter (28798): Receiver: null
  I/flutter (28798): Tried calling: month
  I/flutter (28798): User-created ancestor of the error-causing widget was:
  I/flutter (28798):   MaterialApp
  I/flutter (28798):   file:///C:/Users/tim-a/Desktop/AndroidStudioProjects/Flutter/wochenplaner/lib/main.dart:14:12
  I/flutter (28798): When the exception was thrown, this was the stack:
  I/flutter (28798): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
  I/flutter (28798): #1      _DateFormatPatternField.formatMonth (package:intl/src/intl/date_format_field.dart:445:41)
  I/flutter (28798): #2      _DateFormatPatternField.formatField (package:intl/src/intl/date_format_field.dart:367:16)
  I/flutter (28798): #3      _DateFormatPatternField.format (package:intl/src/intl/date_format_field.dart:244:12)
  I/flutter (28798): #4      DateFormat.format.&lt;anonymous closure&gt; (package:intl/src/intl/date_format.dart:255:57)
  I/flutter (28798): #5      List.forEach (dart:core-patch/growable_array.dart:278:8)
  I/flutter (28798): #6      DateFormat.format (package:intl/src/intl/date_format.dart:255:19)
  I/flutter (28798): #7      new _MyHomePageState (package:wochenplaner/main.dart:69:44)
  I/flutter (28798): #8      MyHomePage.createState (package:wochenplaner/main.dart:27:37)
  I/flutter (28798): #9      new StatefulElement (package:flutter/src/widgets/framework.dart:4016:25)
  I/flutter (28798): #10     StatefulWidget.createElement (package:flutter/src/widgets/framework.dart:802:38)
  I/flutter (28798): #11     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3091:40)
  I/flutter (28798): #12     Element.updateChild (package:flutter/src/widgets/framework.dart:2896:12)
  I/flutter (28798): #13     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5119:14)
  I/flutter (28798): #14     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3093:14)
  I/flutter (28798): #15     Element.updateChild (package:flutter/src/widgets/framework.dart:2896:12)
  I/flutter (28798): #16     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #17     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #18     StatelessElement.update (package:flutter/src/widgets/framework.dart:4008:5)
  I/flutter (28798): #19     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #20     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5126:14)
  I/flutter (28798): #21     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #22     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5126:14)
  I/flutter (28798): #23     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #24     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5126:14)
  I/flutter (28798): #25     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #26     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5126:14)
  I/flutter (28798): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #28     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #29     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #30     StatefulElement.update (package:flutter/src/widgets/framework.dart:4112:5)
  I/flutter (28798): #31     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #32     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #33     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #34     StatelessElement.update (package:flutter/src/widgets/framework.dart:4008:5)
  I/flutter (28798): #35     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #36     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #37     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #38     StatefulElement.update (package:flutter/src/widgets/framework.dart:4112:5)
  I/flutter (28798): #39     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #40     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5126:14)
  I/flutter (28798): #41     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #42     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #43     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #44     ProxyElement.update (package:flutter/src/widgets/framework.dart:4246:5)
  I/flutter (28798): #45     _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:94:11)
  I/flutter (28798): #46     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #47     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5126:14)
  I/flutter (28798): #48     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #49     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #50     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #51     StatefulElement.update (package:flutter/src/widgets/framework.dart:4112:5)
  I/flutter (28798): #52     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #53     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #54     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #55     StatelessElement.update (package:flutter/src/widgets/framework.dart:4008:5)
  I/flutter (28798): #56     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #57     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5126:14)
  I/flutter (28798): #58     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #59     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #60     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #61     ProxyElement.update (package:flutter/src/widgets/framework.dart:4246:5)
  I/flutter (28798): #62     Element.updateChild (package:flutter/src/widgets/framework.dart:2885:15)
  I/flutter (28798): #63     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3953:16)
  I/flutter (28798): #64     Element.rebuild (package:flutter/src/widgets/framework.dart:3730:5)
  I/flutter (28798): #65     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2340:33)
  I/flutter (28798): #66     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding&amp;ServicesBinding&amp;SchedulerBinding&amp;PaintingBinding&amp;SemanticsBinding&amp;RendererBinding&amp;WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:701:20)
  I/flutter (28798): #67     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding&amp;ServicesBinding&amp;SchedulerBinding&amp;PaintingBinding&amp;SemanticsBinding&amp;RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:285:5)
  I/flutter (28798): #68     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding&amp;ServicesBinding&amp;SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1033:15)
  I/flutter (28798): #69     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding&amp;ServicesBinding&amp;SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:975:9)
  I/flutter (28798): #70     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding&amp;ServicesBinding&amp;SchedulerBinding.scheduleWarmUpFrame.&lt;anonymous closure&gt; (package:flutter/src/scheduler/binding.dart:784:7)
  I/flutter (28798): #72     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
  I/flutter (28798): #73     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
  I/flutter (28798): #74     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
  I/flutter (28798): (elided one frame from package dart:async-patch)
  I/flutter (28798): ════════════════════════════════════════════════════════════════════════════════════════════════════
  </code></pre>
  
  <p>I don't understand why its saying me that <code>The getter 'month' was called on null</code> even though I don't even use month in <code>getWeekday</code>.</p>
  
  <p>Many thanks in advance!</p>