- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
今天我在写测试的时候遇到了一个奇怪的情况。基本上,我上了一堂有数据的课。以 Toy 为例,我们可以从中检索名称:
public class Toy {
private String name;
public Toy(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
我有一个异常(exception),它的工作方式与此类似(例如,只显示我们在它变坏之前处理的所有对象的数据);我还包括一个主要用于测试目的:
public class ToyFactoryException extends Exception {
public ToyFactoryException(Toy firstToy, Toy secondToy) {
super("An error occurred when manufacturing: " +
"\nfirstToy: " + firstToy != null ? firstToy.getName() : null +
"\nsecondToy: " + secondToy != null ? secondToy.getName() : null);
}
public static void main(String[] args) {
try {
throw new ToyFactoryException(null, new Toy("hi"));
} catch (ToyFactoryException myException) {
System.out.println("It should be there.");
} catch (Exception exception) {
System.out.println("But it's there instead.");
}
}
}
正如我在第一个 catch block 中所写,异常应该在 ToyFactoryException 中被捕获。
但是,在异常情况下,它会尝试在此处读取 firstToy.getName():firstToy != null ? firstToy.getName() : 空
firstToy != null
应该评估为 false,这意味着它不应该首先尝试调用 firstToy.getName()
。当你以相反的顺序写它时:
public ToyFactoryException(Toy firstToy, Toy secondToy) {
super("An error occurred when manufacturing: " +
"\nfirstToy: " + firstToy != null ? null : firstToy.getName() +
"\nsecondToy: " + secondToy != null ? secondToy.getName() : null);
}
您意识到它现在读取的是 null
,这意味着它真正将 firstToy != null
读取为 true。
如果您改用这种方式编写 main(null 是构造函数的第二个参数):
public static void main(String[] args) {
try {
throw new ToyFactoryException(new Toy("hi"), null);
} catch (ToyFactoryException myException) {
System.out.println("It should be there.");
} catch (Exception exception) {
System.out.println("But it's there instead.");
}
}
它工作正常,尽管 secondToy 三元条件的编写方式与 firstToy 三元相同。
为什么 firstToy 上的三元条件没有正确评估 null?
最佳答案
您应该在条件表达式两边加上括号。
这个:
"string " + firstToy != null ? firstToy.getName() : null
意思是:
("string " + firstToy) != null ? firstToy.getName() : null
你需要这个:
"string " + (firstToy != null ? firstToy.getName() : null)
关于Java 三元运算符严重评估 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386008/
目前我正在调试我们的应用程序一个看似随机的错误。我将尝试提供尽可能多的细节。基本上过了一会儿,我们服务器上的应用程序的一个应用程序池停止了。我必须启动它,或者有时,重新启动 Coldfusion 或更
在寻找其他东西时,完全出于巧合,我偶然发现了一些关于案例类继承是多么邪恶的评论。有一个叫做 ProductN 的东西,可怜虫和国王, Sprite 和巫师,以及案例类继承如何丢失某种非常理想的属性。那
我正在从服务器下载媒体内容,并且应用程序在一定时间后崩溃,并给出以下错误: 收到内存压力通知(严重) 当我在 didReceiveMemoryWarning 函数中收到警告时删除了所有对象,但没有运气
为什么我会遇到这个错误 我开发了 Jax-Rs Web 服务 Jersey、Maven、Hibernate 和 Spring。如何解决该错误? Aug 03, 2015 2:23:17 PM com.
我是 Perl 新手。我收到此错误: CRITICAL : "file does not exist" 当我尝试运行脚本来通过 Nagios 监视 httpd 错误日志时。 #!/bin/sh # C
我最近一直在努力在 Kivy 中创建一个填充的 Circle,当窗口的大小调整为不同的宽度或高度时,它仍然是一个圆。我在这里看了问题: Centering an object in Kivy 但是当我
我想用实验编程语言实现的主要目的之一是:发生错误(语法,名称,类型等)时,无论程序有多严重或破坏性如何,都可以保持程序运行。 我知道这可能非常糟糕,但是我只想要一些不会在每个错误中都消失的东西-,我发
我使用 NetBeans 来制作我的 Java 项目。在我的数据库表或 DTO 中,它们都有一个名为“paymentDate ”的列。我不明白为什么它会发送此错误。奇怪的是,当我将“PAYMENTDA
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我正在使用Grails 1.3.4,后者使用了Hibernate,并且针对Oracle 11g数据库。我的应用程序启动失败,出现以下错误: 2010-11-04 09:45:34,671 INFO
自从我执行了 pip install google-api-python-client 后,我的 Gunicorn worker 在超时后停止了。 Django==1.5.3 unicorn ==
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Connecting to SQLite Database Fails 我是 Java 初学者 当我想从 SQLit
在我的实体类中,我有以下内容,调用一个返回 sys_refcursor 的 oracle 函数 @Entity @javax.persistence.NamedNativeQuery(
我们在生产环境中遇到了这个问题,但无法追踪。技术:JSP/Servlets,Jetty 服务器版本:jetty-distribution-8.1.15, 以下是异常(exception)情况,我们在
我有一个基于网络的应用程序,每三秒我连接到一个数据库并从 GPS 设备查询数据。 不幸的是,当应用程序工作超过几个小时时我无缘无故地得到了异常:“严重:没有找到适合jdbc的驱动程序......”。应
之前我的 tomcat 服务器运行良好。 但突然出现以下错误。 请帮忙解决这个问题 SEVERE: A child container failed during start java.util.co
请不要标记为重复,因为其他解决方案还没有帮助! 我在尝试运行本教程时在浏览器中收到 404: http://www.vogella.com/articles/REST/article.html 实际上
使用 Spring Tool Suite 3.6.3,我将以下内容添加到 Maven Web 项目的 POM.XML 中。 com.MyCompany.sei MyCompany-a
我在 tomcat 上部署了 RESTful Web 服务'该 Web 服务在同一域场景的客户端/服务器中工作正常'但我需要使 Web 服务可从跨域使用'为此我使用了 CORS 过滤器. 我在 myP
我第一次尝试使用 ServletContextListener 在每次部署应用程序时执行特定函数。为此,我采用了一个简单的 java 类文件并在其上实现了 ServletContextListener
我是一名优秀的程序员,十分优秀!