Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Error, NullPointerException

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error, NullPointerException

    Sup guys, I have 2 questions.

    1) I am getting a compiler error which states this exactly:
    run:
    Exception in thread "main" java.lang.NullPointerException
    at assignment1.Assignment1.processCheck(Assignment1.j ava:173)
    at assignment1.Assignment1.main(Assignment1.java:71)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 4 seconds)

    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    public class Assignment1 
    {
     
     
     
           public static CheckingAccount info;
     
     
     
         public static void main(String[] args)
        {
           String balance, message;
     
           CheckingAccount info;
     
           double ibalance;
           boolean above = true;
           DecimalFormat formatter = new DecimalFormat("#0.00");
     
     
           balance = JOptionPane.showInputDialog ("Enter your initial balance: ");
            ibalance = Double.parseDouble(balance);
     
            info = new CheckingAccount(ibalance);
     
     
     
     
           if (info.getBalance() < 500)
     
               above = false;
     
            int tcode;   
            tcode = getTransCode();
     
            while(tcode != 0)
            {
     
     
     
            if (tcode == 1 )
            {
                 double tamount;
             tamount = getTransAmt();
     
              info.setBalance(tamount, tcode);
              info.setServiceCharge(tcode);
     
              if(info.getBalance() <500 && above ==true)
              {
                  info.setServiceCharge(1.0);
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
                  above = false;
     
              }
              else if (info.getBalance() <0  && above == false)
              {
                  info.setServiceCharge(2.0);
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
              }
              else if (info.getBalance() <0 && above == true)
              {
                  info.setServiceCharge(2.0);
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
                  above = false;
              }
              else 
              {
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
              }
     
            }
            else if (tcode ==2)
            {
                double tamount;
                tamount = getTransAmt();
                info.setBalance(tamount, tcode);
                info.setServiceCharge(tcode);
                processDeposit (tcode, info.getBalance(), tamount, info.getServiceCharge());
            }
           }while(tcode < 0 || tcode > 2);
     
     
                 message = "End of Transaction" + 
                     "\n" +
                       "\nTotal balance: $" + formatter.format(info.getBalance()) +
                       "\nTotal service charge: $" + formatter.format(info.getServiceCharge()) +
                       "\nFinal Balance: $" + formatter.format(info.EndBalance());
                       JOptionPane.showMessageDialog (null, message);
     
        }
     
     
     
     
            public static int getTransCode()
            {
                int tcode;
                String transcode;
                do
                {
                  transcode = JOptionPane.showInputDialog ("Enter your transaction" + 
                       " code:" + 
                       "\n1 For Check" +
                       "\n2 For Deposit" +
                       "\n3 To Exit Program" );
                    tcode = Integer.parseInt(transcode);
     
                }while (tcode >2 || tcode < 0);
     
                 return tcode;
            }
     
            public static double getTransAmt()
            {
                  double tamount;
                  String transamount;
             transamount = JOptionPane.showInputDialog ("Enter your trans amount:");
             tamount = Double.parseDouble(transamount);
     
             return tamount;
            }
     
            public static void processCheck(int Transcode, double Balance, double TransAmt, double totalServiceCharge, boolean above)
            {
                DecimalFormat formatter = new DecimalFormat("#0.00");
                String message;
                if (Balance >= 500)
                {
                   message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $500 --- charge 5.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null,message);
     
                }
                else if (Balance < 500 && Balance > 0 && above == true)
                {
                     message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $500 --- charge 5.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
                }
                else if (Balance < 500 && Balance > 0 && above == false)
                {
                     message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
                }
                else
                {
                     message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $0 --- charge 10.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
                }
     
     
            }
     
            public static void processDeposit(int Transcode, double Balance, double TransAmt, double totalServiceCharge)
     
            {
                String  message;
                DecimalFormat formatter = new DecimalFormat("#0.00");
     
                message = "Transaction: Deposit in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Deposit --- charge $0.10" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
     
            }
     
     
     
     
     
    }


    What is wrong?


    My second question is regarding the boolean variable, I don't quite how it works. As it states, if (info.getBalance() < 500) above = false; // so this means that above = false; which means that info.getBalance() is lower then 500 right?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by alex067 View Post
    Sup guys, I have 2 questions.

    1) I am getting a compiler error which states this exactly:
    run:
    Exception in thread "main" java.lang.NullPointerException
    at assignment1.Assignment1.processCheck(Assignment1.j ava:173)
    at assignment1.Assignment1.main(Assignment1.java:71)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 4 seconds)
    that's not a compiler error but rather a run-time exception. The most important piece of information in it is the line number. Please indicate to us which line is line 173 of your Assignment1 class.

    My second question is regarding the boolean variable, I don't quite how it works. As it states, if (info.getBalance() < 500) above = false; // so this means that above = false; which means that info.getBalance() is lower then 500 right?
    It means that if info.getBalance() < 500 then above will be assigned the boolean value, false.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error, NullPointerException

    What is wrong?
    java.lang.NullPointerException
    Thrown when an application attempts to use null in a case where an object is required.
    There is a null value where there shouldn't be one. The (approximate) line number is included in the error message.



    I don't quite how it works. As it states, if (info.getBalance() < 500) above = false;
    if(info.getBalance() <500 && above ==true)
    There are three conditions here:
    condition1 info.getBalance() < 500
    condition2 above == true
    condition3 the && operator (totally depends on the evaluation of the other two, but can not be ignored)

    In order for the code of the if block to run, all 3 conditions must be met.
    Condition1 says that info.getBalance() must return a value less than 500
    Condition2 says that the variable above must have the value true
    Condition3 says that condition1 and condition2 must both evaluate to true
    If any of the above are false, the code block is skipped.

    Edit
    After seeing curmudgeon's post and looking at the code again, I see the code in question:
    if (info.getBalance() < 500) above = false;
    ...and yes, if info.getBalance() is less than 500, above will get the value false.
    Last edited by jps; October 12th, 2012 at 01:50 AM.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by jps View Post
    java.lang.NullPointerException
    Thrown when an application attempts to use null in a case where an object is required.
    There is a null value where there shouldn't be one. The (approximate) line number is included in the error message.



    if(info.getBalance() <500 && above ==true)
    There are three conditions here:
    condition1 info.getBalance() < 500
    condition2 above == true
    condition3 the && operator (totally depends on the evaluation of the other two, but can not be ignored)

    In order for the code of the if block to run, all 3 conditions must be met.
    Condition1 says that info.getBalance() must return a value less than 500
    Condition2 says that the variable above must have the value true
    Condition3 says that condition1 and condition2 must both evaluate to true
    If any of the above are false, the code block is skipped.

    Edit
    After seeing curmudgeon's post and looking at the code again, I see the code in question:
    if (info.getBalance() < 500) above = false;
    ...and yes, if info.getBalance() is less than 500, above will get the value false.
    So in order to fix it, I would remove the null in the line the error stated?

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by alex067 View Post
    So in order to fix it, I would remove the null in the line the error stated?
    I wouldn't use the words "remove the null" but, basically yes. Give the variable which is null a valid value.
    use System.out.println(myVariableName); to see which variable on the suggested line number(s) is null.

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by jps View Post
    I wouldn't use the words "remove the null" but, basically yes. Give the variable which is null a valid value.
    use System.out.println(myVariableName); to see which variable on the suggested line number(s) is null.
    Well the runtime errors stats that it's at line 174 and 72.

    line 174 is the last message string in the if else statement in the main method, where
    else
    {
     message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $0 --- charge 10.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
                }

    line 72 is the processCheck method calling:

      else if (info.getBalance() <0  && above == false)
              {
                  info.setServiceCharge(2.0);
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
              }

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error, NullPointerException

    I didn't double check, but I will take your word for it. If you say those are the lines, so be it.
    Did you determine which variable(s) have not been initialized on those lines?
    Go up one line above there and do a println on each variable. One at a time if you like. The println would show output just before the nullPointerException. If you are on the correct line number, one of the variables has to be the cause. There are only so many variables present, it can't take too long to check each one.

  8. #8
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by alex067 View Post
    Well the runtime errors stat[e]s that it's at line 174 and 72.
    So, line 174 as seen by your text editor and us Humans is the last line in that "message =" sequence, right?. That's where it is crashing.

    Well, from the compiler's point of view your lines 168-174 are all line 174. That's all one statement to the compiler.
    [/Begin Edit]
    As was pointed out in a subsequent post, this is not a compiler error, so the error is not generated by the compiler. And, in fact, the run-time Java engine flags the first line in a multi-line statement as containing the problem variable. Sorry
    Anyhow, I still say the printing values of reference variables somewhere before the line that was flagged for the NPE will allow you to see which of the variables caused that particular error.

    [/End Edit]

    If you have looked at that entire statement and can't see how anything can have a 'null' value, here is a way to get to the bottom of things:

    There are two variables among all of that stringy stuff, right? One of them is causing a problem. You can put two print statements before the problem statement to see whether there is a 'null' variable value among them. (Put two separate statements to make really sure you can isolate it.)

    So:
    Put a print statement that shows the value of "formatter" just before the "message =" stuff.
    Put another print statement just before the "message=" statement that shows the value of the reference variable info.
           System.out.println("formatter = " + formatter);
           System.out.println("info      = " + info);

    Then look (really look) to see if you can find how that particular variable can have a 'null' value. Was it declared somewhere but never assigned a value

    Cheers!

    Z
    Last edited by Zaphod_b; October 12th, 2012 at 06:01 PM.

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error, NullPointerException

    Well, from the compiler's point of view your lines 168-174 are all line 174. That's all one statement to the compiler.
    Not in any compiler I have ever used. When an error message refers to a line number, it refers to the line numbers as the lines are seen in the source. If a single statement has (for example) 4 variables, and is broken across 4 different lines, each variable would report it's respective line number, all 4 would not show the same line number.

  10. #10
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by jps
    Not in any compiler I have ever used.
    Actually I misspoke. We are not talking about compiler errors, we are talking about runtime errors, so the compiler is not reporting anything for the particular problem. I regret my carelessness. I edited the previous post consistent with my observations on my system.

    Anyhow...

    When a Java source statement spans over multiple lines, and there is a NPE from anywhere within that statement, my Java reports the text editor's line number of the first line. Java bails out when it encounters the error, so NPEs that might have occurred subsequently can't possibly be reported. (Remember, we are talking about run time errors, not compiler errors.)

    Anyhow, regardless of what the compiler sees at compile time or what Java sees at run time, the technique of printing values of reference variables before the line that was flagged can be used to see exactly which variable had a 'null' value that caused that particular error.

    My Java:
    Java 1.6.0_22


    Cheers!

    Z
    Last edited by Zaphod_b; October 12th, 2012 at 06:03 PM.

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by Zaphod_b View Post
    So, line 174 as seen by your text editor and us Humans is the last line in that "message =" sequence, right?. That's where it is crashing.

    Well, from the compiler's point of view your lines 168-174 are all line 174. That's all one statement to the compiler.
    [/Begin Edit]
    As was pointed out in a subsequent post, this is not a compiler error, so the error is not generated by the compiler. And, in fact, the run-time Java engine flags the first line in a multi-line statement as containing the problem variable. Sorry
    Anyhow, I still say the printing values of reference variables somewhere before the line that was flagged for the NPE will allow you to see which of the variables caused that particular error.

    [/End Edit]

    If you have looked at that entire statement and can't see how anything can have a 'null' value, here is a way to get to the bottom of things:

    There are two variables among all of that stringy stuff, right? One of them is causing a problem. You can put two print statements before the problem statement to see whether there is a 'null' variable value among them. (Put two separate statements to make really sure you can isolate it.)

    So:
    Put a print statement that shows the value of "formatter" just before the "message =" stuff.
    Put another print statement just before the "message=" statement that shows the value of the reference variable info.
           System.out.println("formatter = " + formatter);
           System.out.println("info      = " + info);

    Then look (really look) to see if you can find how that particular variable can have a 'null' value. Was it declared somewhere but never assigned a value

    Cheers!

    Z
    I printed the 2 system.out.println for formatter and info and the runtime error states:


    Exception in thread "main" java.lang.NullPointerException
    at assignment1.Assignment1.processCheck(Assignment1.j ava:175)
    at assignment1.Assignment1.main(Assignment1.java:72)
    Java Result: 1

    Line 175 is the else statement in processCheck

       else
                {
                     message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $0 --- charge 10.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
                }

    Line 72 is the processCheck declaration in the main if else loop

       else if (info.getBalance() <0  && above == false)
              {
                  info.setServiceCharge(2.0);
                  processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
              }

  12. #12
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by alex067 View Post
    I printed the 2 system.out.println for formatter and info and the runtime error states:
    Your code does not show those print statements.

    Quote Originally Posted by Zaphod_b
    So:
    Put a print statement that shows the value of "formatter" just before the "message =" stuff.
    Put another print statement just before the "message=" statement that shows the value of the reference variable info.
    Did you try this, as was previously recommended? Or not?
      else
                {
                    System.out.println("formatter = " + formatter);
                    System.out.println("info      = " + info);
                    message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $0 --- charge 10.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
                }

    Since these two new statements are immediately before your NPE, they should have printed something.

    What did they print?


    Cheers!

    Z
    Last edited by Zaphod_b; October 12th, 2012 at 10:29 PM.

  13. #13
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by Zaphod_b View Post
    Your code does not show those print statements.



    Did you try this, as was previously recommended? Or not?
      else
                {
                    System.out.println("formatter = " + formatter);
                    System.out.println("info      = " + info);
                    message = "Transaction: Check in the amount of $" + TransAmt+
                     "\n" +
                       "\nCurrent balance: $" + formatter.format(info.getBalance()) +
                       "\nService Charge: Check --- charge $0.15" +
                       "\nService Charge: Below $0 --- charge 10.00" +
                       "\nTotal service charge:" + formatter.format(info.getServiceCharge());
                       JOptionPane.showMessageDialog (null, message);
     
                }

    Since these two new statements are immediately before your NPE, they should have printed something.

    What did they print?


    Cheers!

    Z
    I printed those 2 new statements right after the last message but nothing printed out apart from the error I previously posted.

    I put those 2 statements right before the }while message right?

  14. #14
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by alex067 View Post
    I printed those 2 new statements right after the last message but nothing printed out apart from the error I previously posted.
    I put those 2 statements right before the }while message right?
    Quote Originally Posted by Zaphod_b
    Since these two new statements are immediately before your NPE, they should have printed something.
    Well, it couldn't print anything after the NPE had already occurred could it? I mean, the NPE terminated the program.

    1. I told you where I suggest for you to put them

    2. I showed you where I suggested for you to put them, assuming at the "message=..." statement is on the line where the NPE was flagged.

    3. Is there anything you don't understand about my suggestions?


    "Little Bo Peep has lost her sheep
    .
    .
    .
    They will come home, wagging their tails behind them."
    ---Old nursery rhyme.

    "Well did we think they would be wagging their
    tails in front of them?"
    ---Andy Griffith recitation of favorite nursery rhymes
    1952


    Cheers!


    Z
    Last edited by Zaphod_b; October 13th, 2012 at 07:03 PM.

  15. #15
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by Zaphod_b View Post
    Well, it couldn't print anything after the NPE had already occurred could it? I mean, the NPE terminated the program.

    1. I told you where I suggest for you to put them

    2. I showed you where I suggested for you to put them, assuming at the "message=..." statement is on the line where the NPE was flagged.

    3. Is there anything you don't understand about my suggestions?


    "Little Bo Peep has lost her sheep
    .
    .
    .
    They will come home, wagging their tails behind them."
    ---Old nursery rhyme.

    "Well did we think they would be wagging their
    tails in front of them?"
    ---Andy Griffith recitation of favorite nursery rhymes
    1952


    Cheers!


    Z
    No need to act cocky, I've done exactly what you've said and it didn't print anything out.

    You told me to put those 2 statements before message =

    }while(tcode < 0 || tcode > 2);
     
                    System.out.println("formatter = " + formatter);
           System.out.println("info      = " + info);
                 message = "End of Transaction" + 
                     "\n" +
                       "\nTotal balance: $" + formatter.format(info.getBalance()) +
                       "\nTotal service charge: $" + formatter.format(info.getServiceCharge()) +
                       "\nFinal Balance: $" + formatter.format(info.EndBalance());
                       JOptionPane.showMessageDialog (null, message);
     
        }

    that is exactly what I did.

    I found out one of the issue was that I re-declared CheckingAccount variable when I already declared it as a public variable, so I removed it in the main method (the CheckingAccount info


    However, I am getting a new set of run-time errors and my program compiles, however the calculations are not correct.

    Error:

    Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1008)
    at java.lang.Double.parseDouble(Double.java:540)
    at assignment1.Assignment1.getTransAmt(Assignment1.ja va:133)
    at assignment1.Assignment1.main(Assignment1.java:57)
    Java Result: 1

    The lines the error is referring to is the tamount variable @:

     if (tcode == 1 )
            {
                 double tamount;
             tamount = getTransAmt();
     
              info.setBalance(tamount, tcode);
              info.setServiceCharge(tcode);

    And the declaration of the tamount variable @ the getTransAmt method:

      public static double getTransAmt()
            {
                  double tamount;
                  String transamount;
             transamount = JOptionPane.showInputDialog ("Enter your trans amount:");
             tamount = Double.parseDouble(transamount);
     
             return tamount;
            }

    The line where it states: tamount = Double.parseDouble(transamount);

  16. #16
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Error, NullPointerException

    Quote Originally Posted by alex067 View Post
    No need to act cocky
    I apologize for my use of sarcasm. I can see that it didn't add anything useful to the conversation. I should have known better. I will try to do better.

    I can also see that asking you what you didn't understand was an extremely stupid thing to say. I mean that was a stupid question on my part. Sorry.

    The intent of my suggestions was the following: If you are getting a NPE as a result of a particular statement in your program, then, first of all look, at the code on the line was flagged. If the statement extends over multiple lines, then look at the entire statement.

    Then, if you can't see how anything in that entire statement can be causing the specific error that was reported, you can put print statements immediately before that statement to try to let the program itself show you which variable had a null value at the time the NPE occurred.

    Putting print statements in other places in the code might not help much.

    In particular...

    Putting print statements in parts of the code that were not executed during a run that results in an NPE definitely will not help find the cause of that NPE for that run.

    Putting print statements after the statement that cause the NPE for that run definitely will not help find that cause of the NPE for that run.

    Of course, the entire point is that just finding which variable had a null value is not the object of the exercise, but it might make you go back and look at the code again with a specific focus of finding where that variable was declared and where you think it was given a value that would make it not be null. Apparently you were able to find out what caused the NPE that you previously reported.

    OK. If we can get past my previous bad behavior, let's get on to your next problem:

    Quote Originally Posted by alex

    Error:

    Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1008)
    at java.lang.Double.parseDouble(Double.java:540)
    at assignment1.Assignment1.getTransAmt(Assignment1.ja va:133)
    at assignment1.Assignment1.main(Assignment1.java:57)
    Java Result: 1
    .
    .
    .
    The line where it states: tamount = Double.parseDouble(transamount);
    OK, let's go through it again.

    First of all, you didn't tell us what you did to make the program give the NPE.

    I will make an assumption here. You fixed the problem about the NPE due to info not being initialized, otherwise you wouldn't have gotten this far.

    Now, I think that you did the following sequence:

    Executed the program.
    The program asked for an initial balance. You entered a number and clicked 'OK'. If you had done something invalid, there would have been an error at that point.

    It asked for a transaction code. You entered 1 and clicked 'OK'. That's the only way that I can see that you got to the point in your program where Something Bad happened

    Now, we get to the point where the NPE occurred.

    The dialog box asks you to "Enter a transaction amount"

    It waits until you close the dialog box. I can think of four things that you could do here to close the dialog box. It might be worthwhile to investigate:

    What happens if you enter a number in the dialog box and click "OK"?

    What happens if you enter something other than a number and click "OK"?

    What happens if you enter nothing in the window and click "OK"?

    What happens if you close the dialog box by clicking "Cancel" or by doing anything other than clicking "OK"?

    Did any of things make an NPE occur at this point in the program?

    Here is my suggestion:

    Go back into your program where this NPE occurs and put a print statement after the call to the showInputDialog() method and before you try to parse the String to obtain a number:
            public static double getTransAmt()
            {
                double tamount;
                String transamount;
                transamount = JOptionPane.showInputDialog ("Enter your trans amount:");
     
                // New statement for debugging: What happens when you click "Cancel"
                // or close the dialog box by any method other than clicking "OK"
                System.out.println("transamount = " + transamount);
     
                tamount = Double.parseDouble(transamount);
     
                return tamount;
            }

    Now, if you don't get this far in the program, it won't print anything out, right? So, follow whatever sequence you executed to get the message that you reported and see what happens here.


    Cheers!

    Z
    Last edited by Zaphod_b; October 14th, 2012 at 09:13 AM.

Similar Threads

  1. NullPointerException Error in Code that Prints Array Elements
    By Atypically Engineered in forum Collections and Generics
    Replies: 8
    Last Post: April 6th, 2012, 10:21 PM
  2. NullPointerException Error
    By nicsa in forum Exceptions
    Replies: 6
    Last Post: November 19th, 2011, 05:32 PM
  3. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 09:34 AM
  4. NullPointerException error
    By pyrotecc25 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2011, 01:17 PM
  5. NullPointerException error
    By blazerix in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 24th, 2011, 09:59 PM