*
The moose likes Java in General and the fly likes Why does this give Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why does this give Null Pointer Exception" Watch "Why does this give Null Pointer Exception" New topic
Author

Why does this give Null Pointer Exception

sanpraveen Kumar
Greenhorn

Joined: May 06, 2005
Posts: 1
import java.io.*;

public class B //1
{
public static void main(String arg[]) //2
{
int [][]a=new int[2][];

for(int k=0;k<2;k++)
{a[0][k]=0;}
for(int k=0;k<3;k++)
{a[1][k]=k;}

}
}
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Because you're not initializing the int[] elements of your int[][].


James Carman, President<br />Carman Consulting, Inc.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

In other words, you've created a REFERENCE to a 2d array, but you don't ever create the elements in it.

it's like saying "Get an envelope. now, take the letter out of the envelope and go to page 3..."


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: Why does this give Null Pointer Exception
 
Similar Threads
method conditions not applying
what is the Output? and why?
project euler
Printing multi-dimesonial arrays
Shadowing variable doubt?