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