2012-09-27 48 views
-5

它每次運行程序時都會得到java.lang.NullPointerException。甚至不會採取任何輸入。我真的不知道爲什麼會發生這種情況。java.lang.NullPointerException

另外,我有多少要解釋這個問題,所以stackoverflow將允許我發佈這個?

import java.util.Scanner; 
import java.text.DecimalFormat; 

public class A1Q2 { 

    Scanner keyboard = new Scanner(System.in); //declare scanner for keyboard input 
    DecimalFormat twoDform = new DecimalFormat("#.##");//declare decimal format for closest cent 

    double OP = 0; //original price of the car 
    double IntM = 0; // store the value of mileage 
    double FM = 0; //the mile age of the car 
    double Age = 0; //the age of the car 


    public static void main(String[] args) { 
     // DECLARE VARIABLES/DATA DICTIONARY 

     // Read in the original values 
     System.out.println("Please enter the original price of the car: "); 
     OP = keyboard.nextDouble();// taking input for the original price 

     //Read in the mileage factor 
     System.out.println("Please enter the mileage factor as 0.7(high),1(average) and 1.2(low):"); 
     FM = keyboard.nextDouble();// taking input for the general mileage of the car 

     //Read in the age of cars 
     System.out.println("Place enter the age of the car :"); 
     Age = keyboard.nextDouble();// taking input for the age of the car 


     // Display the result, Call method to estimate the current price of the car 
     System.out.println("Estimated price of the car is " + Calculation(Age, FM, OP) + " Dollers"); 

    } 


    private String Calculation(double Age, double FM, double OP) { 

     // Declaration of the result variable 
     String EstPrice = ""; 

     //estimate the current price of the car 
     EstPrice = twoDform.format(Math.sqrt(1.0/(Age + 1)) * FM * OP); 

     return EstPrice; 
    } 
} 
+9

你能請張貼堆棧跟蹤? –

+0

請包含完整的錯誤/堆棧跟蹤! –

+10

它不是public void main(String [] args)它是'public static void main(String [] args){' – gtgaxiola

回答

0
  • 更改所有的變量(keyboardtwoDformOPIntMFMAge)到static

  • 更改方法Calculationprivate static String
    Calculation

  • 更改公共無效主要以public static void main

然後,你會有東西運行。

作爲一個側面說明 變化DollersDollars:P

+0

非常感謝您...我是新來的... –

0

你需要讓main方法靜態的,如gtgaxiola說。

然後,您將需要使其他方法和屬性成爲靜態,因爲您不會創建類的實例並且正在從主方法程序運行。

static DecimalFormat twoDform 

    public static void main(String[] args) 

    private static String Calculation(double Age, double FM, double OP) 

還請注意,它是在常規Java進行方法和構件的變量名,開始用小寫字母,即calculation()Calculation()。打破這一慣例只會讓任何想要閱讀代碼的人感到困惑。

+0

謝謝您不知道有一個約定 –

1

你需要做的主要方法靜態以及那些在它使用的變量:

import java.util.Scanner; 
import java.text.DecimalFormat; 

    public class A1Q2 { 

    static Scanner keyboard = new Scanner(System.in); //declare scanner for keyboard input 
    static DecimalFormat twoDform = new DecimalFormat("#.##");//declare decimal format for closest cent 

    static double OP = 0; //original price of the car 
    static double IntM = 0; // store the value of mileage 
    static double FM = 0; //the mile age of the car 
    static double Age = 0; //the age of the car 


public static void main(String[] args) { 
    // DECLARE VARIABLES/DATA DICTIONARY 

    // Read in the original values 
    System.out.println("Please enter the original price of the car: "); 
     OP = keyboard.nextDouble();// taking input for the original price 

    //Read in the mileage factor 
    System.out.println("Please enter the mileage factor as 0.7(high),1(average) and 1.2(low):"); 
     FM = keyboard.nextDouble();// taking input for the general mileage of the car 

    //Read in the age of cars 
    System.out.println("Place enter the age of the car :"); 
     Age = keyboard.nextDouble();// taking input for the age of the car 


    // Display the result, Call method to estimate the current price of the car 
     System.out.println("Estimated price of the car is " + Calculation(Age, FM, OP) + " Dollers"); 

} 


private static String Calculation(double Age, double FM, double OP) { 

    // Declaration of the result variable 
    String EstPrice = ""; 

    //estimate the current price of the car 
    EstPrice = twoDform.format(Math.sqrt(1.0/(Age + 1)) * FM * OP); 

    // Return the result 
    return(EstPrice); 
    } 
} 
0

你的程序,使屬性和方法靜態後,爲我工作。

import java.text.DecimalFormat; 
import java.util.Scanner; 

public class A1Q2 { 
    static Scanner keyboard = new Scanner(System.in); //declare scanner for keyboard input 
    static DecimalFormat twoDform = new DecimalFormat("#.##");//declare decimal format for closest cent 

    static double OP = 0; //original price of the car 
    static double IntM = 0; // store the value of mileage 
    static double FM = 0; //the mile age of the car 
    static double Age = 0; //the age of the car 


public static void main(String[] args) { 
    // DECLARE VARIABLES/DATA DICTIONARY 

    // Read in the original values 
    System.out.println("Please enter the original price of the car: "); 
     OP = keyboard.nextDouble();// taking input for the original price 

    //Read in the mileage factor 
    System.out.println("Please enter the mileage factor as 0.7(high),1(average) and 1.2(low):"); 
     FM = keyboard.nextDouble();// taking input for the general mileage of the car 

    //Read in the age of cars 
    System.out.println("Place enter the age of the car :"); 
     Age = keyboard.nextDouble();// taking input for the age of the car 


    // Display the result, Call method to estimate the current price of the car 
     System.out.println("Estimated price of the car is " + Calculation(Age, FM, OP) + " Dollers"); 

} 


private static String Calculation(double Age, double FM, double OP) { 

    // Declaration of the result variable 
    String EstPrice = ""; 

    //estimate the current price of the car 
    EstPrice = twoDform.format(Math.sqrt(1.0/(Age + 1)) * FM * OP); 

    // Return the result 
    return(EstPrice); 
    } 
} 

輸出

Please enter the original price of the car: 
9000 

Please enter the mileage factor as 0.7(high),1(average) and 1.2(low): 
0.7 

Place enter the age of the car: 
5 

Estimated price of the car is 2571.96 Dollars