null pointer exception while reading array

I was trying above example as follows
http://www.go4expert.com/forums/showthread.php?t=1162

public class ArrayReading {
      public static void main( String [] args ) {
      String[][] Data = null;  
      //Assign the values, do it either dynamically or statically
      //For first fow
      Data[0][0] = "S"; //lastname
      Data[0][1] = "Pradeep"; //firstname
      Data[0][2] = "Kolkata"; //location  //Second row
      Data[1][0] = "Bhimani"; //lastname  
      Data[1][1] = "Shabbir"; //firstname  
      Data[1][2] = "Kolkata"; //location
      //Add as many rows you want  
      //printing
      System.out.print("Lastname\tFirstname\tLocation\n");
      for(int i = 0;i<2;i++) {  
             for(int j = 0;j<3;j++)  
            {     System.out.print(Data[j]+"\t");  
             }  
            //move to new line  
            System.out.print("\n");
            }

      }
      

}



When  try to run getting null pointer exception.

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
 
Sharon SethCommented:
Seems you did not recompile after making the changes ;)
0
 
GreggCommented:
Initialize the array. String[][] Data = new String[2][3]; //two rows with 3 cols

See Java Tutorials: Arrays
0
 
gudii9Author Commented:
changed. still getting null pointer exception.please advise
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
GreggCommented:
Try this:
public class ArrayReading {
      public static void main( String [] args ) {
        String[][] Data = new String[2][3];  
        //Assign the values, do it either dynamically or statically
        //For first fow
        Data[0][0] = "S"; //lastname
        Data[0][1] = "Pradeep"; //firstname
        Data[0][2] = "Kolkata"; //location  
        
        //Second row
        Data[1][0] = "Bhimani"; //lastname  
        Data[1][1] = "Shabbir"; //firstname  
        Data[1][2] = "Kolkata"; //location

        //Add as many rows you want  
        //printing
        System.out.print("Lastname\tFirstname\tLocation\n");
        for(int i = 0;i<2;i++) {  
            for(int j = 0;j<3;j++) {
            {     
                System.out.print(Data[i][j].toString()+"\t");
            }  
        }  
        //move to new line  
        System.out.print("\n");
        }

    }
}

Open in new window

0
 
GreggCommented:
Be sure you account for both the row position and column position in the array.

//This is missing the [i] increment. 
//Also remember toString() will print the String value.
System.out.print(Data[j]+"\t");

//Above should look like this.
System.out.print(Data[i][j].toString()+"\t");

Open in new window

0
 
GreggCommented:
One more time with proper {} indention.

public class ArrayReading {
    public static void main( String [] args ) {
        String[][] Data = new String[2][3];  
        //Assign the values, do it either dynamically or statically
        //For first fow
        Data[0][0] = "S"; //lastname
        Data[0][1] = "Pradeep"; //firstname
        Data[0][2] = "Kolkata"; //location  
        //Second row
        Data[1][0] = "Bhimani"; //lastname  
        Data[1][1] = "Shabbir"; //firstname  
        Data[1][2] = "Kolkata"; //location
        //Add as many rows you want  
        //printing
        System.out.print("Lastname\tFirstname\tLocation\n");
        for(int i = 0;i<2;i++) {  
            for(int j = 0;j<3;j++) {
                System.out.print(Data[i][j].toString()+"\t");
            } //end j
            //move to new line  
            System.out.print("\n");
        }//end i
    }//end main
}//end class

Open in new window


Please take a look at the Java Trails for Arrays Specifically, the "Creating, Initializing, and Accessing an Array" section. Hope that helps.
0
 
zzynxSoftware engineerCommented:
>> changed.
To what (code)?

>> still getting null pointer exception.please advise
It's always a good idea to tell us where in the code you get that null pointer.

the more info we get from you, the better we'll be able to help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.