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

Thread: nullPointerException, but doesn't look wrong to me

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default nullPointerException, but doesn't look wrong to me

    Hi, little code problem

        public void display() {
            for (int i = 0; i < 25; i++) {
                StringBuilder line = null;
                for (int j = 0; j < 25; j++) {
                    line.append(puzzleArray[i][j]); //this is line 20
                }
                System.out.println(line.toString());     //this line seems linked to the error.
            }
        }
    The problem I got was:
    #
    Exception in thread "main" java.lang.NullPointerException
    at prog4.Puzzle.display(Puzzle.java:20)
    at prog4.Prog4.main(Prog4.java:8)


    meanwhile when I tried the following (not what I wanted but similar there wasn't a problem)
     
        public void display() {
            for (int i = 0; i < 25; i++) {
                for (int j = 0; j < 25; j++) {
                    System.out.println(puzzleArray[i][j]);
                } 
            }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: nullPointerException, but doesn't look wrong to me

    What variable on line 20 has a null value? Find the variable and backtrack in the code to see why the variable does not have a valid value.

    Where do you assign a non-null value to the line variable after you give it a null value?

  3. The Following User Says Thank You to Norm For This Useful Post:

    Goldfinch (February 19th, 2012)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: nullPointerException, but doesn't look wrong to me

    StringBuilder line = null;
    for (int j = 0; j < 25; j++) {
        line.append(puzzleArray[i][j]); //this is line 20

    You set the string builder line to null, and then, a couple of lines later in the loop you try and call its append() method. You will always get a NullPointerException when you call a method on something that is null. (That's basically what the exception means.)

    Try initialising line to a non null value.

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

    Goldfinch (February 19th, 2012)

Similar Threads

  1. Replies: 3
    Last Post: October 19th, 2011, 11:55 PM
  2. [SOLVED] NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 21st, 2011, 11:35 AM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM