Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

How do I put a NullPointerException catch/try in this program?

  Asked By: Jose    Date: Dec 11    Category: Java    Views: 717
  

Everyting in this program works except one part. I have to
put a NullPointException in this program to catch if someone pushes
the "Cancel Button" on the second dialog box. This may seem like an
easy task for most, this is my first time. This is all I have left
to do, and I can't figure it out. I know someone out there could
tell me how to do this, and I would appreciate it.
Dave
Here is my Program, what Am I doing wrong?



import javax.swing.JOptionPane;

import java.text.*;//import javax.swing.*;

import java.lang.*;

public class NewEnhancedInvoiceApp{

public static void main(String[] args){ // start of main method

double discountAmount = 0;

//double invoiceTotal;


String choice = "";

while (!(choice.equalsIgnoreCase("n"))) {

String inputString = JOptionPane.showInputDialog(

"Enter order total: ");

double orderTotal = parseTotal (inputString);

// code that calculates and displays new total

if (orderTotal >= 500 )

discountAmount = orderTotal * .2;

else if (orderTotal >= 250 && orderTotal < 500)

discountAmount = orderTotal * .15;

else if (orderTotal >= 100 && orderTotal <
250)

discountAmount = orderTotal * .1;

else if (orderTotal < 100)

discountAmount = 0.0;

double invoiceTotal = orderTotal - discountAmount;


//Format Order Total, Discount Amount and Invoice Total
//to display numeric expression as currency,

NumberFormat currency = NumberFormat.getCurrencyInstance();

String message = "Order Total: " + currency.format(orderTotal) +"\n"
+"Discount Amount: " +
currency.format(discountAmount)+ "\n"
+"Invoice Total: " + currency.format
(invoiceTotal) + "\n\n"
+ "To Continure, Press O.K. \n"
+ "Continure Y / N ?";



choice = JOptionPane.showInputDialog(null, message, "Invoice
Application", JOptionPane.PLAIN_MESSAGE);

if (choice == "y") break;

} // end while loop

System.exit(0);

}

private static double parseTotal(String totalString){

double orderTotal = 0;

boolean tryAgain = true;

while (tryAgain) {

try{

orderTotal = Double.parseDouble(totalString);

while (orderTotal <= 0) {

totalString =
JOptionPane.showInputDialog(
"Invalid order total. \n"
+"Please enter a positive
number: ");
orderTotal =
Double.parseDouble(totalString);

}

tryAgain = false;

}

catch(NumberFormatException e) {
totalString = JOptionPane.showInputDialog(
"Invalid order total. \n"
+"Please enter a number: ");

//try{
//}


/*catch(NullPointerException rex) {
System.out.println ("Please Enter Data");
totalString = JOptionPane.showInputDialog(null,
"Invalid order total. \n"
+ "Please enter a number: ",
"Invoice", JOptionPane.ERROR_MESSAGE);

*/
}
}
return orderTotal;
}




}

//}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Amir Shaikh     Answered On: Dec 11

Basically, your catch/try clause is in the right
format. But try to capture the nullPointerException
because nullPointerException covers quite a few
situations including multithreading. Is your program
is that solid? I only give it a quick glance.

 
Answer #2    Answered By: Abriana Rossi     Answered On: Dec 11

The program  is solid, with the exception of the
NullPointerException....I just can't figure  out what to do to make
the program throw out an exception if the user pushes cancel on the
second input dialog  box. I am also suppose to display a message
through a dialog box  when this happens. I am sure it is something
minor I am not doing, but I just can't figure it out. It is the
first program I have written, so I am happy to be this far. I just
have one problem, I can't seem to work out. I have spent hours on
this.