the output showing is what I get when I verify, yet it verified and completed. Why? How do I get rid of the error and get the output I am expecting?
Output
java.lang.NullPointerException
at com.codegym.task.task07.task0726.Solution.main(Solution.java:25)
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)
Can someone please explain why this compiled and verified while throwing an error? And how to make it work?
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
David Haines
23 August 2020, 19:52
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));
int age;
int weight;
int tailLength;
while (true)
{
String name = reader.readLine();
if (name.isEmpty())
{
break;
}
else
{
age = Integer.parseInt(reader.readLine());
weight = Integer.parseInt(reader.readLine());
tailLength = Integer.parseInt(reader.readLine());
/* Cat cat = new Cat(name); */
Cat cat = new Cat(name, age, weight, tailLength);
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: " + this.name + ", age: " + this.age + ", weight: " + this.weight + ", tail: " + this.tailLength;
}
}
}
0
hidden #10651964
23 August 2020, 18:17
Show your code.
0