Java Programming - Language Fundamentals - Discussion

Discussion :: 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 { }

[A]. null
[B]. theDogs
[C]. Compilation fails
[D]. An exception is thrown at runtime

Answer: Option D

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.


Purnima said: (Nov 6, 2011)  
Any one can expalin this?

Ashwini Surya said: (Nov 9, 2011)  
Please explain it briefly.

Manoj said: (Oct 3, 2012)  
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

Rohit said: (Feb 23, 2013)  
@Manoj.

But they had.

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

As in your example Date d= new Date()

Vinix said: (Jun 5, 2013)  
Though we have

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

This comment is an uninitialized array of "Objects"

If you recall that an uninitialized object contain null pointer which actually points to nothing.

Hence the Exception.

Sri said: (Oct 13, 2013)  
In the above program we have initialized just first column not second one. So we are getting null pointer exception.

Lucky said: (Sep 21, 2014)  
Can anyone explain that what's the solution of this problem.

Piyali said: (Dec 22, 2014)  
Dog[][] the Dogs = new Dog[3][];

Should be replaced by int Dog[][] the Dogs = new Dog[3][any no.];

Arjun said: (Jun 9, 2015)  
Dog [][] the Dogs = new Dog[3][];

The Dogs[2] = new Dog[7]; // Initialize the 3rd row of the Dogs 2D array.

The Dogs[2][0] = new Dog (); // Initialize the Dog instance at the 1st column of the 3rd row.

System.out.println (the Dogs[2][0].toString ()); // Now you can execute methods of the Dogs[2][0].

Queen said: (Sep 13, 2015)  
public class Test
{
public static void main(String [] args)
{
int [][] theDogs = new int[2][];
System.out.println(theDogs[1][0]);
}
}

The above program will give throw the same Exception NullPointer because we have only declared an array and not initialized it.

Now check the below program i.e. after initializing the array.

public class Test . This code will print the value 3
{
public static void main(String [] args)
{
int [][] theDogs = new int[][] {{1,2},{3,4}};
System.out.println(theDogs[1][0]);
}
}

Sid said: (Jan 23, 2017)  
Why not compilation fails?

Raj said: (Aug 26, 2017)  
Why array didn't initialized automatically?

Shr said: (Sep 24, 2017)  
But Dog is a class. And if we want to create an array fo object we need only one subcript.

Devi said: (Sep 11, 2019)  
Dog is a class and why the object has been created like that?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.