Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Printing a 2-dimensional array (http://www.go4expert.com/forums/printing-2-dimensional-array-t9995/)

cyrow 13Apr2008 23:24

Printing a 2-dimensional array

kinstown 45000
RoseHall 78000
man can 5675
Kinstown RoseHall 564
Uproad  Augustine 678

import java.io.*;
import java.util.*;

class City{
        private String cityName;
        private int population;
        City(String city, int pop){
                cityName  = city;
                population = pop;
        public String toString(){
                String str;
                str = "cityname    " + cityName+"\n"+
                      "Population  " + population+"\n";
                return str;
}//end of City class

class Distance{
        private String c1;
        private String c2;
        private int distance;
        Distance(String cc1, String cc2, int dis){
                c1 = cc1;
                c2 = cc2;
                distance = dis;
        public String toString2(){
                String str;
                str = "cityname    " + c1+"\n"+
                          "cityname 2  " + c2+"\n"+
                      "Distance    " + distance+"\n";
                return str;
}//end of Distance class

public class As11{
        public static void main(String[] args){

                City[]citty = new City[16];
                Distance[][]distance = new Distance[16][16];
                        Scanner in = new Scanner (new FileReader("cities.txt"));
                        String x  = in.next();
                        int count  = 0;
                        while(!(x.equals("####"))){//Read  first part of file into a one dimensional array
                            citty[count] = new City(x,in.nextInt());
                                x = in.next();
                }//end while
                int i =0;
                int k = 0;
                int count2 = 0;
                String y = in.next();
                while(!(y.equals("####"))){// Read second part of file into a two dimensional array.
                distance[i][k]= new Distance(y, in.next(),in.nextInt());
                                y = in.next();
        /*        for(int j = 0; j < count; j++){
                for(int l = 0; l < distance.length-1; l++){//For loop to print the two dimensional array
                        for (int h = 0; h < distance[l].length; h++){
                                System.out.println(distance[l][h].toString2());//Error Exception in thread "main" java.lang.NullPointerException
    at As11.main(As11.java:107)
                }catch(IOException e){
                        System.out.println("File Error");
                }//end catch
        }//end main
}//end class

//out put
cityname    man
cityname 2  can
Distance    5675

Exception in thread "main" java.lang.NullPointerException
    at As11.main(As11.java:107)

Process completed.

It prints the first record in the second part of the file then it generates the NullPointerException error. How can I rectity this problem.

All times are GMT +5.5. The time now is 00:08.