Can't get my program to work

+2 Gijs Vermeulen · November 13, 2014
Hey,

I've been trying to make this program to make my math exercises easier, but I can't get it to work.
Here's my code:

body.java:

import java.util.Scanner;
public class body {
private static Scanner gijs;
public static void main(String[] args) {
System.out.println("This program calculates the Discriminant");
System.out.println("Please enter the values for a, b and c");
work workObject = new work();
workObject.calcDis();
workObject.calcSol();
System.out.printf("The Discriminant is %f", workObject.getDis());
System.out.printf("Solution 1 is %f", workObject.getSol1());
System.out.printf("Solution 2 is %f", workObject.getSol2());
}
}




work.java:

import java.util.Scanner;
public class work {
public static Scanner gijs;
private Double a;
private Double b;
private Double c;
private Double Discriminant;
private Double x1;
private Double x2;
public work (){
a = gijs.nextDouble();
b = gijs.nextDouble();
c = gijs.nextDouble();
}
public void calcDis(){
Discriminant = b*b - 4*a*c;
}
public void calcSol(){
x1 = ((-b)+Math.sqrt(Discriminant))/(2*a);
x2 = ((-b)-Math.sqrt(Discriminant))/(2*a);
}
public Double getDis(){
return Discriminant;
}
public Double getSol1(){
return x1;
}
public Double getSol2(){
return x2;
}
}


When I try to run this it gives me this:

Exception in thread "main" java.lang.NullPointerException
at work.<init>(work.java:11)
at body.main(body.java:7)

I am trying to make a class that calculates the Discriminant and then I want to implement that class in my main program.

Anyone have any idea of how I can fix this?

Post a Reply

Replies

Oldest  Newest  Rating
+1 Marijn van Stee · November 13, 2014
Hey,

This works for me:
import java.util.Scanner;

public class work {
public Scanner gijs;
private Double a;
private Double b;
private Double c;
private Double Discriminant;
private Double x1;
private Double x2;
public work (){
gijs = new Scanner(System.in);
a = gijs.nextDouble();
b = gijs.nextDouble();
c = gijs.nextDouble();
}
public void calcDis(){
Discriminant = b*b - 4*a*c;
}
public void calcSol(){
x1 = ((-b)+Math.sqrt(Discriminant))/(2*a);
x2 = ((-b)-Math.sqrt(Discriminant))/(2*a);
}
public Double getDis(){
return Discriminant;
}
public Double getSol1(){
return x1;
}
public Double getSol2(){
return x2;
}
}


import java.util.Scanner;

public class body {
 
public static void main(String[] args) {
System.out.println("This program calculates the Discriminant");
System.out.println("Please enter the values for a, b and c");
work workObject = new work();
workObject.calcDis();
workObject.calcSol();
System.out.printf("The Discriminant is %f", workObject.getDis());
System.out.printf("Solution 1 is %f", workObject.getSol1());
System.out.printf("Solution 2 is %f", workObject.getSol2());
}
}

Just let me know if it's works. This tutorial is about how the scanner works:
Java Programming Tutorial - 6 - Getting User Input
Marijn
0 Gijs Vermeulen · November 13, 2014
I am an idiot. I usually know how to use scanners I just forgot to add that one line :p Thanks for the help, it solved my problem :)
  • 1

Java / Android Development

107,236 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator