簡體   English   中英

Dr.java中的空指針異常

[英]null pointer exception in dr.java

我正在嘗試編寫一個程序,在該程序中我需要輸入一定數量的天數和起始溫度。 在整天內,溫度都會以某種方式變化。 然后打印最后一天的溫度。 我的教授說要使用類TempPattern,字段num_days和first_day_temp以及構造函數和finalTemp方法。 這是我所擁有的:

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

它可以編譯,但是會出現該錯誤。

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)

我認為某些東西是空值,但我真的不知道該如何解決。 我也認為我沒有正確地完成整個構造函數和字段的工作,因此請隨時提供任何幫助/建議,我對此表示贊賞。 我會清除所有沒有意義的內容。 提前TY。

這里有一些越野車,需要更改:

  • 主方法應該並且必須是靜態的。
  • findFinalDay()方法應該是靜態的,以便從main()方法調用它。
  • TemperaturePattern類不應是公共類,因為它打算用作內部類(據我的理解)。

找到以下修改后的代碼:

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;
      }
      }
}

靜態方法的說明:加載類時會創建一個static方法或變量。 僅當將類實例化為對象時(例如,使用new運算符),才會創建未聲明為static的方法或變量。

此外,雖然編制,並在開始執行的Java虛擬機不創建類的實例,而它只是加載main()方法中, main()方法必須與聲明static修改,以便盡快類加載后, main()方法可用。

這些變量,哪些是外部的類的方法main()不具有的方法static修飾符不能使用,直到類的實例已經為中的對象創建main()方法,所以你的情況方法'findFinalDayTemperature()'必須是靜態的,才能由'main()'方法調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM