简体   繁体   English

Java程序编译没有错误,但是没有运行

[英]java program compiles with no error but does not run

Here is my code after changes suggested by stackoverflow members. 这是我的代码,经过stackoverflow成员建议的更改。 The code compiles with no errors but when I run "java Realtor11", I get the following errors. 代码编译没有错误,但是当我运行“ java Realtor11”时,出现以下错误。 I am importing the correct libraries so not sure what the issue is. 我正在导入正确的库,所以不确定是什么问题。

The goal of the program is to read two values (first line is a string (John) and the second line is a double (100)) and output them with some calculations to a JOption message. 该程序的目标是读取两个值(第一行是一个字符串(John),第二行是一个double(100)),并通过一些计算将它们输出到JOption消息。

Exception in thread "main" java.lang.NullPointerException at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at Realtor11.main(Realtor11.java:49) sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)处的线程“ main”中的java.lang.NullPointerException异常,Realtor11.main(Realtor11.java:49)处的java.lang.Double.parseDouble(Unknown Source)处

// 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. 

}

} }

The problem is this line. 问题是这条线。

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

This is kind of a "degenerate loop" because of the semicolon at the end. 由于末尾使用分号,因此这是一种“简并循环”。 It continues to read from the file until there's nothing left to be read. 它会继续从文件中读取,直到没有任何内容可读取为止。 After that, there are no double values to parse. 此后,就没有要解析的double值。

These three lines cause the error: 这三行会导致错误:

    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.

That should probably be: 那应该是:

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

This can be improved: 可以改进:

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

Look at this code: 看下面的代码:

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

The first line reads everything from the reader, without doing anything with what it reads. 第一行从阅读器读取所有内容,而不会对其读取的内容进行任何处理。 Then, when everything has been read, you're trying to read a line again. 然后,当所有内容都已读取后,您将尝试再次读取一行。 That will obviously return null, since you've already read everything. 显然,这将返回null,因为您已经阅读了所有内容。 And then you're trying to read a line again (which of course continues to return null), and parse this null to a double. 然后,您尝试再次读取一行(该行当然继续返回null),并将此null解析为double。

And at the end, you close the reader, but since you don't do it in a finally block, the NPE that is thrown before prevents it to be closed. 最后,您关闭了阅读器,但是由于您没有在finally块中进行操作,因此,抛出的NPE会阻止它被关闭。

I think it's time for you to read the javadoc of the methods you're using, and the Java IO tutorial. 我认为是时候阅读您正在使用的方法的Javadoc和Java IO教程了。

Also, avoid this like the plague: 另外,避免像瘟疫这样的情况:

catch (IOException e) {}

You're simply hiding the exception from yourself, making any diagnostic impossible if an IOException occurs. 您只是在对自己隐藏异常,如果发生IOException,就不可能进行任何诊断。

There is a difference between compile errors and runtime errors (which this is). 编译错误和运行时错误之间是有区别的。

try this instead of the while loop 试试这个而不是while循环

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM