Results 1 to 2 of 2

Thread: Static variable

  1. #1
    m4r7in is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default Static variable

    I have 2 problems, first one is code from my class "Square":
    Java Code:
    ...
    private static Square mChosenSquare;
    ... 
    public boolean isMarked()
        {
            boolean returnValue = false;
            if(mChosenSquare != null)
            {
                if((mChosenSquare.getCol() == this.getCol()) && (mChosenSquare.getRow() == this.getRow()))
                {
                    returnValue = true;
                }
            }
            return returnValue;
        }
    When i try to run the project i get:

    Exception in thread "main" java.lang.NullPointerException
    at ravspelet.Square.isMarked(Square.java:57)



    -----------------------------------------------------------------------




    My second question is on this code:

    Java Code:
        private class PlayingField extends JPanel
        {       
            private Graphics g;  
            
            public PlayingField(MouseListener listener)
            {
                addMouseListener(listener);
            }
             
            public void paintSquare(int x,int y, Color color)
            {
                g.setColor(color);
                // -1 för att få en markering mellan rutorna
                g.drawRect(x*SQUARE_SIZE, y*SQUARE_SIZE, SQUARE_SIZE-1, SQUARE_SIZE-1);
            }
            
            public void paintPiece(int x,int y, Image image)
            {
                g.drawImage(image, x*SQUARE_SIZE, y*SQUARE_SIZE, SQUARE_SIZE-1, SQUARE_SIZE-1, null);
            }
            
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
            }
        }
    I havent tested it yet because i still need to complete other parts of the program but i have a strong feeling the last part wont work (the paintComponent method)


    Any help is very much appreciated...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do visit the Sun graphics tutorial as you are doing your painting wrong. You need to do your painting in the paintComponent method, not outside of it. the tutorial will show you how.

Similar Threads

  1. Static variable context Errors ?
    By Shyam Singh in forum New To Java
    Replies: 16
    Last Post: 08-08-2008, 09:11 PM
  2. static variable
    By udhayageetha in forum AWT / Swing
    Replies: 17
    Last Post: 07-07-2008, 11:22 AM
  3. About static variable
    By MarkWilson in forum New To Java
    Replies: 5
    Last Post: 06-27-2008, 01:43 PM
  4. Replies: 1
    Last Post: 08-01-2007, 09:25 PM
  5. Help with static variable counter
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-19-2007, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •