0

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(GrahamVersion.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"

7
  • I swear I just commented on a question similar to this one. Have you made sure to check if your get objects are not null? Commented Nov 27, 2010 at 3:29
  • get object for p3 is null!I do not know why ? I have checked it before with !p3.equals(null) Commented Nov 27, 2010 at 3:36
  • I used p3!=null but still throws nullpointerexception. Commented Nov 27, 2010 at 3:49
  • Check whether p3.getElement() (not p3) is null since that's what is initializing get1. Commented Nov 27, 2010 at 4:06
  • I have checked that if (p3!=null) do not get determinate but it gets???? Commented Nov 27, 2010 at 4:09

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Yep. You have to write it as "if (p3 != null)".
also my element in each node is an object.
0

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

2 Comments

also my element in each node is an object.
@user472221: your current code only checks p3.getElement() for null but not p3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.