gpt4 book ai didi

java - 每当我运行代码时都会出现 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-30 04:18:54 26 4
gpt4 key购买 nike

为了在夏天练习我的基本编程技能,我决定编写一个一维运动物理问题求解器。每当我尝试运行该程序时,都会收到 java.lang.NullpointerException 错误。我不知道我写错了什么才给我这个错误。注意:现在我假设solvFor变量的输入将是“加速度”,以便修复此错误:

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(“时间”))”

以及在主程序主体的第 40 行中出现错误:“问题1.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 类中的哪里给了变量一个字符串?

答案:你不知道!

请注意,在不同作用域中声明的两个同名变量不是同一个变量。仅仅因为您的两个类有一个 MissingVar String 变量并不意味着它们共享相同的变量,正如您所发现的,它们实际上并不共享。解决方案:在尝试使用PhysicsProblem 类中的missingVar 变量之前对其进行设置。为此,为类提供一个 setter 方法。

即,

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

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

关于java - 每当我运行代码时都会出现 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17625774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com