File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 10916
    
  12

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why does this give Null Pointer Exception
 
Similar Threads
Shadowing variable doubt?
method conditions not applying
project euler
Printing multi-dimesonial arrays
what is the Output? and why?