ismail_1
ismail_1

Reputation: 149

java program compiles with no error but does not run

Here is my code after changes suggested by stackoverflow members. The code compiles with no errors but when I run "java Realtor11", I get the following errors. 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.

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)

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

}

}

Upvotes: 0

Views: 4742

Answers (4)

Logan Murphy
Logan Murphy

Reputation: 6230

There is a difference between compile errors and runtime errors (which this is).

try this instead of the while loop

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

Upvotes: 0

Axel Amthor
Axel Amthor

Reputation: 11096

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

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691755

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. And then you're trying to read a line again (which of course continues to return null), and parse this null to a 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.

I think it's time for you to read the javadoc of the methods you're using, and the Java IO tutorial.

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.

Upvotes: 1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

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.

Upvotes: 3

Related Questions