Java矩形90数组空指针

时间:2017-02-25 23:04:45

标签: java arrays arraylist nullpointerexception awt-eventqueue

我无法想出这个...所以我一直在线程

中得到一个例外
from lxml import html
import requests

page = requests.get('http://mlb.mlb.com/mlb/standings/exhibition.jsp?ymd=20161002')
tree = html.fromstring(page.content)
#This will create a list of buyers:
##buyers = tree.xpath('//div[@title="buyer-name"]/text()')
#This will create a list of prices
prices = tree.xpath('//td[@class="tg_w"]/text()')

print("Wins: ", prices)
print()
##print("Buyers: ", buyers)
我的代码中出现

错误,但我找不到nullPointer!我的矩形[]后一个数组是公共的,我只是试图传递我无法从中引用的数组简单对象。

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

有什么想法?  我已经用几种不同的方式对其进行了初始化,这是我尝试过的最新方法:

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

iv也做了标准的矩形[]后者=新的Rectangle [8];指定的后者

1 个答案:

答案 0 :(得分:1)

i<=latter.length中的{p> for似乎不正确,应为i < latter.length(请记住,如果长度为9,则您可以访问的元素来自08)。但这应该会导致IndexOutOfBoundsException而不是NullPointerException

小心数组维度,latter的正确初始化,例如应该是Rectangle[] latter = new Rectangle[9];,所以9而不是8