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:
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
Re: Problem printing a two dimensional boolean array
Quote:
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
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:
Code :
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).
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:
Quote:
Exception in thread "main" java.lang.NullPointerException
at TestCode3.main(TestCode3.java:26)
1 Attachment(s)
Re: Problem printing a two dimensional boolean array
There is nothing else - Exception: line 2. java.lang.NullPointerException
is all that it says!Attachment 626
Sorry I can't give you more to go on!!!
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.
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?
Re: Problem printing a two dimensional boolean array
Quote:
Originally Posted by
sallyB
There is nothing else - Exception: line 2. java.lang.NullPointerException
is all that it says!
Attachment 626
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.
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
Re: Problem printing a two dimensional boolean array
Here's code for a single dim array.
Code :
for(int i =0; i < bArray.length; i++) {
System.out.println(bArray[i] ? "T" : "F"); // print: T if true, F if false
}
Re: Problem printing a two dimensional boolean array
Thank you muchly for this, I'll have a go at converting it and post again.