0

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. 

}

}

13
  • What's at the line the error points to? Commented Jan 28, 2014 at 21:37
  • 1
    Time to do a little debugging. Consider using a debugger or sprinkle your code with lots of printlns. Commented Jan 28, 2014 at 21:37
  • Just FYI - I explicitly declare variables 'price' and 'seller' since I was getting compile errors. But I assume that should not matter since the variables would bu updated based on the contents of the text file. Commented Jan 28, 2014 at 21:38
  • A NullPointerException is thrown when an application attempts to use null in a case where an object is required: docs.oracle.com/javase/7/docs/api/java/lang/… As @HovercraftFullOfEels suggested you should debug your application. Commented Jan 28, 2014 at 21:39
  • 1
    isn't the error self-explaining? you know the error, you know the line number and you can check what causes this type of error. try debugging. Commented Jan 28, 2014 at 21:39

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

True and I fixed this. Still the same error. I am working through the other comments. I am new to this so will keep researching.
Good eye David. I wouldn't have recognised that.
OK, how did you fix it? What is your call to Double.parseDouble seeing now? It's a bit hard to debug partially-fixed code, without seeing what the fix is.
Guess you just deleted the ; which means he reads "sellers" until the EOF and the Double.parseDouble still gets a null because of EOF already reached ...
Yes, that seems quite likely.
|
2

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

1 Comment

Thank you Axel for EOF statement. Now I understand why the values would not process after the first line.
1

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.

1 Comment

I will be honest. I did not even research on the "exception" part. I was concentrating on the upper portion of grabbing the values from the ext file. In the future, I will keep this in mind.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.