1

To practice my basic programming skills over the summer I decided to write a 1dimensional motion physics problem solver. I am getting a java.lang.Nullpointerexception error whenever I try to run the program. I can't figure out what I've written incorrectly to give me the error. NOTE: Right now I am assuming the input for the solveFor variable will be "acceleration" for the sake of fixing this error:

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();
        }
    }
}

This is the class file for the program. I'm getting an error in the line 36: "if (missingVar.equalsIgnoreCase("time"))"

As well as getting an error in line 40 of the main program body: "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: ");
        }
    }

Why is it throwing an exception?

Cœur
  • 37,241
  • 25
  • 195
  • 267
TommyD
  • 35
  • 6
  • 1
    `missingVar` is obviously null. Look back in the code to find out why. Where do you ever give the variable a String in the PhysicsProblem class?? Answer: you don't!!! – Hovercraft Full Of Eels Jul 13 '13 at 00:18
  • 2
    I wish I could replace [nullpointerexception] with [howtouseadebugger] .. see http://www.vogella.com/articles/EclipseDebugging/article.html (and others); as a developer, you *must* be the one who debugs your own code. – user2246674 Jul 13 '13 at 00:19
  • In the main body of the program: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(); – TommyD Jul 13 '13 at 00:20
  • TommyD: you're wrong. Please see my answer to see why. – Hovercraft Full Of Eels Jul 13 '13 at 00:23

2 Answers2

1

missingVar is obviously null. Look back in the code to find out why. And doing this, ask yourself where do you ever give the variable a String in the PhysicsProblem class?

Answer: you don't!

Note that two variables with the same name declared in different scopes are not the same variable. Just because your two classes have a missingVar String variable does not mean that they share the same variable, and as you're finding out, they in fact don't. The solution: set the missingVar variable in the PhysicsProblem class before trying to use it. Give the class a setter method for this.

i.e.,

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

And then call the method before using the variable.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    I thought it was important to explicitly point out different scopes, even if "two [different]" should imply it. – user2246674 Jul 13 '13 at 00:23
  • @user2246674: thank you for the improvement! – Hovercraft Full Of Eels Jul 13 '13 at 00:23
  • Thank you for the insight! So should I call that method within the class file or the main method? More explicitly, do I call it in the getUnknownsAccel method, or in the body where I am asking the user for the value of missingVar? – TommyD Jul 13 '13 at 00:28
  • Ok so I know I'm not supposed ask multiple questions in one thread but... now after I run the program (after I fixed everything you mentioned), the program just stops running after it takes the value for one of the missing variables. This is puzzling, since I have a do/while loop in the main body so that the program does not just end. – TommyD Jul 13 '13 at 00:36
1

You never initialize missingVar to anything, so it's null. You need to assign it something so it's not null.

Incidentally, you can switch the order in your call to avoid a NullPointerException here:

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

Also, on this line

if (missingVar == "none");

Remove the semicolon, because that semicolon is interpreted to be the body of the if block, causing your actual block below to not be associated with if (it would then always be executed, regardless of the condition in your if).

Don't compare string values with ==, which compares two objects references to see if they refer to the same object. Use the equals method:

if ("none".equals(missingVar))
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Bad idea to switch the order - then if the string is null it will fail silently and increase debugging time; better to explicitly check null and execute debug code if string is null – tckmn Jul 13 '13 at 00:48