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

Thread: Problem printing a two dimensional boolean array

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem printing a two dimensional boolean array

    Hi, I've only been java'ing for a few months and am struggling with the latest assignment. I'd be really grateful if anyone could help. I need to print a two dimensional boolean array substituting a char for the true or false thereby creating a pattern. For now I just want to print the table but am getting a nullPointerException. Here is my code:
     /**
        * An instance method to print the table with '.' to represent the false and
        * 'O' to represent the true boolean values from the array
        */
       public void display()
       {
     
          for(int j = 0; j < FONT_LETTER_HEIGHT; j++)
          {
             for(int i = 0; i < FONT_LETTER_WIDTH*FONT_LETTER_DISPLAY; i++)
             {
                if(table[j][i])
                {
                   System.out.print('.');
                } 
                else
                { 
                   System.out.print('O');
                }
                System.out.print(table); 
             }
             System.out.println();
           }
          System.out.println();
       }

    I had also created a char that I used as '.' and 'O' in the if statements, but that got the same error! Could anyone please suggest a way that I could get this to work?

    Thank you
    Last edited by sallyB; June 20th, 2011 at 04:07 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem printing a two dimensional boolean array

    but am getting a nullPointerException
    Please copy and paste here the full text of the error message. It shows the line number where the NPE occurred.

    Please put your code in code tags to preserver formatting.
    See: http://www.java-forums.org/misc.php?do=bbcode#code

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem printing a two dimensional boolean array

    Hi - sorry about the formatting error!

    The error code is when I try to run it, it does compile ok. We have a workspace where we can try things out in which I enter this:
    LEDDisplay led = new LEDDisplay();
    led.display();

    then get Exception: line 2. java.lang.NullPointerException

    (LEDDisplay is the constructor for the table) So this looks to me like the display method is where I'm going wrong. Please let me know if I need to clarify anything else (and thanks for your help).

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem printing a two dimensional boolean array

    Please copy and paste here the FULL text of the error message.

    Here is a minimal example:
    Exception in thread "main" java.lang.NullPointerException
    at TestCode3.main(TestCode3.java:26)

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem printing a two dimensional boolean array

    There is nothing else - Exception: line 2. java.lang.NullPointerException
    is all that it says!error.JPG

    Sorry I can't give you more to go on!!!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem printing a two dimensional boolean array

    Sorry, you're in some IDE that is hiding the standard java command error messages.
    The code shown in the image is not compileable code. I have no idea what your IDE is doing with it.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem printing a two dimensional boolean array

    It always helps to post an SSCCE so we can attempt to reproduce the problem. From what you've provided, it seems the display method is throwing the exception...looking at what you've posted it seems the only variable that could not be instantiated is the table array...was this array created?

  8. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Problem printing a two dimensional boolean array

    Quote Originally Posted by sallyB View Post
    There is nothing else - Exception: line 2. java.lang.NullPointerException
    is all that it says!error.JPG

    Sorry I can't give you more to go on!!!
    As Norm suggests, the IDE is confusing matters. In standard Java that sequence isn't possible - an object instantiation that doesn't itself throw an exception always returns an object, so you can't get a NullPointerException on the next line as shown (and if an exception was thrown by the constructor, control would not pass to that second line). The IDE you're using is not behaving like standard Java.

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem printing a two dimensional boolean array

    OK, maybe the stuff I'm working with won't translate to here (or not without putting pages of examples up!!) Is there any way please, that you could show or explain how I could print a two dimensional boolean array using two different 'char' to represent true and false? Maybe then I could convert that into what I am using!

    I really appreciate that this is not your problem but I would be very grateful for any suggestions.

    Thank you

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem printing a two dimensional boolean array

    Here's code for a single dim array.
    for(int i =0; i < bArray.length; i++) {
       System.out.println(bArray[i] ? "T" : "F");  // print: T if true, F if false
    }

  11. #11
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem printing a two dimensional boolean array

    Thank you muchly for this, I'll have a go at converting it and post again.

Similar Threads

  1. Two-dimensional boolean array?
    By tcmei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 08:32 PM
  2. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 07:18 AM
  3. Printing boolean values from an array
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 12:11 AM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  5. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM