java程序编译没有错误但不运行

时间:2014-01-28 21:36:13

标签: java

这是我在stackoverflow成员建议的更改后的代码。代码编译没有错误但是当我运行“java Realtor11”时,我得到以下错误。我正在导入正确的库,因此不确定问题是什么。

程序的目标是读取两个值(第一行是字符串(John),第二行是double(100))并将它们输出一些计算到JOption消息。

  

线程“main”java.lang.NullPointerException中的异常           at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)           在java.lang.Double.parseDouble(未知来源)           在Realtor11.main(Realtor11.java:49)

// java class for keyboard I/O
import java.util.Scanner;
// java class for JOption GUI
import javax.swing.JOptionPane;
// File reader
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

public class Realtor11
{
    public static void main(String[] args)
    {
    // Keyboard and file input
        Scanner console = new Scanner(System.in); 
        Scanner inputStream = null;
    // price of the house, the cost to sell the house and the commission
        double price = 0;
        double cost, commission;
    // seller's name
        String seller = "name";



// GUI diplay message declaration
    String display_message = "This program calculates the cost to sell a home\n" 
    + "and the commission paid to an individual sales agent.\n\n"
    + "The user is asked for the last name of the seller and the\n"
    + "sales price.\n\n";

// Output descriptive messages
    JOptionPane.showMessageDialog(null, display_message, "Lab 1 Description", JOptionPane.INFORMATION_MESSAGE);

// Read Realtor11.txt
    try {
        BufferedReader in = new BufferedReader(new FileReader("Realtor11.txt"));
        while (in.read()!= -1);
        seller = in.readLine();
        price = Double.parseDouble(in.readLine());
        in.close();
        } 

        catch (IOException e) {}


// calculate the cost and the commission
    cost = 0.06 * price;
    commission = 0.015 * price;
// display the input and results
    String 
        out1 = String.format("%nThe " + seller + "'s" + " home sold for $%.2f%n", price),
        out2 = String.format("The cost to sell the home was $%.2f%n", cost),
        out3 = String.format("The selling or listing agent earned $%.2f%n", commission);

    JOptionPane.showMessageDialog(null, out1 + out2 + out3, seller + "'s Home Sale", JOptionPane.INFORMATION_MESSAGE);

// Output to file
// still writing this. 

}

}

4 个答案:

答案 0 :(得分:3)

问题在于这一行。

while (in.read()!= -1);

这是一种“退化循环”,因为最后是分号。它继续从文件中读取,直到没有什么东西可以读取。之后,没有要解析的double值。

答案 1 :(得分:2)

这三行会导致错误:

    while (in.read()!= -1);           // this reads until EOF
    seller = in.readLine();           // this gets a null-pointer since we're behind EOF
    price = Double.parseDouble(in.readLine()); // same as above, This is the printed error.

应该是:

    seller = in.readLine();  // first line: seller
    price = Double.parseDouble(in.readLine()); // second line: price

这可以改进:

    seller = in.readLine();  // first line: seller
    String sPrice = in.readLine(); // second line: price
    if ( sPrice != null )
        price = Double.parseDouble(sPrice); 

答案 2 :(得分:1)

看看这段代码:

while (in.read()!= -1);
seller = in.readLine();
price = Double.parseDouble(in.readLine());
in.close();

第一行从读者那里读取所有内容,而不对其读取内容做任何事情。然后,当所有内容都被读取后,您将再次尝试读取一行。那显然会返回null,因为你已经阅读了所有内容。然后你试图再次读取一行(当然继续返回null),并将此null解析为double。

最后,你关闭阅读器,但由于你没有在finally块中执行,因此之前抛出的NPE会阻止它被关闭。

我认为是时候阅读您正在使用的方法的javadoc以及Java IO教程。

另外,像瘟疫一样避免这种情况:

catch (IOException e) {}

您只是隐藏自己的异常,如果发生IOException,则无法进行任何诊断。

答案 3 :(得分:0)

编译错误和运行时错误之间存在差异(这是)。

试试这个而不是while循环

String line = in.readLine();
if(line != null) {
    seller = line;
}
line = in.readLine();
if(line != null) {
    price = Double.parseDouble(line);
}
in.close();