-2

I cannot figure this one out... so I keep getting an Exception in thread

AWT-EventQueue-0" java.lang.NullPointerException
  at MakeFrame.MovementAndObjects.paint(MovementAndObjects.java:285) 

error in my code but there is no nullPointer that I can find! my rectangle []latter array is public and I'm just trying to pass the array simple objects that I cannot reference from it.

void defineObjects(){
       character = new Player("Zander", true, 10, 1, 1, 1, 1, "none", "none");
       charRect = new Rectangle(character.charX, character.charY,character.charWidth,character.charHeight);//y==about 500, x= whatever the x is currently set at
       floor1 = new Rectangle(-1, 660, 1201, 10);
       floor2 = new Rectangle (-1, 550, 1201, 10);
       floor3 = new Rectangle(-1, 440, 1201, 10);
       floor4 = new Rectangle (-1, 330, 1201, 10);
       floor5 = new Rectangle(-1, 220, 1201, 10);
       floor6 = new Rectangle (-1, 110, 1201, 10);
       wallA = new Rectangle(0,660,-700,12);
       wallB = new Rectangle(1182,660,-700,12);
       latter[0]=new Rectangle(1120,560,35,101);
       latter[1]= new Rectangle(100,450,35,101);
       latter[2]=new Rectangle(400,340,35,101);
       latter[3]= new Rectangle(20,340,35,101);
       latter[4]=new Rectangle(800,230,35,101);
       latter[5]= new Rectangle(100,230,35,101);
       latter[6]=new Rectangle(600,450,35,101);
       latter[7]= new Rectangle(500,120,35,101);
       latter[8]=new Rectangle(700,120,35,101);
       objectDefine=true;

       repaint();
}




@Override
 public void paint(Graphics g)
{
    super.paint(g);
    if(objectDefine)
    {  

        /*==========
        =THE FLOORS=
        ==========*/
       latter[0]=latter1;
       g.setColor(myBarf);//floor
       g.setColor(myBrown);//floor
       g.fillRect(floor1.x, floor1.y, floor1.width, floor1.height);
       g.fillRect(floor2.x, floor2.y, floor2.width, floor2.height);
       g.fillRect(floor3.x, floor3.y, floor3.width, floor3.height);
       g.fillRect(floor4.x, floor4.y, floor4.width, floor4.height);
       g.fillRect(floor5.x, floor5.y, floor5.width, floor5.height);
       g.fillRect(floor6.x, floor6.y, floor6.width, floor6.height);
        /*==========
        =THE  WALLS=
        ==========*/
       g.setColor(Color.red);
       g.fillRect(wallA.x,wallA.y,wallA.height,wallA.width);
       g.fillRect(wallB.x,wallB.y,wallB.height,wallB.width);
        /*===========
        =THE lATTERS=
        ===========*/
        for(int i=0; i<=latter.length; i++)
        {
            g.setColor(myBrown3);
            g.fillRect(latter[i].x, latter[i].y, latter[i].width, latter[i].height);<---- this is the bad line of code according the the error message
            g.setColor(Color.BLACK);
            System.out.println("D1 complete!!!");
            for(int j=5; j<=80; j+=15)
            {
                g.fillRect(latter[i].x+9 ,latter[i].y+j, latter[i].width/2, latter[i].height/10);
            }
        }

Any thought? I have initialized it several different ways, this is the current one that I have tried:

public Rectangle[] latter= {
  new Rectangle(1120,560,35,101), 
  new Rectangle(100,450,35,101), 
  new Rectangle(400,340,35,101),
  new Rectangle(20,340,35,101), 
  new Rectangle(800,230,35,101), 
  new Rectangle(100,230,35,101), 
  new Rectangle(600,450,35,101), 
  new Rectangle(100,230,35,101), 
  new Rectangle(700,120,35,101)
};

iv also done the standard rectangle[] latter=new Rectangle[8]; the assigned latter

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Zander
  • 84
  • 14
  • what about `latter1`? could it be null? – Loris Securo Feb 25 '17 at 23:09
  • All the latters, 0-8, are rectangles. I can call them outside of the array, but not by referencing the index of the array – Zander Feb 25 '17 at 23:10
  • can you post the initialization of the array `latter`? – Loris Securo Feb 25 '17 at 23:20
  • `i<=latter.length` in the `for ` seems incorrect, it should be `i < latter.length` (remember that if length is 9 then the elements you can access are from 0 to 8). But that should cause an `IndexOutOfBoundsException` rather than a `NullPointerException`. – Loris Securo Feb 25 '17 at 23:30
  • I chaged that code and now its working! thank you! but otherparts of my program are now getting arrayIndexOutOfBoundsExceptions. Anyways Thank you for your help Loris! – Zander Feb 25 '17 at 23:36
  • You don't mention what your class is extending, but if it is a JPanel, you should not be overriding the paint() method - override paintComponent() instead. And if you are extending JFrame, don't do that - extend a JPanel instead, and just add an instance of that to a JFrame. – FredK Feb 25 '17 at 23:41

1 Answers1

1

i<=latter.length in the for seems incorrect, it should be i < latter.length (remember that if length is 9 then the elements you can access are from 0 to 8). But that should cause an IndexOutOfBoundsException rather than a NullPointerException.

Be careful with the array dimensions, the correct initialization of latter, for example should be Rectangle[] latter = new Rectangle[9];, so 9 and not 8 as you posted.

Loris Securo
  • 7,538
  • 2
  • 17
  • 28