1

I am trying to write program and keep getting a nullPointerException when I call a particular method, what does this mean ?

5
  • This question and the code looks extremely familiar. I've seen this definitely before a few hours ago, but can't find it now... It was answered there. Commented Mar 5, 2011 at 16:09
  • @stackoflow, Hm.. Have you recreate topic and delete previous? (-1 until answer) Commented Mar 5, 2011 at 16:12
  • 2
    This question is extremely similar to stackoverflow.com/questions/5185577/java-generic-arguments/… Commented Mar 5, 2011 at 16:19
  • This is not homework, and yes this is a previously submitted topic - I phrased my last question badly, it got spammed, I spoke to a mod who deleted it so that it wouldn't confuse any people trying to learn from it. Acceptable answer ? Commented Mar 5, 2011 at 16:58
  • -1 undoned as answer provided. Commented Mar 5, 2011 at 17:51

1 Answer 1

1

I think it should be

private int size; //non static

private static <S extends Comparable<S>> MyList<S> leftHalf(MyList<S> list) {
    MyList<S> leftSide = new MyList<S>();
    int middle = list.size() /2;
    for (int countToMiddle = 0; countToMiddle < middle; countToMiddle++) {
        leftSide.addEnd(list.head());
    }

    return leftSide;
}

if no, please provide more information about what this method should do.

upd: construction issue

public MyList() {   //takes no arguments
    nodes = null;
}
public MyList(T... args) {  //takes any number of arguments
    this();
    for(T t : args){
        add(t);
    }
}

upd: addEnd issue

public void addEnd(T item) {
    if (nodes == null) {
        nodes = new NodesList<T>(item, null);
        return;
    }
    if (nodes.tail == null) {
        nodes.tail = new NodesList<T>(item, null);
    } else {
        nodes.tail == new NodesList<T>(nodes.tail, item);
    }
}
Sign up to request clarification or add additional context in comments.

17 Comments

This method should split the list into two lists, in the first(leftSize) it will contain the elements from 0 to n/2, in the second, rightSide, it will contain the elements from n/2 to n. Unfortunately my implementation does not seem to work, as, when I call 'MyList list = new MyList<Long>( 1L, 2L, 3L ); //3 entries list.print();', I get the error cannot find symbol symbol : constructor MyList(long,long,long)
@stackoflow, I think, this method should do exactly what you want) , correct? Optionally you can remove static modifier and list argument.
@stackoflow, about constructor : you should create constructor with vararg to do things like this. It's other question)
I believe it should, by my logic, which is often incorrect! I cannot test it, however. Unfortunately I am still having the error that when I call the MyList constructor, I get the message "cannot find symbol symbol : constructor MyList(long,long,long)".
@Stas , could you please elaborate on using vararg ? Thanks.
|

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.