2 Replies Latest reply: Oct 10, 2012 7:59 AM by aksarben RSS

    Code compiles but gives me an exception error

    966706
      I am getting this error message when on my program while running it, it compiled fine though.
      Exception in thread "main" java.lang.NullPointerException
      at BasalInput.main(BasalInput.java:27)
      Correct me if I am wrong but from what I have gathered that just means that something has a null value assigned in that particular line?
      I do not really understand what that means or how it happened but I am stumped and my text book has been rather vague and not helpful.
      Here is the program:
      import java.util.*;
      
      class BasalInput
      {
           
           static private BasalMetabolicRate myBMR;
           
           public BasalInput(double bmr)
           {
                bmr = myBMR.getBMR();
           }
           
           public static void main(String[] args)
           {
                double lb, inch, age;
                
                System.out.println("Weight in lbs: ");
                Scanner in = new Scanner(System.in);
                lb = in.nextDouble();
                
                System.out.println("Height in inches: ");
                inch = in.nextDouble();
                
                System.out.println("Age in years: ");;
                age = in.nextDouble();
                
                System.out.println(myBMR.toString());     
           }
      }
      Here is the class for that contains the Basal Metalbolic Rate data:
      class BasalMetabolicRate
      {
           private double weightKg;
           private double heightCm;
           private double ageYear;
           private double bmr;
           
            public BasalMetabolicRate(double h, double w, double a){
            inchToCm(h);
            lbToKg(w);
            ageYear=a;
            calcBMR();
            }
           
           public BasalMetabolicRate(){
           
           }
           
           private void inchToCm(double inch){          //1 inch = 2.54 cm
           double cm = 2.54 * inch;
           }
           
           
           private void lbToKg(double lb){                    //1 lb = .453592 kg
           double kg = .453592 * lb;
      
           }
           
           private void calcBMR(){               //male equation
           bmr = (66.4730 + (13.7516 * weightKg) + (5.0033 * heightCm) - (4.6756 * ageYear));
           }
           
           public double getBMR(){
           
           return bmr;
           
           
           }
           
           public String toString(){
              String s = "The Basal metabolic rate of a male, age ";
                s = s + ageYear + ", weight" + weightKg;
                s= s + "kgs, and height " + (heightCm / 100.0) + " meters, " + (heightCm % 100.0) + " cm.";
                return s;
           }
           
           
           
      }
        • 1. Re: Code compiles but gives me an exception error
          rp0428
          Welcome to the forum!
          >
          Exception in thread "main" java.lang.NullPointerException
          at BasalInput.main(BasalInput.java:27)
          >
          And what line is line 27 in your code?

          I mean is it this one?
          System.out.println(myBMR.toString());     
          Where you try to convery the 'myBMR' variable to a string when you have never created an instance yet?

          You have this declaration
          static private BasalMetabolicRate myBMR;
          But you never create an instance of it.

          And if you do try to create an instance of it you better do it before you try to create an instance of 'BasalInput' because that constructor needs the instance
          public BasalInput(double bmr)
               {
                    bmr = myBMR.getBMR();
               }
          • 2. Re: Code compiles but gives me an exception error
            aksarben
            I'm guessing from your question that you're fairly new to the language. I strongly recommend you invest a couple of hours in learning to use a debugger, which would show immediately what the problem is in the code. Most IDE's come with debugger built in.

            Good luck!