每当我运行代码时都会获得java.lang.NullPointerException

时间:2013-07-13 00:17:57

标签: java nullpointerexception logic

为了在夏天练习我的基本编程技巧,我决定编写一个1维运动物理问题求解器。每当我尝试运行程序时,我都会收到java.lang.Nullpointerexception错误。我无法弄清楚我写错了什么来给我错误。注意:现在我假设为了解决这个错误,solveFor变量的输入将是“加速”:

import java.util.Scanner;

public class PhysicsProblem
{
private double vI; // initial velocity  
private double vF; // final velocity    
private double t;  // time 
private double deltaX;  // change in the x value
private double accel;
private String missingVar;

public PhysicsProblem (double acceleration, double initialV, double finalV, double time, double changePosition) 
{
    acceleration = accel;
    initialV = vI;
    finalV = vF;
    time = t;
    changePosition = deltaX;    
}

Scanner scan = new Scanner(System.in);

public void getUnknownsAccel()
{
    //-----------
    // checks for another unknown value that is not accel
    //-----------
    if (missingVar.equalsIgnoreCase("time"))
    {
        System.out.println("Please enter the value for time: ");
        t = scan.nextDouble();
        while (t <= 0 || !scan.hasNextDouble())
        {
            System.out.println("That is not an acceptable value!");
            t = scan.nextDouble();
        }
    }       
    if (missingVar.equalsIgnoreCase("initial velocity"))
    {
        System.out.println("Please enter the value for initial velocity: ");
        vI = scan.nextDouble();
        while (!scan.hasNextDouble())
        {
            System.out.println("That is not an acceptable value!");
            vI = scan.nextDouble();
        }
    }
    if (missingVar.equalsIgnoreCase("final velocity"))
    {
        System.out.println("Please enter the value for final velocity: ");
        vF = scan.nextDouble();
        while (!scan.hasNextDouble())
        {
            System.out.println("That is not an acceptable value!");
            vF = scan.nextDouble();
        }
    }
    if (missingVar.equalsIgnoreCase("delta X"))
    {
        System.out.println("Please enter the value for delta X: ");
        deltaX = scan.nextDouble();
        while (!scan.hasNextDouble())
        {
            System.out.println("That is not an acceptable value!");
            deltaX = scan.nextDouble();
        }
    }
}

这是程序的类文件。我在第36行收到错误: “if(missingVar.equalsIgnoreCase(”time“))”

除了在主程序体的第40行中收到错误: “problem1.getUnknownsAccel();”

public static void main (String[] args)
{

    String missingVar;      // other missing variable
    double vI = 0;
    double vF = 0;
    double t = 0;
    double deltaX = 0;
    double accel = 0;
    Scanner scan = new Scanner(System.in);

    PhysicsProblem problem1 = new PhysicsProblem (accel, vI, vF, t, deltaX);

    System.out.println("Which variable are you solving for? ");
    String solveFor = scan.nextLine();


    // after receiving solveFor input, assesses data accordingly

    if (solveFor.equalsIgnoreCase("acceleration"))
    {
        System.out.println("Solving for Acceleration!");
        System.out.println("Are there any other unknowns? (enter 'none' or the name " +
                "of the variable)");
        missingVar = scan.nextLine();
        do
        {
            problem1.getUnknownsAccel();
            System.out.println("Are there any other unknowns? (enter 'none' or the name " +
                    "of the variable)");
            missingVar = scan.nextLine();
        }   
        while (!missingVar.equalsIgnoreCase("none") || !missingVar.equalsIgnoreCase("acceleration"));

        if (missingVar == "none");
        {
            // Write code for finding solutions
            System.out.println("Assuming you have given correct values, the solution is: ");
        }
    }

为什么会抛出异常?

2 个答案:

答案 0 :(得分:1)

missingVar显然是空的。回顾一下代码,找出原因。这样做,问问自己,在PhysicsProblem类中,你在哪里给变量一个String?

答案:你没有!

请注意,在不同范围内声明的两个具有相同名称的变量是不是同一个变量。仅仅因为你的两个类有一个missingVar String变量并不意味着它们共享同一个变量,而你发现它们实际上并没有。解决方案:在尝试使用之前,在PhysicsProblem类中设置missingVar变量。为此类提供一个setter方法。

即,

public void setMissingVar(String missingVar) {
  this.missingVar = missingVar;
}

然后在使用变量之前调用该方法。

答案 1 :(得分:1)

您永远不会将missingVar初始化为任何内容,因此它是null。你需要给它一些东西,所以它不是空的。

顺便提一下,您可以在通话中切换订单,以避免在此处NullPointerException

while (!"none".equalsIgnoreCase(missingVar) ||
       !"accelmissingVar".equalsIgnoreCase(missingVar));

另外,在这一行

if (missingVar == "none");

删除分号,因为该分号被解释为if块的主体,导致下面的实际块与if无关(它将始终执行,无论您if)中的条件。

不要将字符串值与==进行比较,后者会比较两个对象引用,以查看它们是否引用同一个对象。使用equals方法:

if ("none".equals(missingVar))