简体   繁体   English

有人可以解释这个错误是什么意思吗?

[英]Can someone explain what this error means please?

java.lang.NullPointerException
    at java.lang.Integer.compareTo(Unknown Source)
    at java.lang.Integer.compareTo(Unknown Source)

I slightly edited my code, deleted no methods but changed the name of one or two, and now...boom! 我稍微修改了代码,没有删除任何方法,但更改了一个或两个的名称,现在……轰! Nothing works ! 没用! So annoying because I just had it working, went back and commented it and now I can't see whats changed...help ? 太烦人了,因为我刚刚让它工作了,回去评论了它,现在我看不到有什么变化...帮助? :) :)

You're probably trying to do something like this: 您可能正在尝试执行以下操作:

Integer i = null;
Integer j = 42;
i.compareTo(j); // throws NullPointerException since i is null

or this: 或这个:

Integer i = 21;
Integer j = null;
i.compareTo(j); // throws NullPointerException since j is null

but you haven't shown any code. 但您没有显示任何代码。

From the docs : 文档

Thrown when an application attempts to use null in a case where an object is required. 当应用程序在需要对象的情况下尝试使用null时抛出。 These include: 这些包括:

  • Calling the instance method of a null object. 调用空对象的实例方法。
  • Accessing or modifying the field of a null object. 访问或修改空对象的字段。
  • Taking the length of null as if it were an array. 将null的长度视为数组。
  • Accessing or modifying the slots of null as if it were an array. 访问或修改null插槽,就好像它是一个数组一样。
  • Throwing null as if it were a Throwable value. 将null抛出,就好像它是一个Throwable值一样。

Applications should throw instances of this class to indicate other illegal uses of the null object. 应用程序应抛出此类的实例,以指示对null对象的其他非法使用。

You will surely have encountered one of these cases. 您肯定会遇到这些情况之一。 On a more higher level, you might have called a function (here compareTo()), with a null argument, which then leads to a NullPointerException in the function. 在更高的层次上,您可能已经调用了带有null参数的函数(此处为compareTo()),该参数随后导致函数中的NullPointerException。

Maybe you have a TreeMap, and inserted a null into the map? 也许您有一个TreeMap,并在地图中插入了null?

Are you sure that when you changed the name of your method that you changed it everywhere you called it? 您确定在更改方法名称时会在调用它的所有位置都对其进行更改吗? Chances are that with a null pointer exception, you have an improperly typed or spelled name. 出现空指针异常的可能是您输入或拼写的名称不正确。 It looks like you were trying to compare two integers, and chances are that the method that gives out one of those integers isn't being called correctly anymore. 似乎您正在尝试比较两个整数,并且可能无法再正确调用给出这些整数之一的方法。 Always make sure to double check. 始终确保仔细检查。

Put a break point in you ide at that line and see what var is null. 在您的那条线的终点放置一个断点,看看var是否为null。 From there it will be a easy fix. 从那里将很容易解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM