什么是NoSuchMethod错误,如何解决? [英] What is a NoSuchMethod error and how do I fix it?

查看:642
本文介绍了什么是NoSuchMethod错误,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,当我运行它时会产生一个错误,说:

I have some code and when I run it produces an error, saying:


NoSuchMethod:方法 XYZ在空值上被调用

NoSuchMethod: the method 'XYZ' was called on null

这是什么意思,我该如何解决?

What does that mean and how do I fix it?

推荐答案

< h1>为什么会出现此错误?

示例


作为现实世界的比较,这次对话刚发生了什么:

Why do I get this error?

Example

As a real world comparison, what just happened is this conversation:


嘿,汽车油箱中还剩多少汽油?

Hey, how much gas is left in the tank of the car?



您在说什么,我们 没有 汽车。

这就是程序中正在发生的事情。您想调用类似 _car.getGasLevel(); 的函数,但没有汽车,变量 _car null

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 is null.

很显然,在您的程序中,它可能不是汽车。可能是列表,字符串或其他任何东西。

Obviously, in your program it might not be a car. It could be a list or a string or anything else really.

您正在尝试使用<$ c $的变量c> null 。您已经将它明确设置为 null ,或者根本就没有设置,默认值为 null

You are trying to use a variable that is null. Either you have explicitly set it to null, or you just never set it at all, the default value is null.

就像任何变量一样,它可以传递给其他函数。您得到错误的地方可能不是来源。您必须遵循从实际 null 值到其原始来源的线索,以找出问题所在和解决方案。

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 可以具有不同的含义:未设置为另一个值的变量将为 null ,但是有时程序员有意使用null值表示没有值。数据库具有可空字段,JSON缺少值。丢失的信息确实确实可能是 信息本身。例如,当用户说是时,变量 bool userWantsPizzaForDinner; 可能用于 true 如果用户拒绝,则返回false ;如果用户尚未选择某些内容,则返回 null 。这不是一个错误,它是有意使用的,需要进行相应的处理。

null can have different meanings: variables not set to another value will be null, 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 variable bool userWantsPizzaForDinner; for example might be used for true when the user said yes, false when the user declined and it might still be null when the user has not yet picked something. That's not a mistake, it's intentionally used and needs to be handled accordingly.

使用错误消息附带的堆栈跟踪信息来准确找出错误所在的行。然后在该行上设置一个断点。当程序到达断点时,检查变量的所有值。其中之一是 null ,找出哪个。

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.

知道它是哪个变量,找出它最终是 null 的原因。它从哪里来的?该值是否从未设置过?值是另一个变量吗? 那个变量如何获得其价值。这就像一条面包屑,您可以遵循,直到发现某个变量从未设置过,或者您发现某个变量被故意设置为 null 。如果不是故意的,请修复它。将其设置为所需的值。如果是故意的,那么您需要在程序中进一步处理它。也许您需要另一个 if 来针对这种情况进行特殊处理。如有疑问,您可以询问故意将其设置为 null 的人。

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 to null. 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 another if to do something special for this case. If in doubt, you can ask the person that intentionally set it to null what they wanted to achieve.

这篇关于什么是NoSuchMethod错误,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆