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 12 of 12

Thread: Help with code.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with code.

    import java.util.Scanner;
     
    public class Calc {
    	public static void main(String pizdets[]) {
     
    		double add[] = {0}, total = 0;
    		int blat, i = 0;
     
    		for (blat = 1; blat != 0; i++) {
    			Scanner addend = new Scanner(System.in);
    			add[i] = addend.nextDouble();
    			blat = addend.nextInt();
    		}
    		for (i = 0; i < add.length; i++) {
    			total += add[i];
    		}
    		System.out.print(total);
    	}
    }

    What happens is I get an error message stating
    "Exception in thread "main" java.lang.NullPointerException
    at Calc.main(Calc.java:11)"
    Last edited by pbrockway2; April 29th, 2013 at 07:16 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    972
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Help with code.

    Hi DanyaMartchenkov, welcome to the forums!

    I have added "code" tags to your post. Basically you put [code] at the start of a section of code and [/code] at the end: that way the code will be properly formatted by the forum software.

    Which is line 11?

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with code.

    Thanks, line 11 is 'add[i] = addend.nextDouble();'

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    972
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Help with code.

    OK. And can you confirm the exact runtime message? Copy and post it.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with code.

    Exception in thread "main" java.lang.NullPointerException
    at Calc.main(Calc.java:11)

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    972
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Help with code.

    That's truly weird! When I run your code I get

    3
    5
    6
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at Calc.main(Calc.java:11)

    where 3,5,6 were some random input I typed.

    To be honest, that's the error I would expect. But let's pursue your NullPointerException...

    The error means that somewhere on line 11 you are using a variable as if it had a non null value when it is really null. There are only two candidates: add and addend. (That's because you can't get elements from a null array, or call methods of a null thing.) You can see which of these variables is the problem by printing their values just before line 11.

    for (blat = 1; blat != 0; i++) {
        Scanner addend = new Scanner(System.in);
        System.out.println("About to assign the next double to add[i]");
        System.out.println("add=" + add);
        System.out.println("addend=" + addend);
        add[i] = addend.nextDouble();
        blat = addend.nextInt();
    }

    What is the output with that change? (including your input, if any).

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with code.

    About to assign the next double to add[i]
    add=null
    addend=java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]


    Exception in thread "main" java.lang.NullPointerException
    at Calc.main(Calc.java:14)

    Line 14 is:
    add[i] = addend.nextDouble();


    --- Update ---

    changing
    double add[] = {0}
    to
    double[] add = new double[100];
    seemed to fix the error but it still doesn't compile like expected.

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    972
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Help with code.

    The output is indicating that the add array is null. However we can see you assign to it right at the start of main().

    I'm bewildered. I was going to suggest that you recompile to make sure you are using exactly the code you posted (ie delete Calc.class and recompile), but the fact that you see the output saying "add=null" means that the program is uptodate. Do double check that your code is exactly what you posted. Consider doing what I did and copying the code from your post and replacing your existing code with that.

    Otherwise I am baffled - maybe someone else has a clever idea.

  9. The Following User Says Thank You to pbrockway2 For This Useful Post:

    DanyaMartchenkov (April 29th, 2013)

  10. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with code.

    Haha, well, thanks anyways!

  11. #10
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    972
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Help with code.

    Our posts crossed.

    If there's a compiler message, post it and the offending code.

  12. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with code.

    I changed it up a bit:
    import java.util.Scanner;
     
    public class Calc {
    	public static void main(String pizdets[]) {
     
    		double[] add = new double[100];
    		double total = 0;
    		int i = 0;
    		String svalota = "";
     
    		for (i = 0; svalota.equals("=") == false; i++) {
    			Scanner addend = new Scanner(System.in);
    			add[i] = addend.nextDouble();
    			svalota = addend.nextLine();
    		}
    		for (i = 0; i < add.length; i++) {
    			total += add[i];
    		}
    		System.out.print(total);
    	}
    }

    Now, after sending "=" I get this error message:
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at java.util.Scanner.nextDouble(Unknown Source)
    	at Calc.main(Calc.java:13)


    --- Update ---

    Line 13 being
    add[i] = addend.nextDouble();

  13. #12
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    972
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Help with code.

    InputMismatchException means that the stream is looking at the wrong kind of input. It is a runtime error (not a compiler message) and it is occurring when you call nextDouble().

    Basically you enter "=", the runtime tries to read that as a double, and you get the exception.

    You may have noticed that your code *never* stops and waits for you to enter a value for svalota. This becomes even clearer if you print some prompts:

    for (i = 0; svalota.equals("=") == false; i++) {
        Scanner addend = new Scanner(System.in);
        System.out.print("Enter a double: ");
        add[i] = addend.nextDouble();
        System.out.print("Enter svalota");
        svalota = addend.nextLine();
    }

    And the reason for that is that nextDouble() returns a double but does not move to the next line. So nextLine() will always return straight away and assign an empty string to svalota. It will do this every time. The answer is to read a new line after nextDouble(). You don't do anything with what is read (it is an empty string) but it "positions" the stream properly:

    import java.util.Scanner;
     
    public class Calc {
        public static void main(String pizdets[]) {
     
            double[] add = new double[100];
            double total = 0;
            int i = 0;
            String svalota = "";
     
            for (i = 0; svalota.equals("=") == false; i++) {
                Scanner addend = new Scanner(System.in);
                add[i] = addend.nextDouble(); // read the double
                addend.nextLine();  // throw away the newline
                svalota = addend.nextLine();  // read = to stop, or something else to continue
            }
            for (i = 0; i < add.length; i++) {
                total += add[i];
            }
            System.out.print(total);
        }
    }

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 08:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 12:36 PM