0

I am currently trying to create a pathfinding method for my grid based game based on the A* method algorithm. However I am having a basic problem with manipulating variables within my PathNode class instances:

public void AStarPathfinding(PathNode snakeHead, PathNode foodLocation) {

    System.out.println(food.xFood);
    System.out.println(food.yFood);

        openNodes.add(snakeHead);

        int xHead = (int) snakeSegments.get(0);
        int yHead = (int) snakeSegments.get(1);


        snakeHead.xCoordinate = (int) xHead;
        snakeHead.yCoordinate = (int) yHead;
        foodLocation.xCoordinate = (int) food.xFood;
        foodLocation.yCoordinate = (int) food.yFood;

however I am receiving null-point exception errors:

Exception in thread "Thread-2" java.lang.NullPointerException
    at ArtificialSnake.AStarPathfinding(ArtificialSnake.java:136)

which is this line:

snakeHead.xCoordinate = (int) xHead;

The idea is to set the startNode(snakeHead) to the current snake head's location.... but as suggested above I cannot work out how to modify the xCoordinate variable in the snakeHead instance of the PathNode class.

Looking at another question: Edit variables from object in ArrayList?

It suggests using setters, I have tired this however I still get null point exception errors.

Note: the Thread2 is the gameLoop thread separate from the Swing U.I, the class that this pathfinding method is in is part of the same thread.

What am I missing here?


mysqli_num_rows returns 1 no matter what

When I do a SQL search in phpMyAdmin (substituting the variable for the actual value) it returns the correct row number but when using PHP to return this value it always returns 1 no matter what. Thanks in advance.

function user_exists($username) {
    $link = mysqli_connect('localhost','root','','test');
    $username = sanitize($username);
    $query = mysqli_query($link, "SELECT COUNT(`user_id`) FROM `new_base` WHERE `username`='$username'");
    $row_cnt = mysqli_num_rows($query);
    echo $row_cnt;
    mysqli_free_result($query);
    mysqli_close($link);
}
4

1 回答 1

0

如果您在该行上收到一个空指针错误,snakeHead.xCoordinate = (int) xHead;这意味着,请检查它没有snakeHead被传递到方法中。如果您想知道为什么没有发生空指针错误是因为可以添加到某些 java 集合中,那么它们不会全部尝试对您传递给它们的内容执行任何操作(那些可能调用,或取决于集合类型及其实现)。nullnullopenNodes.add(snakeHead);nullhashCode()equals()compare()

当您在方法之间传递对象并将对象传递到集合/数组中时,您正在制作浅拷贝(您正在传递对对象的引用),因此您对对象所做的任何更改都将在您传递引用的所有其他地方可见。

对对象进行深度复制(这样更改不会影响对对象的其他引用)的唯一方法是复制对象的每个成员变量(某些类可能提供执行此操作的克隆方法)。

于 2013-08-05T18:40:29.377 回答