java.lang.NullPointerException
at com.codegym.task.task07.task0726.Solution.main(Solution.java:21)
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)
COMPILED BUT OUT PUT NOT COMING ,GETTING THIS ERROR
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Przemysław Dr
1 August 2019, 20:31
Please paste the code.
0
KARAN BHOITE
2 August 2019, 21:38
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;
}
}
}
0
KARAN BHOITE
2 August 2019, 21:38
please give me reply as soon as possible
0
Сергей
4 August 2019, 13:21
int tail=Integer.parseInt(reader.readLine());
Change "tail" to "tailLength"
0
Przemysław Dr
4 August 2019, 18:12
@Сергей
Tail or tailLength, it doesn't matter in this case.
@Karan
Have You tried to verify task? It looks ok. Try to enter all required information about cat i.e. Tiger, 6, 5, 22. Without any of this information NullPointer will be thrown.
0