BootyBump
BootyBump

Reputation: 59

java.lang.NumberFormatException - Can't find where the error is

I am getting this message:

Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at fdc.programming.VendingMachine.InsertMoney(VendingMachine.java:70)
    at fdc.programming.VendingMachineDriver.main(VendingMachineDriver.java:30)
Java Result: 1

I had been trying to work out, how to do a validate loop so that only positive integers can be accepted and I gave up for now, but I did not change anything and put everything back as it was before messing around. Now when I try to enter a number it gives the above error but there are no errors in Netbeans that I can use to figure out what is wrong! Please be aware that I have only done one basic module in Java for college ;)

My code is:

public class VendingMachine
{
String sinsertMoney, sinsertMoney2; // Money inserted value for parsing into int
String productName;     // Name of product

int insertMoney, insertMoney2;  // Money inserted by customer (int = pence)
int price;                  // Price of products on sale
int changeLeft;         // Change left from inserted money after selection

int again;          // variable for deciding program repeat

DecimalFormat pence = new DecimalFormat("#p");  // Format display output for pence

public void InsertMoney() {
    String soption;             // Variable for machine operation
    productName = " Nothing";
    insertMoney = 0;        // Default inserted money initialised to zero
    insertMoney2 = 0;   // Default additional inserted money initialised to zero
    price = 0;          // Initialising money variables

    // Vending machine welcome dialog

    soption = JOptionPane.showInputDialog(
            "============================================"
            + "\nWelcome to the College Vending Machine!" 
            + "\n============================================"
            + "\n\nOptions: i for insert money, s for select item, q for quit."
            + "\n\n============================================");

    if ("q".equals(soption)) {   // If user chooses q: quit 
        JOptionPane.showMessageDialog(null, "Have a Nice Day!");
        System.exit(0);     // terminate application
    }
    if ("i".equals(soption)) {   // if user chooses i: insert money
        JOptionPane.showInputDialog(
                "=============================" 
                + "\nPlease enter some money (in pence)" 
                + "\n=============================");   // Inserting money
        insertMoney = Integer.parseInt(sinsertMoney);   // Parsing for calculations
    }
    if ("s".equals(soption)) {    // if user chooses s: select item
    }
}

Upvotes: 0

Views: 326

Answers (2)

Sanjeev
Sanjeev

Reputation: 9946

You need to get the entered value in sinsertMoney like:

sinsertMoney = JOptionPane.showInputDialog(
        "=============================" 
        + "\nPlease enter some money (in pence)" 
        + "\n=============================");

And also implement null check on the sinsertMoney for cancel operation and empty strings.

Upvotes: 1

PakkuDon
PakkuDon

Reputation: 1615

I can't see where you've declared sinsertMoney but it looks like you've forgotten to assign the result of your call to JOptionPane.showInputDialog to something, hence why that value is still null when you try to parse it.

Try this:

sinsertMoney = JOptionPane.showInputDialog(
    "=============================" 
    + "\nPlease enter some money (in pence)" 
    + "\n=============================");   // Inserting money
insertMoney = Integer.parseInt(sinsertMoney);

Upvotes: 3

Related Questions