This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 11168
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does this give Null Pointer Exception
 
Similar Threads
Shadowing variable doubt?
method conditions not applying
what is the Output? and why?
project euler
Printing multi-dimesonial arrays