-3

I have this problem with my code. Its called java.lang.nullpointerexception. and I cant seem to fix it. please help me take a look at it. Thank you. I didnt include the class name and import. class name is called CHORD. I didnt make public static since my assginemnt say dont use global variable.

private ArrayList<Integer> nodeList;

public static void main(String[] args){


    CHORD obj = new CHORD();
    obj.nodeList = new ArrayList<Integer>();

    String filename ="";

    if(args.length ==1){
    filename = args[0];
    obj.read(filename);
    }

}

public void read(String file){
    CHORD obj = new CHORD();
    obj = null;

    Scanner loadFile = null;
    try{
    loadFile = new Scanner(new File(file));
    String inputLine;

    while(loadFile.hasNextLine()){
        inputLine = loadFile.nextLine();

        String[] inputArray = inputLine.split(" ",3);

        if(inputArray[0].equalsIgnoreCase("init")){

            int size = Integer.parseInt(inputArray[1]);
            setSizeFT(init(size));  
        }
        else if(inputArray[0].equalsIgnoreCase("addpeer")){
            System.out.println("adding");
            nodeList.add(Integer.parseInt(inputArray[1]));

        }
    }
    }
    catch(FileNotFoundException x){

    }
    finally{
        System.out.println(getFT());
        loadFile.close();
    }
    System.out.println(getFT());
}

public void print(){
    CHORD obj = new CHORD();
    for(int x =0; x< obj.nodeList.size(); x++){
        System.out.println(obj.nodeList.get(x));
    }
}


public int init(int num){
    int n = 23;
    double k = Math.ceil(Math.log(n)/Math.log(2));
    int size = (int)k;
    return size;
}

public void setSizeFT(int size){
    sizeFT = size;
}

public int getFT(){
    return sizeFT;
}

}

4

1 回答 1

4

Here's an explanation of what NullPointerException's are: http://antwerkz.com/dealing-with-nullpointerexceptions/ From the article:

The most common (and obvious to the seasoned developer) is that you didn't initialize a variable.

Looking at that line, it looks like obj.nodeList may be null. Here's how I deduced that:

  1. I can see that obj is not null, because the first line is CHORD obj = new CHORD();. That means you didn't set obj to null.
  2. I can tell that Integer is not null. That's a class and you're calling a static method. That can't be null because there's nothing to be assigned there.
  3. inputArray[1] may return null, but if that were happening, your stack trace would not end on this line, it would probably end on some line inside Integer.parseInt. I'd need to see a full stack trace to be sure though. But looking at the javadoc of Integer.parseInt, it doesn't say it'll throw a NPE so that's even more evidence to rule it out.
  4. If inputArray was null, you'd probably get the error on the first if statement so I can rule that out.

Somewhere your code needs to do a obj.nodeList = new NodeList() or something like that. I can't say for sure without seeing what the CHORD class looks like.

于 2013-02-09T06:57:35.477 回答