1

我有三门课,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

当我试图找到最大的一面时,我错过了什么吗?

4

5 回答 5

2

您没有userData在课堂上初始化变量SolveMath,所以这一行。-

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

NullPointerException在第一次访问时抛出userData.

您可以为类添加一个默认构造函数SolveMath。-

public SolveMath() {
    userData = UserData();
}
于 2013-10-14T15:34:40.387 回答
2

userDataSolveMath实例中的成员是null,所以您得到NullPointerException. main()需要告诉SolveMath实例,“这里有一个UserData实例供你使用”,然后SolveMath实例需要使用它。

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

于 2013-10-14T15:35:33.287 回答
1

类中的userData变量SolveMath总是null,这意味着您正在尝试调用 上的方法null,这是非法的(因此是NullPointerException)。

您需要确保您的类中的userData字段从. 一种方法是简单地通过构造函数传入它,如下所示:SolveMathuserDataMain

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是(并可能抛出它自己的异常)。null

于 2013-10-14T15:35:51.120 回答
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();
    }
}
于 2013-10-14T15:42:00.490 回答
-1

用户数据为空。您必须将 userData 作为参数传递给solvemath,当然solvemath 必须具有用于传递用户数据的集合函数或构造函数。您可以在构造函数中提供默认值。所以,solvemath 必须有一个额外的构造函数或一个 set 方法。你选

于 2013-10-14T15:35:57.543 回答