Adding Numbers to a Range

浪尽此生 提交于 2019-12-12 04:06:07

问题


I'm making a program where I can add an Integer sequence to a Range. I'm having trouble adding this values to the Range. It should run the method addNodeAfter, but it does nothing.

Then when I want to display the Range I get a NullPointerException one this line:

for (int i = 1; i <= manyNodes; i++){

Any tips?

Main:

public class PDEMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

            Scanner input = new Scanner(System.in);
            System.out.print("Enter a start number: ");
            Integer startNum = input.nextInt();

            System.out.print("Enter end number: ");
            Integer endNum = input.nextInt();

            System.out.print("Enter increment: ");
            Integer increment = input.nextInt();

            Range obj = new Range(startNum, endNum, increment);
            System.out.println(obj);

            obj.display();
    }

}

Range:

public class Range implements Cloneable {

    private Integer data; // holds the data
    private Range link; //holds the link
    Range head; //refers to head of linked list
    private Integer manyNodes;
    private Integer startValue;
    private Integer endValue;
    private Scanner input;

    public Range(Integer data, Range link){    
        this.data = data;
        this.link = link;
    }

    public Range(Integer data, Range link, Range head) {
        this.data = data;
        this.link = link;
        this.head = head;
        manyNodes++;
    }


    public Range(Integer start, Integer end,Integer increment){
        if(start == null){
            startValue = 0;
        }
        if(increment == null){
            if(start < end){
                increment++;
            }else{
                increment--;
            }
        }
        for (int i = start; i <= end; i+= increment){
            addNodeAfter(i);
            System.out.println(i);
        }

    }

    public Integer getData() {
        return data;
    }

    public void setData(Integer data) {
        this.data = data;
    }

    public Range getLink() {
        return link;
    }

    public void setLink(Range link) {
        this.link = link;
    }

    public Range getHead() {
        return head;
    }

    public void setHead(Range head) {
        this.head = head;
    }

    public void addNodeAfter(Integer element){

        this.link = new Range(element, this.link);

    }

    public void display(){
        Range cursor = head;

        for (int i = 1; i <= manyNodes; i++){ // NPE on this line
            System.out.print(cursor.getData() + " ");
            cursor = cursor.getLink();            
        }
        System.out.println("");
    }

}

回答1:


You have defined manyNodes as an Integer, not an int. This means that its default value is null, not 0, and you never set the value anywhere in your code.

When you try to loop using it as the control variable in your display() method, the JVM is going to throw an NPE when it tries to unbox the null.

A quick fix would be to change the type to int:

private int manyNodes;

This will solve the immediate NPE, but still not display anything, since you never actually increment manyNodes in the constructor you're calling. This means that the for-loop in your display() method falls through and never actually prints any of the data.

I would recommend getting rid of head and manyNodes altogether, and re-writing your display() method along the lines of:

public void display() {
    Range cursor = getLink();
    while (cursor != null) {
        System.out.print(cursor.getData() + " ");
        cursor = cursor.getLink();
    }
    System.out.println("");
}

Note that this will output the data "backwards" because of the way you add things in this constructor:

public static void main(String[] args) throws Exception {
    Range obj = new Range(1, 10, 1);
    obj.display();  // prints 10 9 8 7 6 5 4 3 2 1
}

You might want to take a look at an existing linked-list implementation to get a better idea how they are normally written.



来源:https://stackoverflow.com/questions/34364453/adding-numbers-to-a-range

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!