2013-06-24 33 views
0

我正在學習Java,我寫了這個非常簡單的類。一切都很好,直到我走到最後。第二個showinputDialog後,它繼續從另一個Java文件我完成另一個對話框。我想我的代碼有錯誤,或者我的eclipse中的某些設置關閉。Java:最後運行另一個程序

// Reads in the radius and length of a cylinder and computes volume 
// using the formula: area = radius * radius * PI 
//     volume = area * length 

import javax.swing.JOptionPane; 

class ComputeVolumeOfCyclinder { 
    public static void main(String[] arugs) { 
     final double PI = 3.1415; 
     String radiusString = JOptionPane.showInputDialog(
       "Enter the radius, \n for example: 15.25"); 

     String lengthString = JOptionPane.showInputDialog(
       "Enter the length, \n for example: 50.15"); 

     double radius = Double.parseDouble(radiusString); 
     double length = Double.parseDouble(lengthString); 

     double area = radius * radius * PI; 
     double volume = area * length; 

     area = (int)area * 100/100.0; 
     volume = (int)volume * 100/100.0; 

     String output = "The area is " + area + "\n The volume is " + volume; 
     JOptionPane.showMessageDialog(null, output); 
    } 
} 

當我存在Eclipse並嘗試再次閱讀它後,它顯示此錯誤消息。

Exception in thread "main" java.lang.NullPointerException 
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:989) 
    at java.lang.Double.parseDouble(Double.java:510) 
    at ComputeLoanUsingInputDialog.main(ComputeLoanUsingInputDialog.java:14) 

更新:我不知道爲什麼,但現在看來我甚至無法運行此程序。通常我按下綠色的播放按鈕,它會運行我的程序。但重啓後,它總是運行其他程序(不是我打算做的)。我認爲我目前的計劃應該是其中一種選擇?

enter image description here

回答

1

一個更好的答案,因爲你是在運行Eclipse,而不是Run As你可能要點擊Debug As正如你在圖片中看到

debug as

然後用step into(F5)和step over(F6)

enter image description here

經過測試,如果您輸入正確的值但是如果取消showInputDialog將返回null你必須檢查它,並檢查只允許數字值。

+0

我不確定我的設置是否搞砸了,但是當我打開調試選項時,我看到「無應用程序」...由於某種原因它現在開始運行一些其他的Java文件。 – George

+0

查看視圖的角度是在調試中,嘗試調試,是遵循執行的很好工具。 – nachokk

+0

@George只需右鍵點擊你有主要方法的類,並把調試作爲..就像我在圖片中顯示 – nachokk

相關問題