简体   繁体   English

Java-空指针异常

[英]Java - Null pointer exception

I am creating a simple video game and I have been trying to add some basic collision detection. 我正在创建一个简单的视频游戏,并且我一直在尝试添加一些基本的碰撞检测。 I am doing this out of a entityCollision.class that I have created. 我是在我创建的entityCollision.class中执行此操作的。 The collision detection uses rectangles that are placed outside of the charater of AI. 碰撞检测使用放置在AI字符外部的矩形。 I am trying to create these rectangles by doing this: 我正在尝试通过执行以下操作创建这些矩形:

Movement Movement;
AIMovement AIMovement;

Rectangle player = new Rectangle(Movement.x + 4, Movement.y, 56, 64);
Rectangle robot1 = new Rectangle(AIMovement.x[0] + 16, AIMovement.y[0], 32, 64);
Rectangle robot2 = new Rectangle(AIMovement.x[2] + 16, AIMovement.y[2], 32, 64);
Rectangle robot3 = new Rectangle(AIMovement.x[3] + 16, AIMovement.y[3], 32, 64);
Rectangle robot4 = new Rectangle(AIMovement.x[4] + 16, AIMovement.y[4], 32, 64);

In the code I first make a non-static way to refrence the class. 在代码中,我首先采用一种非静态的方式来刷新该类。 After that I try to make the variables using the cords from the other classes. 之后,我尝试使用其他类中的电线进行变量制作。 This is were it gives me the nullpointerexeption, right on the line of the first rectangle. 这是因为它给了我nullpointerexeption,就在第一个矩形的行上。 All three of the classes I am using are created around the same time, this is the code: 我正在使用的所有三个类都是在大约同一时间创建的,这是代码:

Main Main;
Movement Movement;
AIMovement AIMovement;
Bullet Bullet;
entityCollision entityCollision;

public GamePanel() throws FileNotFoundException, InterruptedException{
    Movement = new Movement();
    AIMovement = new AIMovement();
    Bullet = new Bullet();
    entityCollision = new entityCollision();

I get the error right after the code is created like in the code above. 像上面的代码一样,在创建代码后立即得到错误消息。

I would like to know how would I make it were the entityCollision class is created after the other classes variables are not null or other ways of getting ride of the error. 我想知道在其他类变量不为null或其他获取错误的方法之后创建了entityCollision类的情况下,该如何处理。

Just comment and I will add more code or answer questions 请发表评论,我将添加更多代码或回答问题

Why do you go from 你为什么从

Rectangle robot1 = new Rectangle(AIMovement.x[0] + 16, AIMovement.y[0], 32, 64);

to

Rectangle robot2 = new Rectangle(AIMovement.x[2] + 16, AIMovement.y[2], 32, 64);?

On your fourth robot, if there is nothing in AIMovement.x[4] and/or the corresponding y , which I am assuming there is not, then you are passing a null pointer to the Rectangle class constructor. 在您的第四个机器人上,如果AIMovement.x[4]和/或对应的y没有任何东西(我假设没有),那么您正在将一个空指针传递给Rectangle类构造函数。

First fix all the cases where you have a variable name that collides with a class name. 首先解决所有变量名与类名冲突的情况。 The best way is to follow the convention of naming each variable starting with a lower case letter and each class starting with an upper case letter. 最好的方法是遵循以下约定:将每个变量命名为小写字母,将每个类命名为大写字母。

You have a bunch of these cases, and they make it ambiguous whether eg Movement.x gets the static variable x from the Movement class or the instance variable x from the Movement variable -- which is null so that expression will give a NullPointerException. 你有一大堆的这些情况,和他们说暧昧是否如Movement.x获取静态变量xMovement类或实例变量xMovement变量-这是空,这样表达会给出一个NullPointerException异常。

Now you need to set those variables before use. 现在,您需要在使用之前设置这些变量。

If you're still puzzled on what's causing the exception, step through the code in the debugger and see where the exception happens and what's null at that point. 如果您仍然对导致异常的原因不解,请在调试器中逐步执行代码,查看异常发生的位置以及此时的null。 You can split that statement into smaller parts if it has more than one expression that could cause the exception. 如果该语句具有多个可能导致异常的表达式,则可以将其拆分为较小的部分。

Eg new Rectangle(aIMovement.x[0] + 16, aIMovement.y[0], 32, 64) will throw a NPE if aIMovement is null or aIMovement.x is null or aIMovement.y is null. 例如new Rectangle(aIMovement.x[0] + 16, aIMovement.y[0], 32, 64)如果aIMovement为null或aIMovement.x为null或aIMovement.y为null new Rectangle(aIMovement.x[0] + 16, aIMovement.y[0], 32, 64)将引发NPE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM