开发者

about null pointer exception

Hi I have written such this code but it will return this exception .and I do not know why please help me thanks.

         private void Scan(DoublyLinkedList dList) { // T(n) = O(n)
    DNode p1 = dList.getFirst();


    while (p1!=null) {


        DNode p2 = p1.next;
         System.out.println(p1.getElement().toString()+"lol");
        if (p2.next!=null) {

         DNode p3 = p2.next;

            if(p3.getElement()!=null){

                boolean b = Determinate.isPointRightSide(p1.getElement(), p2.getElement(),p3.getElement());

                if (b == true) {
                    p1 = p1.next;
                } else {
                    p1.next = p3;
                    p3.prev = p1;
                    dList.remove(p2);
                    p1 = p1.prev;
                }


            }
            else break;


    }else break;}

}

    public static double determinate(Object get, Object get0, Object get1) {

    double data[][] = new double[3][2];

    data[0][0] = ((Point) get).getX();
    data[0][1] = ((Point) get).getY();
    data[1][0] = ((Point) get0).getX();
    data[1][1] = ((Point) get0).getY();
    **data[2][0] = ((Point) get1).getX();**
    data[2][1] = ((Point) get1).getY();


    return ((data[0][0] * (data[1][1] - data[2][1])) - (data[1][0] * (data[0][1] - data[2][1])) + (data[2][0] * (data[0][1] - data[1][1])));
}

exception:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at ConvexHull.Determinate.determinate(Determinate.java:55)
        at ConvexHull.Determinate.isPointRightSide(Determinate.java:15)
        at ConvexHull.GrahamVersion.Scan(Graha开发者_C百科mVersion.java:104)
        at ConvexHull.GrahamVersion.grahamScan(GrahamVersion.java:83)
        at ConvexHull.GrahamVersion.<init>(GrahamVersion.java:25)
        at UI.MainFrame.grahamButtonActionPerformed(MainFrame.java:221)

this will show that "p3" is null! but I have check "p3" why it returns "null" again? I use strong for showing those lines that throws exception.

EDIT: Ihave edited my post but it will throw this exception for "p1"


One thing that looks wrong is:

if (!p3.equals(null))

This will generally always be true (if p3 != null) or throw a NullPointerException (if p3 == null)

The correct way to test whether p3 is not null is:

if (p3 != null)

Although that may not be why you're getting your NullPointerException

If the NullPointerException occurs on the line you highlight, it must be because get1 is null. This is passed in as p3.getElement(), so find out whether that could be null.

In theory, if data[2] was null then data[2][0] would throw a NullPointerException but since you initialize data then that won't be the problem in this case.

Also, is there some reason that your parameters for determinate() are Object instead of Point? If this is your actual code and not some simplified test-case, then the parameters should all be Point since that's what they must be.

Edit:
I see you've changed your original code to add some of the suggestions on this page.

But I still see some problems:

while (p1!=null) {
    DNode p2 = p1.next;

    if (p2.next!=null) {
//      ^^^^^^^ If p2 is null, then this will throw NullPointerException

     DNode p3 = p2.next;

     if(p3.getElement()!=null){
//      ^^^^^^^^^^^^^^^   If p3 is null, then this will throw NullPointerException


         boolean b = Determinate.isPointRightSide(p1.getElement(), p2.getElement(),p3.getElement());
//                                                ^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^ if one of these returns null then isPointRightSide() will throw a NullPointerException


I'm pretty sure you got the wrong line, because this:

!p3.equals(null)

will not work - that line (or those involving p1 or p2) is throwing the Exception. You cannot invoke any method on a null, including equals(). Use this instead for all your null checks:

p3 != null
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜