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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 41

Thread: Implementing a two-dimensional boolean array

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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

     
    /**
     * 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


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

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

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

    Default Re: Implementing a two-dimensional boolean array

    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.

    I don't see how the matrix overrides the other? I must be being blind.
    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

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

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

    Default Re: Implementing a two-dimensional boolean array

    I suspect the error is occuring on this line:
    if (matrix[j][i])
    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.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Implementing a two-dimensional boolean array

    You don't have to resort to trial and error if you understand what you are doing.
    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.
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    public boolean[][] matrix;

    I then have 3 class constants

    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

    	/**
    	 * 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

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

    Default Re: Implementing a two-dimensional boolean array

            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
    Last edited by Norm; July 13th, 2011 at 04:10 PM.

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing a two-dimensional boolean array

    But it's not just a simple case of removing the instance variable though is it?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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()

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Implementing a two-dimensional boolean array

    Show me the line of code you are talking about.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Implementing a two-dimensional boolean array

    Look at this code example:
    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
       }
    }

  16. #16
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!!!

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Implementing a two-dimensional boolean array

    Ok. Let's see what happens tomorrow.

  18. #18
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ?

         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'

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  20. #20
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Implementing a two-dimensional boolean array

    Sort of.
    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.

  22. #22
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ??

    Is it simply
    getLetter.LEDFont(aCharacter);
    Last edited by eNxy; July 14th, 2011 at 05:17 PM.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Implementing a two-dimensional boolean array

    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);

  24. #24
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing a two-dimensional boolean array

    Oh, so it's LEDFont.getLetter(aCharacter); ... d'oh

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

    Default 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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem printing a two dimensional boolean array
    By sallyB in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 03:56 PM
  2. Two-dimensional boolean array?
    By tcmei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 08:32 PM
  3. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  4. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 07:18 AM
  5. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM