Results 1 to 3 of 3

Thread: Error

  1. #1
    hawk-dc is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Error

    Can anyone tell me why it's showing the following error message ??
    //
    Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Converter.getInput(Converter.java:27)
    at Converter.main(Converter.java:138)


    The program itself
    /*
    import java.io.*;
    import java.util.Scanner;
    public class Converter {

    String strDate;
    int slash = 0;
    String mmonth;
    String ddate;
    String alMonth;
    int nuMonth = 0;
    int nuDate = 0;
    int days = 0;



    public void getInput()
    {
    /*try
    {*/
    System.out.println("Enter the date in the format MM/DD");
    Scanner kbd = new Scanner (System.in);
    strDate = kbd.nextLine();
    slash = strDate.indexOf('/');
    if (slash != 2) //throw new InValidInputException();
    mmonth = strDate.substring (0, slash);
    ddate = strDate.substring(3);
    nuMonth = Integer.parseInt(mmonth);
    nuDate = Integer.parseInt(ddate);
    /*} catch (InValidInputException e)
    {
    System.out.println(e.getMessage());
    System.exit(0);
    }*/

    }
    public void doConversion()
    {
    try
    {
    switch(nuMonth)
    {
    case 1:
    if (nuMonth == 1)
    alMonth = "January";
    if(nuDate > 31)
    throw new DayException();
    break;


    case 2:
    if (nuMonth == 2)
    alMonth = "February";
    if(nuDate >28)
    throw new DayException();
    break;
    case 3:
    if (nuMonth == 3)
    alMonth = "March";
    if (nuDate > 31)
    throw new DayException();
    case 4:
    if (nuMonth == 4)
    alMonth = "April";
    if (nuDate > 30)
    throw new DayException();
    case 5:
    if (nuMonth == 5)
    alMonth = "May";
    if (nuDate > 31)
    throw new DayException();
    case 6:
    if (nuMonth == 6)
    alMonth = "June";
    if (nuDate > 30)
    throw new DayException();
    case 7:
    if (nuMonth == 7)
    alMonth = "July";
    if (nuDate > 31)
    throw new DayException();
    case 8:
    if (nuMonth == 8)
    alMonth = "August";
    if (nuDate > 30)
    throw new DayException();
    case 9:
    if (nuMonth == 9)
    alMonth = "September";
    if (nuDate > 31)
    throw new DayException();
    case 10:
    if (nuMonth == 10)
    alMonth = "October";
    if (nuDate > 30)
    throw new DayException();
    case 11:
    if (nuMonth == 11)
    alMonth = "November";
    if (nuDate > 31)
    throw new DayException();
    case 12:
    if (nuMonth == 12)
    alMonth = "December";
    if (nuDate > 30)
    throw new DayException();
    default:
    break;
    }

    }
    catch (DayException e)
    {
    System.out.println(e.getMessage());
    }

    }
    public void MonthValid()
    {
    try
    {
    if((nuMonth < 0) || (nuMonth > 12))
    throw new MonthException();
    }


    catch (MonthException e)
    {
    System.out.println(e.getMessage());
    }
    }
    public void ShowOutput()
    {
    System.out.println("This is the same as " + alMonth + " " + ddate);
    }
    public static void main(String[] args) {

    Converter object = new Converter();
    object.getInput();
    object.doConversion();
    object.MonthValid();
    object.ShowOutput();
    }
    }
    */

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Error

    Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Converter.getInput(Converter.java:27)
    It looks like the call to parseInt() on line 27 passed an invalid value to the method: null.
    Check the code to see why it did not pass a valid String of digits to parseInt().

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Error

    Without the formating it is quite hard to read.

    Maybe you want to check out a source versioning system like git, svn, tfs ...
    If you get to know such a tool you could simply get versions of your code which means that you do not have to keep uncommented code in your code. That often makes it hard to read and it is simply an easy way to run into errors.

    Another hint: Use an integrated development environment that can indent your code. Just make it reformat your code so you can see the logic inside.

    And last but not least the reason for these hints:
    if (slash != 2) //throw new InValidInputException();
    mmonth = strDate.substring (0, slash);
    You commented out the throw command so your if statement is just:
    if (slash != 2) mmonth = strDate.substring (0, slash);

    So if the month is 2 characters then the mmonth is not set. So my guess is, that you try to parse mmonth which is still null (because of this if!).

    With kind regards,

    Konrad

Similar Threads

  1. Replies: 8
    Last Post: 11-22-2012, 08:16 AM
  2. Replies: 3
    Last Post: 05-08-2012, 06:26 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 3
    Last Post: 01-23-2009, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •