I have some code and when I run it produces an error, saying:
NoSuchMethod: the method 'XYZ' was called on null
What does that mean and how do I fix it?
I have some code and when I run it produces an error, saying:
NoSuchMethod: the method 'XYZ' was called on null
What does that mean and how do I fix it?
Simply the variable/function you are trying to access from the class does not exist:
someClass.xyz();
The above will give the error
NoSuchMethod: the method 'xyz' was called on null
because the class someClass
does not exist.
The following will work fine:
// SomeClass created
// SomeClass has a function xyz
class SomeClass {
SomeClass();
void xyz() {
print('xyz');
}
}
void main() {
// Create an instance of the class
final someClass = SomeClass();
// Access the xyz function
someClass.xyz();
}
Why do I get this error?
Example
As a real world comparison, what just happened is this conversation:
That is exactly what is happening in your program. You wanted to call a function like
_car.getGasLevel();
but there is no car, the variable_car
isnull
.Obviously, in your program it might not be a car. It could be a list or a string or anything else really.
Technical explanation
You are trying to use a variable that is
null
. Either you have explicitly set it tonull
, or you just never set it at all, the default value isnull
.Like any variable, it can be passed into other functions. The place where you get the error might not be the source. You will have to follow the leads from the actual
null
value to where it originally came from, to find what the problem is and what the solution might be.null
can have different meanings: variables not set to another value will benull
, but sometimes null values are used by programmers intentionally to signal that there is no value. Databases have nullable fields, JSON has missing values. Missing information may indeed be the information itself. The variablebool userWantsPizzaForDinner;
for example might be used fortrue
when the user said yes,false
when the user declined and it might still benull
when the user has not yet picked something. That's not a mistake, it's intentionally used and needs to be handled accordingly.How do I fix it?
Find it
Use the stack trace that came with the error message to find out exactly which line the error was on. Then set a breakpoint on that line. When the program hits the breakpoint, inspect all the values of the variables. One of them is
null
, find out which one.Fix it
Once you know which variable it is, find out how it ended up being
null
. Where did it come from? Was the value never set in the first place? Was the value another variable? How did that variable got it's value. It's like a line of breadcrumbs you can follow until you arrive at a point where you find that some variable was never set, or maybe you arrive at a point where you find that a variable was intentionally set tonull
. If it was unintentional, just fix it. Set it to the value you want it to have. If it was intentional, then you need to handle it further down in the program. Maybe you need anotherif
to do something special for this case. If in doubt, you can ask the person that intentionally set it tonull
what they wanted to achieve.