javadrjava

null pointer exception in dr.java


Im trying to make a program where I get input of a certain amount of days, and a starting temperature. The temperature changes in a certain way throughout the amount of days. It then prints the temperature of the final day. My professor said to use the class TempPattern, fields num_days and first_day_temp as well as a constructor and finalTemp method. Heres what I have:

  public class TempPattern{ 

    int num_of_days = 0;
    int temp_first_day = 0;

    public void TempPattern(int temp, int days){
         days = num_of_days;
         temp = temp_first_day; 
    }
      public int num_of_days(int days){
       return days;
      }
      public int temp_first_day(int temp){
        return temp;
      }

      }

        public void finalDayTemp(int days, int temp){
           int half = days / 2; 
           int newtemp = temp + 2;                                                     

              for (int current_day = 1; current_day <= half; current_day++){        
                  newtemp = newtemp - 2;                                                
             }
              for (int current_day = half + 1; current_day <= days; current_day++){ 
                  newtemp++;                                                        
             }
              System.out.println("Temperature on final day would be " + newtemp);
      }
         public void main(String[] args){
         Scanner keyboard = new Scanner(System.in);           
           int days;                                         
           int temp; 

          System.out.print("number of days: ");   
             days = keyboard.nextInt();                                        

          System.out.print("temperature of first day: ");              
             temp = keyboard.nextInt(); 

             finalDayTemp(days,temp);
       }

It compiles but that error comes up.

java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

I think something is a null value but I really don't know how to fix this. I also don't think I did the whole constructor and fields stuff correctly so feel free to give any help/advice, Id appreciate it. I'll clear up anything that doesn't make sense. TY in advance.


Solution

  • There are couple of things buggy here, which needs to be changed:

    Find the below modified code :

    import java.util.Scanner;
    public class HWfive{ 
            public static void findFinalDayTemperature(int days, int temp){
               int half = days / 2; 
               int newtemp = temp + 2;
              for (int current_day = 1; current_day <= half; current_day++){        
                      newtemp = newtemp - 2;                                                
                 }
              for (int current_day = half + 1; current_day <= days; current_day++){ 
                      newtemp++;                                                        
                 }
                  System.out.println("Temperature on final day would be " + newtemp);
          }
             public static void main(String[] args){
             Scanner keyboard = new Scanner(System.in);           
               int days;                                         
               int temp;
              System.out.print("number of days: ");   
                 days = keyboard.nextInt();                                      
              System.out.print("temperature of first day: ");              
                 temp = keyboard.nextInt(); 
                 findFinalDayTemperature(days,temp);
           }
    class TemperaturePattern{ 
        int num_of_days = 0;
        int temp_first_day = 0;
         public void TemperaturePattern(int temp, int days){
             days = num_of_days;
             temp = temp_first_day; 
            }    
         public int num_of_days(int days){
           return days;
          }    
         public int temp_first_day(int temp){
            return temp;
          }
          }
    }
    

    Explanation of static methods: A static method or variable is created at the time a class is loaded. A method or variable that is not declared as static is created only when the class is instantiated as an object for example by using the new operator.

    Also, the Java Virtual Machine does not create an instance of the class rather it just loads while compilation and starts execution at the main() method, the main() method must be declared with the static modifier so that as soon as the class is loaded, the main() method is available.

    Those variables and methods of the class which are outside of the main() method which do not have the static modifier can not be used until an instance of the class has been created as an object within the main() method, so in your case method 'findFinalDayTemperature()' has to be static to get it called by the 'main()' method.