显示java.lang.NullPointerException

时间:2012-09-27 20:45:42

标签: java

每次运行程序时都会得到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;
   }
}

4 个答案:

答案 0 :(得分:1)

您需要将main方法以及其中使用的变量设置为静态:

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

答案 1 :(得分:0)

  • 将所有变量(keyboard twoDform OP IntM FM Age)更改为static

  • 将您的方法Calculation更改为private static String
    Calculation

  • 将您的公共虚空主页更改为public static void main

然后你会有一些东西运行。

作为旁注Dollers更改为Dollars:P

答案 2 :(得分:0)

您需要将main方法设为静态,如 gtgaxiola 所示。

然后,您需要将其他方法和属性设置为静态,因为您没有创建类的实例并且正在从main方法中以程序方式运行。

    static DecimalFormat twoDform 

    public static void main(String[] args)

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

另请注意,Java中的常规方法和成员变量名称以小写字母开头,即calculation()而不是Calculation()。打破这个惯例只会让任何试图阅读你的代码的人感到困惑。

答案 3 :(得分: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