Tag: null

我只得到null:Object selectedValue = jPane.getValue(); (jPane =新的JOptionPane(…)

private void cancelButtonPressed() { jPane = new JOptionPane(“quit?”, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, buttons, buttons[1]); jPane.setName(“WPane”); dialog = jPane.createDialog(jPane.getParent(), “Confirm Dialog”); dialog.setVisible(true); dialog.dispose(); Object selectedValue = jPane.getValue(); System.out.println(“selectedValue:” + selectedValue); if(selectedValue == null) System.out.println(“selectedValue:” + selectedValue); //return CLOSED_OPTION; if(buttons == null) { if(selectedValue instanceof Integer) { //return ((Integer)selectedValue).intValue(); System.out.println(“selectedValue instanceof Integer, selectedValue:” + selectedValue); //return CLOSED_OPTION; } […]

Java字符串返回null

我试图让一个类从另一个类返回一个字符串,虽然我得到的返回值为null。 我有一个set方法,可以在原始类中设置字符串,但是当我在第二个类中调用该方法时,我得到一个返回null。 这是第一堂课; public class IceCream { // instance variables – replace the example below with your own private String flavour; public static double price; /** * Constructor for objects of class IceCream */ public IceCream() { // initialise instance variables String flavour = getFlavour(); price = 0.50; } /** * Gets price in pence. […]

Class#getClassLoader何时返回null?

说我有一些Java代码: public class Widget { …whatever } 还有一些代码可以加载Widget : ClassLoader widgetLoader = Widget.class.getClassLoader(); widgetLoader可以为null吗? 为什么/为什么不呢? 如果是这样,在什么情况下呢?

可以在超级构造函数完成之前初始化java子类的私有final字段吗?

我有一对看起来像这样的课程; public abstract class Class1 { //… public Class1() { //… function2(); //… } protected abstract void function2(); } public class Class2 implements Class1 { private final OnSomethingListener mOnSomethingListener = new OnSomethingListener() { @Override onSomething() { doThatOtherThing(); } } protected void function2() { //uses mOnSomethingListener //however mOnSomethingListener is null when this function is called from […]

为什么我将null作为数组的值?

我有一个Hra1类,它定义了游戏规则(游戏= hra)。 问题是,我得到一个空值,例如poleMinci == null,尽管在构造函数中创建了数组poleMinci。 换句话说,玩家的移动方法总是返回false。 构造函数: public Hra1() { Mince [] poleMinci = new Mince[20]; poleMinci[0] = new Mince(“stříbrná”, “coin.png”); poleMinci[3] = new Mince(“stříbrná”, “coin.png”); poleMinci[4] = new Mince(“zlatá”, “coin_gold.png”); poleMinci[8] = new Mince(“stříbrná”, “coin.png”); poleMinci[10] = new Mince(“stříbrná”, “coin.png”); poleMinci[12] = new Mince(“stříbrná”, “coin.png”); } 玩家的移动方法: public boolean tahHrace(Tah tah){ if (poleMinci != […]

为什么在字符串输出中出现空值?

当我执行以下代码时,输​​出为“nullHelloWorld”。 Java如何处理null? import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be “Main” only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { String str=null; str+=”Hello World”; System.out.println(str); } }

Caught Throwable或Exception为null

这里有两个类似的问题,从来没有任何答案。 或者答案是:“这是不可能的!” 对不起,可能太多了: try{ … // the line that causes the error LinearLayout cell = (LinearLayout) inflater.inflate(R.layout.channels_list_cell, column); … } catch(Throwable e){ Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); < breakpoint here! } 在断点处,e为空。 我该如何寻找错误? 很可能它不是java或Android的问题,而是Eclipse调试器,它本身需要调试很糟糕。 但是我要做什么,除了换到另一个IDE? 有任何想法吗? 事先感激不尽。 我尝试过Throwable,Exception,RuntimeException。 结果是一样的。 尝试跳过断点会导致NullPointerException,因此,当时e似乎已经为空。 哪里可以丢失? 编辑:我向所有人表达我的感激之情,并向每个回答者致以+1。 这是一个Eclipse bug。 重新启动Eclipse后,exception不再为null,这是正常的RuntimeException:二进制XML文件行#15:您必须提供layout_width属性。 这将是另一个需要解决的问题,但是这个问题已经解决了。

我们可以在null对象上调用任何方法吗?

可能吗? Object obj=null; obj.someMethod(); someMethod{/*some code here*/}

传递null时选择哪个构造函数?

在下面的示例中,我有两个构造函数:一个接受String,另一个接受自定义对象。 在此自定义对象上,存在一个返回String的方法“getId()”。 public class ConstructorTest { private String property; public ConstructorTest(AnObject property) { this.property = property.getId(); } public ConstructorTest(String property) { this.property = property; } public String getQueryString() { return “IN_FOLDER(‘” + property + “‘)”; } } 如果我将null传递给构造函数,选择哪个构造函数,为什么? 在我的测试中,选择了String构造函数,但我不知道是否总是这样,为什么。 我希望有人可以为我提供一些见解。 提前致谢。

为什么null不是编译时常量?

因此,如果我有一个static final Object CONSTANT = null ,由于某种原因,如果我在另一段代码中引用它,如doSomething(CONSTANT) ,它将不会在编译期间嵌入到代码中。 所以编译后不是doSomething(null) ,而是doSomething(CONSTANT) 。