尝试处理用户输入的异常

时间:2013-10-14 15:32:32

标签: java if-statement nullpointerexception return-value

我有三个班级Main

public class Main {
public static void main(String []args){
    UserData myData = new UserData();
    SolveMath myMath = new SolveMath();
    myMath.findLength();
   }
}

UserData(获取用户输入):

import java.util.Scanner;
public class UserData {
private double side1;
private double side2;
private double side3;
private double weight;
public UserData(){
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter side one:");
    side1 = scan.nextDouble();
    System.out.println("Enter side two:");
    side2 = scan.nextDouble();
    System.out.println("Enter side three:");
    side3 = scan.nextDouble();
    System.out.println("Enter weight:");
    weight = scan.nextDouble();
}

public double getSide1(){
    return side1;
}
public double getSide2(){
    return side2;
}
public double getSide3(){
    return side3;
}
public double getWeight(){
    return weight;
  }
}

SolveMath(到目前为止只是试图找到最大的一面):

public class SolveMath {
private double length;
UserData userData;

public void findLength(){
    if(userData.getSide1() > userData.getSide2() && userData.getSide1() > userData.getSide3()){
        length = userData.getSide1();
    }else if(userData.getSide2() > userData.getSide1() && userData.getSide2() > userData.getSide3()){
        length = userData.getSide2();
    }else if(userData.getSide3() > userData.getSide1() && userData.getSide3() > userData.getSide2()){
        length = userData.getSide3();
    }
    System.out.println(length);
}

}

当我编译时,我得到这个错误(在给出所有输入后发生注释错误):

Exception in thread "main" java.lang.NullPointerException
at eval.user.data.SolveMath.findLength(SolveMath.java:17)
at drivers.Main.main(Main.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1

当我试图寻找最大的一面时,我是否遗漏了什么?

5 个答案:

答案 0 :(得分:2)

您没有在userData课程中初始化SolveMath变量,所以此行.-

if(userData.getSide1() > userData.getSide2() && userData.getSide1() > userData.getSide3())

在第一次访问NullPointerException时抛出userData

您可以为SolveMath类添加默认构造函数.-

public SolveMath() {
    userData = UserData();
}

答案 1 :(得分:2)

userData个实例中的SolveMath成员为null,因此您获得了NullPointerExceptionmain()需要告诉SolveMath实例,“这是一个UserData实例供您使用”,然后SolveMath实例需要使用它。

您可以通过向SolveMath的构造函数添加构造函数参数,并将实例从main()作为参数传递给构造函数调用来完成此操作。然后构造函数应该将实例存储在您已经拥有的成员变量中。

答案 2 :(得分:1)

userData类中的SolveMath变量始终为null,这意味着您正在尝试调用null上的方法,这是非法的(因此{{ 1}})。

您需要确保NullPointerException类中的userData字段从SolveMath获得对userData实例的引用。一种方法是简单地通过构造函数传递它,如下所示:

Main

要使其工作,您还需要向UserData userData = new UserData(); SolveMath myMath = new SolveMath(userData); // <-- add as parameter here 添加一个带有SolveMath参数的构造函数:

UserData

在尝试使用它之前,我还会考虑更改public class SolveMath { private UserData userData; public SolveMath(UserData userData) { // Set the private field to the instance passed in this.userData = userData; } ... } 函数以检查findLength()是否为userData(并可能抛出它自己的异常)。

答案 3 :(得分:0)

您没有在SolveMath中初始化UserData实例。

一种方法是通过下面的构造函数传递它

public class SolveMath {
    private double length;
    UserData userData;

    public SolveMath(final UserData userData) {
        this.userData = userData;
    }

    public void findLength(){
        if(userData.getSide1() > userData.getSide2() && userData.getSide1() >             userData.getSide3()){
        length = userData.getSide1();
       }else if(userData.getSide2() > userData.getSide1() && userData.getSide2() >    userData.getSide3()){
        length = userData.getSide2();
       }else if(userData.getSide3() > userData.getSide1() && userData.getSide3() > userData.getSide2()){
        length = userData.getSide3();
    }
    System.out.println(length);
    }
}

public class Main {
    public static void main(String []args){
        UserData myData = new UserData();
        SolveMath myMath = new SolveMath(myData);
        myMath.findLength();
    }
}

答案 4 :(得分:-1)

UserData为null。你必须将userData作为参数传递给solvemath,当然solvemath必须有一个set函数或构造函数来传递userdata。您可以在构造函数中提供默认值。因此,solvemath必须有一个额外的构造函数或set方法。你选择