Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java登录引发NullPointerException_Java_Eclipse_Nullpointerexception - Fatal编程技术网

java登录引发NullPointerException

java登录引发NullPointerException,java,eclipse,nullpointerexception,Java,Eclipse,Nullpointerexception,我正在试验Java登录方法。有人能给我解释一下为什么我会得到这个空点异常吗 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Remote implements ActionListener { Action action; Gui gui; String output; Boolean result; public Remot

我正在试验Java登录方法。有人能给我解释一下为什么我会得到这个空点异常吗

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Remote implements ActionListener {
    Action  action;
    Gui     gui;
    String  output;
    Boolean result;

public Remote(Gui g, Action a) {
    action = a;
    gui = g;
    actionListenerMeth(this);
}

@Override
public void actionPerformed(ActionEvent arg0) {
    try {
        String a = gui.username_tf.getText();
        char[] c = gui.password_tf.getPassword();
        String b = new String(c);
        result = action.login(a, b);
    } catch (Exception ee) {
        ee.printStackTrace();
    }
    if (result == true) { //<--this is where Eclipse shows me the error ...
        output = "You are Successfuly Loged In!";
    } else {
        output = "Username or Password is Wrong!";
    }
    gui.result_lb.setText(output);
}

public void actionListenerMeth(ActionListener ae) {
    gui.login_bt.addActionListener(ae);
}

}

}

看起来像
action.login(a,b)返回
null

因此,在您的例子中,布尔对象
null
应该与原语
false
进行比较。 因此,
null
将使用
boolean.booleanValue()
转换为一个基元
boolean
,它给出一个
NullPointerException

所以你必须改变:

if (result == true) 


您将
result
声明为
Boolean
对象,而不是
Boolean
原语。当执行
if(result==true)
时,VM尝试使用
result
上的方法调用将
result
转换为布尔基元,该方法调用为null

解决方案:将
结果
设置为布尔值。这将隐式初始化为
false

顺便说一句,您不需要说
if(result==true)
,您只需要说
if(result)

,在使用box type
Boolean
之前您需要学习。错误来自box type
Boolean
等于unbox type布尔值true或false。如果使用box type,则代码应为

Boolean result;    
if (result != null) 
或者,如果您使用unbox类型,您的代码应该是

boolean result;
if (result == true) {//TODO}
实际上,ubox布尔值不需要
==
。你可以这样申报

if (result){//DO SOMETHING}

action.login做什么?第29行是哪一行?该行中有空值:
nova.Remote.actionPerformed(Remote.java:29)
如果在
If(result==true)
中抛出异常,则表示
action.login(a,b)==null
显示您的
login
methodTrue,但是大概OP不认为
login
应该返回空值。问题是
有人能解释一下为什么我会得到这个空值异常
答案是正确的“解决方案:将结果设为布尔值”。不。。。这只会导致异常抛出到另一行。此代码存在一些问题,但提出的具体问题与将null对象转换为基元有关。这不是解决方案。问题是
login
返回的
Boolean
为空。是的,因为即使
gui.username\u tf.getText()
抛出异常,
result
仍然是一个值为false的有效原语(或者,因为它是一个实例变量,无论上次是什么-它实际上应该是一个局部变量). 测试
if(result)
将计算为false,而不是抛出一个NPE。但是,yes-action.login应该返回一个基元或非null布尔值。就像我说的,这个代码还有其他问题。你说得对,谢谢!如果(结果){//…}
足够的话,就必须从布尔值改为布尔值。我是新来的,我正在学习:)
boolean result;
if (result == true) {//TODO}
if (result){//DO SOMETHING}