Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

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
0
gudii9
Asked:
gudii9
3 Solutions
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
Sharon SethCommented:
Seems you did not recompile after making the changes ;)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now