Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - Finding the output (Q.No. 4)
4.
What will be the output of the program?

public class TestDogs 
{
    public static void main(String [] args) 
    {
        Dog [][] theDogs = new Dog[3][];
        System.out.println(theDogs[2][0].toString());
    }
}
class Dog { }
null
theDogs
Compilation fails
An exception is thrown at runtime
Answer: Option
Explanation:

The second dimension of the array referenced by theDogs has not been initialized. Attempting to access an uninitialized object element (System.out.println(theDogs[2][0].toString());) raises a NullPointerException.

Discussion:
14 comments Page 2 of 2.

Rohit said:   1 decade ago
@Manoj.

But they had.

Dog [][] theDogs = new Dog[3][]

As in your example Date d= new Date()

Manoj said:   1 decade ago
Hi I am Manoj,

The above problem ,

nullpointerException means,
EX:
prblm:

Date d;//d holds null value
d.getyear();//this raises java.lang.NullpointerException

solv:

Date d=new Date();
d.getyear();
//this will not raises java.lang.NullpointerException
(1)

Ashwini Surya said:   1 decade ago
Please explain it briefly.

Purnima said:   1 decade ago
Any one can expalin this?


Post your comments here:

Your comments will be displayed after verification.