Tag: null

多个重载方法:null是否等于NullPointerException?

public class TestMain { public static void methodTest(Exception e) { System.out.println(“Exception method called”); } public static void methodTest(Object e) { System.out.println(“Object method called”); } public static void methodTest(NullPointerException e) { System.out.println(“NullPointerException method called”); } public static void main(String args[]) { methodTest(null); } } 输出:调用NullPointerException方法

程序无法正确绘制屏幕

我一直在构建一个简短的程序,它基本上在JPanel上绘制一个太空船,并听取指示程序射击子弹的键。 问题是它甚至没有在屏幕上绘制宇宙飞船或子弹。 我还怀疑KeyBindings可能无法工作,因为这是以前的问题(我可能已经或可能没有修复),但手头的主要问题仍然是我的屏幕没有被绘制。 这是我的代码: public enum Direction { LEFT, RIGHT, SPACE } import javax.swing.JFrame; public class Main { public static void main(String[] args) { JFrame frame; Ship s1; Shoot shoot; // Set the frame up frame = new JFrame(); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); // Get some more necessary objects s1 = new Ship(); shoot […]

如何在不抛出NullPointerException的情况下使用SplashScreen?

无论我尝试什么, SplashScreen.getSplashScreen() 始终为 null 。 从在线搜索我发现这是一个常见问题,并且它与不给SplashScreen使用图像有关…因此,在导航方法中,我认为应该使用setImageURL(URL) 。 这仍然无效。 在SO上也存在类似的问题,这些问题没有帮助,似乎建议使用无数的插件或从Frame开始创建这样的类。 即使是Oracle教程也很神秘,并没有概述正确使用SplashScreen每个逻辑步骤…… 如果使用SplashScreen是不可能或不必要的,那么有没有替代方法呢? 或者有没有人破解出这个问题的简单方法? 这是我的尝试: import java.awt.Color; import java.awt.Graphics2D; import java.awt.SplashScreen; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.URL; /** */ public final class MainGUI implements ActionListener { /** * @throws IOException * @throws IllegalStateException * @throws NullPointerException */ private final static void showSplashScreen() throws NullPointerException, IllegalStateException, […]

在Java中重载方法中使用null

可能重复: 方法重载NULL参数 以下代码编译并正常运行。 public class Main { public void temp(Object o) { System.out.println(“The method with the receiving parameter of type Object has been invoked.”); } public void temp(String s) { System.out.println(“The method with the receiving parameter of type String has been invoked.”); } public void temp(int i) { System.out.println(“The method with the receiving parameter of […]

空检查编码标准

关于空检查的编码标准我有疑问。 我想知道它们之间的区别 if(a!=null) 和 if(null!=a) 哪一个更好,哪一个使用,为什么?

在null-reference上创建方法引用不会引发exception

是否有可能在Java中的null引用上创建方法引用? 这样做可能永远不会正确,但可能导致以后很难找到的错误: public class Test { void m() { } public static void main(String[] args) { Test test = null; Runnable fn = test::m; // no exception System.out.println(fn); // prints Test$$Lambda$1/791452441@1c20c684 fn.run(); // throws a null pointer exception } }

为什么我可以在三元操作中将原始类型设置为null

我一直认为Java中的原始类型不能为null ,因为如果我尝试执行以下操作,则编译时错误: int test = null; 然而,在三元操作中,似乎允许: int test = something != 0 ? 5 : null; 不是三元操作(在这种情况下): int test; if (something != 0){ test = 5; } else { test = null } 当然不应该被允许。 如果该条件失败,由于自动装箱,它将自动抛出NullPointerException 。 那么为什么java编译器不会像这样获取废话呢?

null是Java关键字吗?

null是Java中的关键字吗?

什么是c#,java中的null?

就像……在C ++中它是0吗? 或者它是一些“特殊”的对象? 或者也许完全不同的东西? – 编辑 – 我知道它是什么 ,问题是 – 它是如何完成的

为什么将null转换为Object?

我在一些代码中找到了一个位置,我正在处理将null为Object因为它被传递给方法。 为什么要这样做? 我知道这个问题涉及重载方法,并使用强制转换来确定要调用的方法版本。 但是如果未执行强制转换,如果使用null参数调用该方法,是否不会选择使用类型为Object的参数的重载方法而不是该方法的任何其他匹配版本? 那么演员还能完成什么呢?