This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                                2年前关闭。
            
                    
为了在暑假期间练习基本的编程技能,我决定编写一维运动物理问题解决程序。每当我尝试运行程序时,都会收到java.lang.Nullpointerexception错误。我无法弄清楚我写错了什么给我错误。注意:现在,为了解决此错误,我假设solveFor变量的输入为“ acceleration”:

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行出现错误:
“如果(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: ");
        }
    }


为什么会引发异常?

最佳答案

missingVar显然为空。回顾一下代码,找出原因。然后,问问自己,您在PhysicsProblem类中的哪个位置给变量提供了String?

答:你不!

请注意,在不同作用域中声明的两个具有相同名称的变量不是同一变量。仅仅因为您的两个类都有一个MissingVar String变量并不意味着它们共享相同的变量,而且正如您所发现的那样,实际上它们并非相同。解决方案:尝试使用PhysicsProblem类中的missingVar变量。为此给类提供一个setter方法。



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


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

08-05 19:39