Getting java.lang.NullPointerException whenever I run my code [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
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?

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.

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))

Related

how do i make the user input a double?

can somebody correct me what i did wrong here? my goal was for the user to input a number on the parameter System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1));
double upgradeAccessories= sc.nextDouble(); but i can't seem to see the issue
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("#####");
Scanner sc = new Scanner(System.in);
double availAmount;
Car owner = new Car();
owner.owner("Marcus Laurence", 2014);
double availAmount1=owner.upgradeAccessories(availAmount);
double remainAmount=owner.upgradeAccessories(availAmount);
System.out.println("Owner:" + owner.name);
System.out.println("Model:" + owner.model);
System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1));
double upgradeAccessories= sc.nextDouble();
System.out.println("Installed AC:" + owner.hasAC);
System.out.println("Installed Leather Seats:" + owner.hasLeatherSeats);
System.out.println("Installed Back Wipers:"+ owner.hasBackWipers);
System.out.println("Installed Fog Lights:" + owner.hasFogLights);
System.out.println("Amount Remaining After Upgrade:" + df.format(remainAmount));
}
public double upgradeAccessories(double availAmount)
{
if(availAmount == 25000)
{
availAmount -= 21500;
hasAC=true;
}
else if(availAmount == 40000)
{
availAmount -= 21500;
availAmount -= 14400;
hasAC=true;
hasLeatherSeats=true;
}
else if(availAmount == 50500)
{
availAmount -=21500;
availAmount -=14400;
availAmount -=6250;
availAmount -=3300;
hasAC=true;
hasLeatherSeats=true;
hasBackWipers=true;
hasFogLights=true;
}
return availAmount;
}
how do i make the user input a double?
You did this part fine - scanner.nextDouble() is all it takes. Here's a really simple standalone example, including output:
System.out.print("enter a double: " );
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
System.out.println("d: " + d);
enter a double: 123.456
d: 123.456
So reading a double is not the issue. However, this line is suspicious:
double upgradeAccessories = sc.nextDouble();
Why? A few reasons..
It defines a new variable "upgradeAccessories" which is not used anywhere else – so it's reading a value (from sc.nextDouble()) and then doing nothing with it. This is something that an IDE (such as IntelliJ or Eclipse) will help you see – it will draw attention to unused code, like declaring a value that is never used.
The name of the variable – "upgradeAccessories" – is the same as the method name defined elsewhere in your code, namely:
public double upgradeAccessories(double availAmount)
So to fix your code, it seems that you probably want to replace this:
double upgradeAccessories = sc.nextDouble();
with something like this:
double next = sc.nextDouble();
owner.upgradeAccessories(next);
Also, the way you're comparing doubles is dangerous. Generally, fractional numbers are never exactly equal. Instead, of doing comparisons like this:
if (availAmount == 25000) { ... }
It's better to do something like greater-than-or-equal:
if (availAmount >= 25000) { ... }
Or if you're using only whole-number values, use integer instead of double, and then direct x == y comparisons will work fine.
In this segment of your code...
double availAmount;
Car owner = new Car();
owner.owner("Marcus Laurence", 2014);
double availAmount1=owner.upgradeAccessories(availAmount);
... you passed the "availAmount" in to a function as a parameter. You haven't given that variable any value yet. You just declared it. If you assign a default value to it like 0.0, does it work?

How to set integer range in Scanner? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am learning java and I want to create a command-line application that calculates exam percentages based on marks obtained. But the problem is I don't have the idea to set the range of marks obtained while the marks range is between 0 to 100.
Below is the code, I have tried: -
package com.company;
import java.util.*;
public class CbseCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter marks obtained of Physics");
float physics = sc.nextFloat();
System.out.println("Enter marks obtained of Chemistry");
float chemistry = sc.nextFloat();
System.out.println("Enter marks obtained of Math");
float math = sc.nextFloat();
System.out.println("Enter marks obtained of English");
float english = sc.nextFloat();
System.out.println("Enter marks obtained of Computer Science");
float computer = sc.nextFloat();
float total = 500;
float obtained = (physics + chemistry + math + english + computer);
float percentage = (obtained/total)*100;
System.out.println("The percentage obtained is: "+percentage);
sc.close();
}
}
It is not a good idea to try to get Scanner to do that1.
Instead, you should use Scanner to read an int and then test the result that it gives you to check that it is in the correct range. Something like this:
int number;
if (myScanner.hasNextInt()) {
number = myScanner.nextInt();
if (number < 0 || number > 100) {
// handle case where the number is out of range
}
} else {
// handle case where the input is not an integer
}
I will leave it to you to figure out how to map the above onto your application's requirements.
1 - The standard Scanner class doesn't provide a method that reads a number in a given range (and rejects numbers outside of that range). You could conceivably extend the Scanner class with this functionality, but it would be difficult. There are simpler solutions.
I would suggest you to write a function to get a valid input as below :-
public int getValidInput(Scanner in, int range) {
while (in.hasNext()) {
if (in.hasNextInt()) {
int val = in.nextInt();
if (val >= 0 && val < range) { // <-- from "0" to "range".
return val;
}
} else {
in.next();
}
}
return -1;
}
This function is ensuring that the input is given as an integer only and it lies in the range o to range. You can change it as per your requirement.
Consider this method:
static int getMark(String course){
int mark = 0;
boolean valid = true;
Scanner sc = new Scanner(System.in);
System.out.println("Enter marks obtained of " + course + ": ");
while(valid){
mark = sc.nextInt();
if(mark < 0 || mark > 100){
System.out.println("Mark must be between 0-100");
} else {
valid = true;
}
}
sc.close();
return mark;
}
This way you can get two birds with one stone, leaving the resulting code as this:
public static void main(String[] args){
int physics = getMark("Physics");
int chemistry = getMark("Chemistry");
int math = getMark("Math");
int english = getMark("English");
int computer = getMark("Computer Science");
float total = 500;
float obtained = (physics + chemistry + math + english + computer);
float percentage = (obtained / total) * 100;
System.out.println("The percentage obtained is: " + percentage);
}

validation, the users input must equal to his/her account number and pin, is this the correct way? i seem to just be getting errors

This part of code is not in a function as I am declaring all values before they can get used by future functions.
Here is a snippet that I'm struggling with
validate if user input is correct
if (userinput.equals(x.indexOf("1234")) && userinput.equals(pinA.indexOf("1223")) ){
{System.out.println(x);
int bal = 500;
System.out.println("your balance is: " + bal);
MainMenu();
}
// that if statement is giving me errors
//here is my variable declaration
public class SimpleBankingApp {
//declare the pins
String pinA = "1223";
int pinB = 1227;
int pinC = 7643;
int pinD = 8554;
// have the users details in strings
public String x = "1234 500";
public String b = "3546 50000";
public String d = "4253 6000";
public String t = "7722 804";
//get user input from user
static {System.out.println("enter your choice");}
Scanner a = new Scanner(System.in);
String userinput = a.nextLine();
if (userinput.equals(x.indexOf("1234")) && userinput.equals(pinA.indexOf("1223")) ){
{System.out.println(x);
int bal = 500;
System.out.println("your balance is: " + bal);
MainMenu();
}
By the declaration of the pins, I tested to see if a string variable would work hence why one of them is a string
You don't need to use indexOf() here. Just use equals like this userinput.equals(pinA), also use "or"(||) instead of "and"(&&) in your if condition or use 2 user inputs if you want to compare 2 separate fields, because 1 field can't be equals to 2 different strings

Java out of scope

My program is nearly done except for one problem. I'm having an out of scope problem with the for loop. The goal of the program is to compound monthly interest for a user inputted amount & term.
An example of output at $5000 principal with 5% interest for 3 years would be:
Month: Interest: Principal:
1 $20.83 $5020.83
2 $20.92 $5041.75
etc etc etc
Starting Balance = $ 5000.00 // having problem outputting these w/ for-loop
Final Account Balance = $ 5807.36 // System.out.print keeps repeating multiple times
Total Interest Paid = $ 807.36 // but i can't use variables outside of loop
My problem is that during my for loop, I keep outputting Starting Balance, Final Balance and Total Interest every time the program goes through the loop. but if I try to use the variables outside the loop it goes out of scope and if I try to declare variables outside of the loop I can't use them inside the loop because it's already been declared in the constructor.
Can anyone give me some hints or advice?
My code:
public class Calculator
{
public Calculator()
{
Scanner input = new Scanner(System.in);
boolean error = false;
while (!error){
System.out.print("Please input the following: principal, interest rate, term >> ");
double principal = input.nextDouble();
double interest_rate = input.nextDouble();
int term = input.nextInt();
String Month = input.next();
char dollar_sym = 36;
if (interest_rate <= 0 || term <= 0 || principal <= 0) // input validation
{
System.out.println("The term, interest rate and principal must be greater
than zero");
continue;
}
if (!Month.equals("month")) // input validation
{
System.out.println("Please input month after term");
continue;
}
System.out.println("Month: " + " Interest: " + "Principal: ");
if (Month.equals("month"))
{
for (int month = 1; month <= term; month++)
{
double interest = (principal * interest_rate / 100) / 12;
principal = principal + interest;
System.out.printf("%4d %c%5.2f %c%5.2f\n", month,
dollar_sym, interest, dollar_sym, principal );
double start_principal = principal - interest; // problem
double final_principal = principal; // problem
double total_interest = interest * interest_rate; // problem
System.out.println(" Starting balance = " + start_principal ); // problem
System.out.println("Final account balance = " + final_principal ); // problem
System.out.println("Total Interest Paid = " + total_interest); // problem
}
}
}
}
}
Declare them before the loop begins, so they will exist inside the loop and after it:
double start_principal = 0;
double final_principal = 0;
double total_interest = 0;
Scanner input = new Scanner(System.in);
boolean error = false;
while (!error) {
// ...
}
// ...
In my answer, I am assuming that when you say goes out of scope, you mean that you get a compile time error. (note, it would make the answer easier to address if you provided the error message and the line that is causing the error message).
The scope of a variable refers to where the variable is accessible. For example, if you declare a variable inside of an if statement, the scope is that if statement. Some example code:
public void updateStatus(){
Boolean shouldCheckStatus = true;//this variable is declared inside of the updateStatus method, so that is the scope of the variable
if(shouldCheckStatus == true){
Int hitCounter = 0;//this variable is declared inside of the if statement, so it is only accessible inside of the if statement
//do some work
if(hitCounter > 100){
self.registerHits(hitCounter);//hitCounter is still accessible here, because it is still inside of the if statement
}
else{
shouldCheckStatus = false;
}
}//close the if statement and so close the scope...
//the hitCounter variable is no longer in scope, because we are no longer in the if statement
//but shouldCheckStatus is still in scope, because we are still in the method
if(shouldCheckStatus == true){
self.callAnotherMethod();
}
}
So in your problem, you need to declare your variable above where you want to use it, inside of the scope that you want to use it. And then not declare it again. So declare before the loop.

How do I declare a string as a user input, then check to see if it's "yes" or "no"? (JAVA)

I cannot get my program to take a user input as a string and then see if it equals "yes" or "no" and if it equals neither, then to display "incorrect entry." It always displays "incorrect entry" regardless if I type "yes" or "no." I have tried a few different types of if and do/while's but I just can't seem to get it:
Class file:
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
//Make sure to add acceleration
public PhysicsProblem (double vI, double vF, double t, double deltaX)
{
this.vI = vI;
this.vF = vF;
this.t = t;
this.deltaX = deltaX;
}
public void setVi(String strVi)
{
while (!(strVi.equals("no") || strVi.equals("yes")));
{
System.out.println("incorrect entry");
}
if (strVi.equals("yes"))
{
System.out.println("Enter the initial velocity: ");
vI = new Scanner(System.in).nextDouble();
}
if (strVi.equals("no"))
{
System.out.println("The program is assuming you want to solve" +
"for intial velocity");
}
}
Program:
import java.util.Scanner;
public class PhysicsProblemSolver
{
public static void main (String[] args)
{
double vI = 0;
double vF = 0;
double t = 0;
double deltaX = 0;
Scanner scan = new Scanner(System.in);
PhysicsProblem problem1 = new PhysicsProblem (vI, vF, t, deltaX);
// Checks to see if initial velocity is given
System.out.println("Do you know the initial velocity? (Type 'yes' or 'no')");
String strVi = scan.next();
problem1.setVi(strVi);
I know it looks like an incomplete program, and it is, but I just needed help with this one section, so I tried not to include the unnecessary parts. Sorry if it's confusing!
you can do the testing in main itself i you can,
Scanner sc = new Scanner(System.in);
String input;
do{
System.out.println("enter valid input");
input=sc.next();
}while(!(input.equals("yes") || input.equals("no")));
Then simply pass input to function.
remove unnecessary checks there. as others mentioned its infinite loop(deadlock).
while (!(strVi.equals("no") || strVi.equals("yes")));
while (!(strVi.equals("no") || strVi.equals("yes")));
will always evaluate to true if the input is neither 'yes' nor 'no' so it will go into infinite loop.
Have you tried:
public void setVi(String strVi){
if(strVi.equalsIgnoreCase("yes")){
//What if yes
}else if(strVi.equalsIgnoreCase("no")){
//What if no
}else{
//What if not yes nor no
}
}

Categories

Resources