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)