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

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

问题描述

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

<块引用>

NoSuchMethod:方法 'XYZ' 被调用为 null

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

解决方案

为什么会出现这个错误?

示例

作为一个真实世界的比较,刚刚发生的是这个对话:

<块引用>

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

<块引用>

你在说什么,我们没有汽车.

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

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

技术说明

您正在尝试使用 null 的变量.要么你已经明确地将它设置为 null,或者你根本没有设置它,默认值是 null.

像任何变量一样,它可以传递给其他函数.出现错误的地方可能不是来源.您必须跟踪从实际 null 值到它最初来自哪里的线索,以找出问题所在以及可能的解决方案.

null 可以有不同的含义:未设置为其他值的变量将是 null,但有时程序员有意使用 null 值来表示没有值.数据库有可为空的字段,JSON 有缺失值.丢失的信息可能确实是信息本身.例如,变量 bool userWantsPizzaForDinner; 可能用于 true 当用户说是时,false 当用户拒绝时它可能仍然是 null 当用户还没有选择某些东西时.这不是错误,它是故意使用的,需要相应处理.

我该如何解决?

找到它

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

修复它

一旦你知道它是哪个变量,找出它是如何结束的null.它从哪里来的?该值是否从未设置过?该值是另一个变量吗?那个 变量是如何获得它的值的.这就像一行面包屑,您可以按照直到到达某个变量从未设置过的点,或者可能到达某个变量被有意设置为 null 的点.如果是无意的,只需修复它.将其设置为您希望它具有的值.如果是故意的,那么您需要在程序中进一步处理它.也许您需要另一个 if 来为这种情况做一些特别的事情.如果有疑问,您可以询问有意将其设置为 null 的人他们想要实现的目标.

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?

解决方案

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?

What are you talking about, we don't have a car.

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.

Technical explanation

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.

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

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 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天全站免登陆