I keep getting a null pointer on this assignment and I figure that I can't fix it because my logic is deeply flawed somewhere.

Here is the code that I have:

import java.util.*;
import java.io.*;
public class Project1 {



	public static class MapPoint {
		private String elevation;
		private String lattitude;
		private String longitude;
		
		public MapPoint() {
			elevation = "";
			lattitude = "";
			longitude = "";
		}
		//set
		public void setElevation(String s) {
			
			
			elevation = s;
		}
		public void setLattitude(String s) {
			lattitude = s;
		}
		public void setLongitude(String s) {
			longitude = s;
		}
		//get
		public String getElevation() {
			return elevation;
		}
		public String getLattitude() {
			return lattitude;
		}
		public String getLongitude() {
			return longitude;
		}
		
	}




	public static void main(String[] args) {
		
		try {
			String numRows = "";
			String numCols = "";
			boolean status = true;
			
			File input = new File("input.txt");
			Scanner infile = new Scanner(input);
			while(status == true) {
				
				numRows = infile.next();
				numCols = infile.next();
				int numRowsInt = Integer.parseInt(numRows);
				int numColsInt = Integer.parseInt(numCols);
				
				if(numRowsInt == 0 && numColsInt == 0) {status = false;}//kills the loop when end of file signifier reached
				else {
					MapPoint[][] myMap = new MapPoint[numRowsInt][numColsInt];
					for(int i = 0; i < numRowsInt; i++) {
						for(int j = 0; j < numColsInt; j++) {
							MapPoint mapPoints = new MapPoint();
							mapPoints.setElevation(infile.next());
							mapPoints.setLattitude(infile.next());
							mapPoints.setLongitude(infile.next());
						}
					}
					//test loop
					
					for(int i = 0; i < numRowsInt; i++) {
						for(int j = 0; j < numColsInt; j++) {
							System.out.println(myMap[i][j].getElevation() + " " + myMap[i][j].getLattitude() + " " + myMap[i][j].getLongitude());						
						}
					}
					
					
				}

				status = false;

			}

			//System.out.println(numRows);
			//System.out.println(numCols);




		


		}

		catch(FileNotFoundException e) {
			System.out.println("There is no file present.");
		}
		catch(ArrayIndexOutOfBoundsException f) {
			f.printStackTrace();
			f.getMessage();
			f.toString();
		}
	}
}

Any help would be appreciated, and explanation on why my techniques is wretched is welcome.

Member Avatar for ztini

Which line are you getting a null pointer on?
Also, can you provide an example of what is in "input.txt"?

include

try{
              //YOUR CODE,READING FROM INPUT FILE
}catch(NullPointerException str){
                                    JOptionPane.showMessageDialog(null,"Error:"+str,"Error", JOptionPane.ERROR_MESSAGE);
}

Line 76 is the where the error is being thrown.

3 3
90 35d43m9s 45d45m19s 8 36d46m19s 51d25m19s 45 36d46m19s 11d25m39s  
7 35d23m9s 25d43m9s 25 25d43m9s 35d33m39s 30 32d33m9s 17d33m22s
6 15d43m19s 25d43m29s 108 39d33m40s 25d23m29s 565 25d23m29s 15d41m19s
4 4
100 15d43m19s 25d43m29s 5 39d33m40s 25d23m29s 6 25d23m29s 15d41m19s 90 32d33m9s 17d33m22s
8 35d43m9s 45d45m19s 7 36d46m19s 51d25m19s 10 36d46m19s 11d25m39s 5 22d46m39s 12d25m29s
80 35d23m9s 25d43m9s 7 25d43m9s 35d33m39s 6 32d33m9s 17d33m22s 2 39d38m29s 37d23m12s
9 12d53m49s 18d33m22s 8 44d33m9s 22d33m22s 97 38d38m9s 47d36m26s 5 52d53m9s 57d53m22s
0 0

Explanation:first line is number of rows and columns. Second has 3 strings for each column. Elevation, lat and long.
End of file signified by 0 0.

Member Avatar for ztini

That is a strange input file indeed.

1) Enclose your input with try/catch. I suggest using a StringBuffer, then you can read each line individually.

2) For each line you take from input, use something like this:
String[] tokens = inFile.next().split(" "); It will probably be much easier to break apart your data by using an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.