-
Implementing a two-dimensional boolean array
Hello,
First of all I hope this is the right place! I am a bit of a newbie at Java and I am having a mental breakdown in my assignment. Hopefully a kind member can pin-point me in the right direction. To cut a long story short I require to create a 2D boolean array of height 5 and width 6, with a total number of letters displayed as 10. I have a utility class in which it encodes alphabetic characters and the space character into 2d boolean values. As a display, it will print "." for false values, and "O" for true values, creating a text representation.
My problem is that despite compiling the code with no errors, I receive a "Exception: line 2. java.lang.NullPointerException" error when I execute the commands
Code :
LEDDisplay led = new LEDDisplay();
led.display();
Here is my coding so far, please excuse the excess template code that I haven't deleted and edited after creating a new class
Code :
/**
* Class LEDDisplay - write a description of the class here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class LEDDisplay
{
/* instance variables */
boolean[][] matrix;
/* Private class constants */
public static int FONT_LETTER_HEIGHT = 5;
public static int FONT_LETTER_WIDTH = 6;
public static int LETTERS_PER_DISPLAY = 10;
/**
* Default constructor for objects of class LEDDisplay
*/
public LEDDisplay()
{
super();
boolean[][] matrix = new boolean[FONT_LETTER_HEIGHT][(FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY)];
}
/* instance methods */
/**
* 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 * LETTERS_PER_DISPLAY); i++)
{
if (matrix[j][i])
{
System.out.print('.');
}
else
{
System.out.print('O');
}
System.out.print(matrix);
}
System.out.println();
}
System.out.println();
}
}
Many thanks for your time
-
Re: Implementing a two-dimensional boolean array
One problem you have is that you have two variables named matrix at different scopes.
The variable at the inner scope hides the outer one,so the outer one does not get set to a value.
When you post error messages please copy and paste the full text of the message. You have left out valuable information. The error message you posted does NOT make sense. You must have edited it.
-
Re: Implementing a two-dimensional boolean array
I am a little confused - I don't see how the matrix overrides the other? I must be being blind.
That is the only error message that is displayed. The program that is given (BlueJ) is a simple teaching/learning IDE program for object orientated programming. I would appreciate it if you didn't accuse me straight off for editing the error code given when I have pasted it straight off the program display box.
-
Re: Implementing a two-dimensional boolean array
Quote:
I have pasted it straight off the program display box.
Then your IDE does not work with a valid java program.
It is very,very unusual for an error to occur on line 2 of a java program.
Usually the first line is the class statement (and that excludes the import statements).
the second line would define a method
the third line and fourth lines would be the code you posted.
The error you posted minimally would be on line 4 of the source not on line 2.
Quote:
I don't see how the matrix overrides the other? I must be being blind.
Code :
public class LEDDisplay
{
/* instance variables */
boolean[][] matrix; //<<<<<<<<<<<<<<HERE is the outer definition
/* Private class constants */
public static int FONT_LETTER_HEIGHT = 5;
public static int FONT_LETTER_WIDTH = 6;
public static int LETTERS_PER_DISPLAY = 10;
/**
* Default constructor for objects of class LEDDisplay
*/
public LEDDisplay()
{
super();
boolean[][] matrix = <<<<<<<<<< Here is a local definition
-
Re: Implementing a two-dimensional boolean array
As far as I know this is only for learning purposes and to test small bits of code. It only goes up to a certain point and it's not going to be implemented into a larger model for any commercial use, maybe that's why it isn't giving full error codes to properly pin-point what has gone wrong.
As for the problem, I still can't figure it out :confused:
-
Re: Implementing a two-dimensional boolean array
I suspect the error is occuring on this line:
because the variable matrix has not been set to any value because of the problem I showed in post #4
Your IDE doesn't seem to show you where in the display method the exception occurs.
For a training aid, it lacks a lot.
If you want to write code with more than a dozen lines, I'd recommend you find another IDE to use.
-
Re: Implementing a two-dimensional boolean array
It's an assignment and it's the supplied program so I should be able to figure it out... with lots and lots of time! I don't want to have to resort to trial and error but I guess I might have to, and then work backwards.
-
Re: Implementing a two-dimensional boolean array
You don't have to resort to trial and error if you understand what you are doing.
Code java:
public class LEDDisplay {
boolean[][] matrix;
public LEDDisplay() {
boolean[][] matrix = new boolean....;
I have removed all the other code so you can concentrate on the important bits. See how you have TWO variables called matrix. One is declared and initialised inside the constructor. Due to scoping rules you cannot access it outside the constructor. The other one is an instance variable and is never initialised and therefore is null. The instance variable is the one you can access in the display method but since it is null you get a NullPointerException.
-
Re: Implementing a two-dimensional boolean array
Thank you for all your replies so far. Firstly I've gone back to the start - I have declared an instance variable called "matrix" which is capable of referncing a 2D array of type boolean
Code :
public boolean[][] matrix;
I then have 3 class constants
Code :
public static final int FONT_LETTER_HEIGHT = 5;
public static final int FONT_LETTER_WIDTH = 6;
public static final int LETTERS_PER_DISPLAY = 10;
And my default constructor for the class LEDDisplay
Code :
/**
* Default constructor for objects of class LEDDisplay
*/
public LEDDisplay()
{
super();
boolean[][] matrix = new boolean[FONT_LETTER_HEIGHT][(FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY)];
}
Surely as "matrix" is made public for testing purposes I am able to create the array of the constant sizes that I have declared above.
I am sorry if I seem to be banging on about the same thing, I'm going insane because I can't figure this out :(
-
Re: Implementing a two-dimensional boolean array
Code :
public LEDDisplay()
{
super();
boolean[][] matrix = new boolean ...
Here you have defined a new variable in addition and shadowing the global one. You have two variables named matrix. The one above is given values. The other one remains null.
Go back and read post #2
<data type> <var name> = ... //<<<< this defines a variable
<var name> = ... //<<<< this assigns a value to an existing variable
-
Re: Implementing a two-dimensional boolean array
But it's not just a simple case of removing the instance variable though is it?
-
Re: Implementing a two-dimensional boolean array
Its a simple case of removing the <data type> from the assignment statement in the main() method to make it only an assignment and not a definition and assignment.
-
Re: Implementing a two-dimensional boolean array
Only problem is that if I remove the <data type> then wouldn't it not implement an array, which completely defeats the point of display()
-
Re: Implementing a two-dimensional boolean array
Show me the line of code you are talking about.
-
Re: Implementing a two-dimensional boolean array
Look at this code example:
Code :
public class TestCode4 {
int theVar = 4; // the class variable that will be shadowed in method1
public void method1() {
int theVar = 55; // define a local variable and give it a value
System.out.println("method1 theVar=" + theVar);
}
public void method2() {
System.out.println("method2 theVar=" + theVar);
}
public static void main(String[] args) throws Exception {
TestCode4 tc4 = new TestCode4();
tc4.method1();
tc4.method2();
System.out.println("tc4.theVar=" + tc4.theVar);
// The output from above
//method1 theVar=55
//method2 theVar=4
//tc4.theVar=4
}
}
-
Re: Implementing a two-dimensional boolean array
AH! I think I may have got it. I am going to test it tomorrow after work. Many thanks for not giving me the answer directly as it forces me to actually think!!!
-
Re: Implementing a two-dimensional boolean array
Ok. Let's see what happens tomorrow.
-
Re: Implementing a two-dimensional boolean array
I have a utility class where it encodes the characters I input into 2D boolean arrays.
I specify the character using a method called writeLetter(), which then sends a getLetter() method on this utility class with the original argument specified.
Is this the best way to do it ?
Code :
public void writeLetter(char aCharacter)
{
getLetter();
}
public void getLetter()
{
boolean[][] letter;
letter = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH];
}
So if I specify writeLetter('O'), it will trigger getLetter() which sets the local variable 'letter' to a 2D boolean array but I'm unsure how to invoke the utility class to make the "O" that I have inputted into a 2D boolean array that is assigned to the variable 'letter'
-
Re: Implementing a two-dimensional boolean array
Please describe what you want the code to do. Your example methods are full of errors.
For example a method named get... should return something.
The letter array in getLetter() will go away when the method exits.
-
Re: Implementing a two-dimensional boolean array
The writeLetter() method should take an argument called aCharacter of type char and returns no result. This method should invoke the getLetter() method on the LEDFont class (utility class) with aCharacter as the argument. The getLetter() method should return a 2D boolean array which is of [row][column], which represents the LED settings for aCharacter. The array then has to be assigned to a declared local variable called 'letter'. The boolean values of 'letter' are then copied into 'matrix' (at the start) to override the existing values.
Hope it makes sense
-
Re: Implementing a two-dimensional boolean array
Sort of.
Quote:
boolean values of 'letter' are then copied into 'matrix'
Is this a merge of the contents of one array into another or is the complete replacement of one array be another?
For example if letter is [2][4] and matrix is [30][50] then it would be some kind of merge. No idea how that would be done. If the arrays were the same size, then one would replace the other.
Now try to code it.
-
Re: Implementing a two-dimensional boolean array
It's to replace it, so that when I execute display(), it will bring up what I was coding before (but with a letter I have specified)
I have no clue how to invoke the getLetter method on the LEDFront class ?? :mad:
Is it simply
Code :
getLetter.LEDFont(aCharacter);
-
Re: Implementing a two-dimensional boolean array
Quote:
how to invoke the getLetter method on the LEDFront class
The way you invoke a method of a class is to get a reference to that class and append to it the method separated by a . (dot):
aClassRef.theMethod(theargs);
-
Re: Implementing a two-dimensional boolean array
Oh, so it's LEDFont.getLetter(aCharacter); ... d'oh
-
Re: Implementing a two-dimensional boolean array
What is LEDFont? A class or a reference/pointer to an instance of a class?
For example:
AClass aClsRef = new AClass(); // create instance of a class and save its address
AClass is a class. aClsRef is a reference/pointer to an instance of a class.
If LEDFont is a class then the way you coded it would be valid if the getLetter method is static.