0

So I was trying to code a viewer class and then create a list which is based on these viewers as another Class called ViewerList.

Usually everything should work but somehow it doesn't let me use the methods "toLine()" and "getName()". If anyone here could help me it would be awesome. As in title the only error-report i get is NullPointerException.

This is the Viewer class:

public class Viewer {

private int points = 0;
private String name;

public Viewer(String name, int points) {
    this.points = points;
    this.name = name;
}

public int getPoints() {
    return points;
}

public String toLine() {
    String line = getName() + " " + getPoints();
    return line;
}

public void addPoint(int amount){
    points += amount;
}

public String getName(){
    return name;
}

This is the ViewerList class:

public class ViewerList {

private Viewer[] array;
private int nextFreeSlot = 0;
private int count = 0;

public ViewerList(int capacity){
    array = new Viewer[capacity];
}

public void add(Viewer o){
    if (nextFreeSlot == array.length) {
        throw new IllegalStateException("Liste ist voll!");
    }
    array[nextFreeSlot] = o;
    count++;
    nextFreeSlot++;
}

public Viewer[] getArray(){
    return array;
}

public Viewer get(int index) {
    return array[index];
}

public int getCapacity() {
    return array.length;
}

public int getElementCount() {
    return count;
}

public void addPoints () {
    if(array.length > 0){
        for(Viewer v  : array){
            v.addPoint(1);
        }
    }
}

public int getPointsOf(String viewerName) {
    int points = 0;
    String vName = "";
        for(Viewer v : array){
                vName = v.getName(); // The v.getName() method is an error here
            if(viewerName.equals(vName)) {
                    points = v.getPoints();
            }

        }

    return points;
}

public boolean contains(String name) {
    boolean contains = false;
        for(Viewer v : array){
            if(name.equals(v.getName())){ // also here i get the NullPointerException
                contains = true;
                break;
            }
        }
    return contains;
}

}

Also if you need the stack trace then here it is:

java.lang.NullPointerException
    at bot.PointList.getPointsOf(PointList.java:50)
Zissouu
  • 924
  • 2
  • 10
  • 17
  • 2
    If your method throws an exception that you don't want or expect (based on its input), then perforce it is *not* coded correctly. – John Bollinger Oct 06 '17 at 17:30
  • 2
    Where is the PointList.java? That's what's throwing the exception according to your stack trace. – Michael Platt Oct 06 '17 at 17:32
  • I think viewername or v is null and not name. Print v and viewer name before calling any operation on it or use debugger to see it's value – Optional Oct 06 '17 at 17:32
  • Moreover, your `Viewer.getName()` will never throw. If you try to invoke it on a null reference, however, then the implicit dereference will certainly throw an NPE. – John Bollinger Oct 06 '17 at 17:34
  • Can you please show us the code where you are creating a new `Viewer` object? – Chris Phillips Oct 06 '17 at 17:48
  • Guess: You are iterating over all of the array's elements while not all of them are initialized. The *enhanced for loop* will include `null` elements. – Izruo Oct 06 '17 at 17:56

1 Answers1

0

Null pointer exception occurs when you do not initialize the declared variable. My guess in your case is the string name. Debug your code by adding a print statement in your getName() method.