我有三门课,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
当我试图找到最大的一面时,我错过了什么吗?