2016-05-15 6 views
-2

が非常にうまくコンパイルされますが、実行時にメソッドUserInputWindowsの文字列入力でエラーが発生します。私は問題のない前にこのようなwhileループを書いたが、私は今なぜエラーになっているのか理解できないようだ。プログラムはどこにもありませんでした。私はちょうど私が何かエラーがあるかどうかを確認するためにそれを本当に走った。ここではそれは、単純なタイプミスだプログラムは非常にうまくコンパイルされますが、ドロップを実行すると文字列入力でエラー

enter code here 
import java.util.Scanner; 

public class PaintEstimate 
{ 
final static double WallAndCeilingPaintCost = 18.50;//cost for 1 gallon 
//final static double WhiteEnamelPaintCost = 9.50; //cost for 1 quart//using only for refrence now//changed to _MoldingCostPerQuart 
//final static double ColorEnamelPaintCost = 12.50; //cost for 1 quart//using only for refrence now// changed to _MoldingCostPerQuart 
final static double CoverageOfWallAndCeilingPaint = 250.00; // coverage of wall and ceiling paint in sqft 
final static double CoverageOfEnamelPaint = 100.00;// coverage of enamel paint both color and whit in linear sqft 

public static double IntchesToFeet; 
public static double RoomSquareFeet, CeilingSqaureFeet; 
public static double InchRoomWidth, InchRoomLength, InchRoomHeight; 
public static double FeetRoomWidth, FeetRoomLength, FeetRoomHeight; 
public static double NumberOfWindows, NumberOfDoors; 
public static double InchesWindowsHeight, InchesWindowsLength, InchesWindowsMoldingWidth; 
public static double InchesDoorsHeight, InchesDoorsLength, InchesDoorsMoldingWidth; 
public static String WindowsMoldingColorOrWhite, DoorsMoldingColorOrWhite; 
public static double WindowsMoldingCostPerQuart = 0; 
public static double DoorsMoldingCostPerQuart = 0; 
public static Scanner scan = new Scanner(System.in); 

public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 
    UserInputWallsAndCeiling(); 
    UserInputWindows(); 
    UserInputDoors(); 
} 
public static void UserInputWallsAndCeiling() 
{ 
    System.out.println("What is the room height in inches? "); 
    InchRoomHeight = scan.nextDouble(); 
    System.out.println("What is the room width in inches? "); 
    InchRoomWidth = scan.nextDouble(); 
    System.out.println("What is the room length in inches? "); 
    InchRoomWidth = scan.nextDouble(); 
} 
public static void UserInputWindows() 
{ 
    System.out.println("How many windows are in the room? "); 
    NumberOfWindows = scan.nextDouble(); 
    if(NumberOfWindows > 0) 
     { 
      System.out.println("What are the windows height in inches? "); 
      InchesWindowsHeight = scan.nextDouble(); 
      System.out.println("What are the windows length in inches? "); 
      InchesWindowsLength = scan.nextDouble(); 
      System.out.println("What are the windows molding width in inches? "); 
      InchesWindowsMoldingWidth = scan.nextDouble(); 
      System.out.println("Is the windows molding color or white? "); 

      boolean WindowsTrue = false; 
      while (!WindowsTrue) 
       { 
        System.out.println("Is the doors molding color or white? "); 
        DoorsMoldingColorOrWhite = scan.nextLine(); 
        if (WindowsMoldingColorOrWhite.equals("color")) 
         { 
          WindowsMoldingCostPerQuart = 12.50; 
          WindowsTrue = true; 
         } 
        else if (WindowsMoldingColorOrWhite.equals("white")) 
         { 
          WindowsMoldingCostPerQuart = 9.50; 
          WindowsTrue = true; 
         } 
        else 
         { 
          System.out.println("Please enter if the molding is color or white"); 
         } 
       } 
     } 
} 
public static void UserInputDoors() 
{ 
    System.out.print("How many doors are in the room? "); 
    NumberOfDoors = scan.nextDouble(); 
    if(NumberOfDoors > 0) 
     { 
      System.out.println("What are the doors height in inches? "); 
      InchesDoorsHeight = scan.nextDouble(); 
      System.out.println("What are the doors length in inches? "); 
      InchesDoorsLength = scan.nextDouble(); 
      System.out.println("What are the doors molding width in inches? "); 
      InchesDoorsMoldingWidth = scan.nextDouble(); 

      boolean DoorsTrue = false; 
      while (!DoorsTrue) 
       { 
        System.out.println("Is the doors molding color or white? "); 
        DoorsMoldingColorOrWhite = scan.nextLine(); 
        if (DoorsMoldingColorOrWhite.equals("color")) 
         { 
          DoorsMoldingCostPerQuart = 12.50; 
          DoorsTrue = true; 
         } 
        else if (DoorsMoldingColorOrWhite.equals("white")) 
         { 
          DoorsMoldingCostPerQuart = 9.50; 
          DoorsTrue = true; 
         } 
        else 
         { 
          System.out.println("Please enter if the molding is color or white"); 
         } 
       } 
      //System.out.print(InchesDoorsHeight + " " + InchesDoorsLength + " " + DoorsMoldingColorOrWhite + " " + DoorsMoldingCostPerQuart) 
     } 
} 
public static void IntchesToFeet() 
{ 
    FeetRoomWidth= InchRoomWidth/12; 
    FeetRoomLength = InchRoomLength/12; 
    FeetRoomHeight = InchRoomHeight/12; 
} 

}

+0

それは投げられるのですか? – CConard96

+0

実際にメソッドと変数の命名法を改善する必要があります。 – SamTebbs33

+0

私の変数はどんなに正確になりますか?彼らは文字通りコメントが必要なわけでもありません。なぜなら、その名前は彼らが何をするかを正確に伝えているからです。 –

答えて

0

以下コーダ;)

System.out.println("Is the doors molding color or white? "); 
DoorsMoldingColorOrWhite = scan.nextLine(); 
if (WindowsMoldingColorOrWhite.equals("color")) { 
     //... 
} 

ドアのWindows成形対成形

あなたが出てくるNullPointerExceptionが確認した場合このコードの行は、そのIF文を直接指しています。 tはその行で、または前に検索を開始することを示しています。:

エラーと何である何

スレッドでの例外PaintEstimate.UserInputWindowsで「メイン」のjava.lang.NullPointerException(PaintEstimate.java:61)

+0

ありがとう、私はあなたの助けに多く感謝します。私はあなたが言ったことを理解したとき、私はかなり笑っていた。多くの冗長化に私はちょうど私のコードをどこに貼り付けてコピーしていたのですか、ちょっとした細部を変更するのを忘れました。ありがとうございました。 –

+0

問題ありません。コードからは、使用できる言語の概念が限られているとします(チュートリアルなど)。変数を "public static"ではなく "private"にした場合、コンパイラの警告(未使用変数)によってエラーが発生する可能性があります。また、自分が2回以上コピー貼りをしているのを見つけたら、デザインを再考して、言語の機能を探して私を怠け者にするのに最適な時期です。 – patrickceg

関連する問題