drjava - null pointer exception in dr.java -


im trying make program input of amount of days, , starting temperature. temperature changes in way throughout amount of days. prints temperature of final day. professor said use class temppattern, fields num_days , first_day_temp constructor , finaltemp method. heres 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;                                                                     (int current_day = 1; current_day <= half; current_day++){                           newtemp = newtemp - 2;                                                              }               (int current_day = half + 1; current_day <= days; current_day++){                    newtemp++;                                                                      }               system.out.println("temperature on final day " + 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 error comes up.

java.lang.nullpointerexception     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)     @ java.lang.reflect.method.invoke(unknown source)     @ edu.rice.cs.drjava.model.compiler.javaccompiler.runcommand(javaccompiler.java:267) 

i think null value don't know how fix this. don't think did whole constructor , fields stuff correctly feel free give help/advice, id appreciate it. i'll clear doesn't make sense. ty in advance.

there couple of things buggy here, needs changed:

  • main method should , must static.
  • findfinalday() method should static, called main() method.
  • temperaturepattern class should not public , intended serve inner class (as per understanding).

find 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;           (int current_day = 1; current_day <= half; current_day++){                           newtemp = newtemp - 2;                                                              }           (int current_day = half + 1; current_day <= days; current_day++){                    newtemp++;                                                                      }               system.out.println("temperature on final day " + 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: static method or variable created @ time class loaded. method or variable not declared static created when class instantiated object example using new operator.

also, java virtual machine not create instance of class rather loads while compilation , starts execution @ main() method, main() method must declared static modifier class loaded, main() method available.

those variables , methods of class outside of main() method not have static modifier can not used until instance of class has been created object within main() method, in case method 'findfinaldaytemperature()' has static called 'main()' method.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -