I was able to get my solution verified, but strangely enough I got the following error in the output area:
java.lang.NullPointerException
at com.codegym.task.task07.task0726.Solution.main(Solution.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
My solution is as follows:
package com.codegym.task.task07.task0726;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Cat code won't compile
*/
public class Solution {
public final static ArrayList<Cat> CATS = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String name = reader.readLine();
if (name.isEmpty()) break;
int age = Integer.parseInt(reader.readLine());
int weight = Integer.parseInt(reader.readLine());
int tail = Integer.parseInt(reader.readLine());
Cat cat = new Cat(name, age, weight, tail);
CATS.add(cat);
}
printList();
}
public static void printList() {
for (Cat cat : CATS) {
System.out.println(cat);
}
}
public static class Cat {
private String name;
private int age;
private int weight;
private int tailLength;
Cat(String name, int age, int weight, int tailLength) {
this.name = name;
this.age = age;
this.weight = weight;
this.tailLength = tailLength;
}
@Override
public String toString() {
return "Cat's name: " + name + ", age: " + age + ", weight: " + weight + ", tail: " + tailLength;
}
}
}
Does anyone have any idea what's wrong? Is my solution really correct, or was there a mistake that was not caught by the verification system?