i'm new to java and any help and/or tips would be awesome. I run my program and keeps giving me a null pointer exception error in my getImage function. I have messed with the line that it points to where the issue is but nothing seems to fix it. The line starts with Scanner input. Eclipse doesn't show an error on that line so it should work yet i keep getting the null exception error. Everything compiles and runs up until the program reaches the getImage function while its running. At that point it gives me the error.
import java.util.*;
import java.io.*;
public class tileMap
{
public static int MAXSIDE = 100;
private static String fileName = null;
private static int imageHeight =0;
private static int imageWidth=0;
private static char[][] buffer = new char [MAXSIDE][MAXSIDE];
private static Object If;
/*
FUNCTION NAME: Main ;
INPUT: none.
OUTPUT: a message to the user of this program, all of the
prompts and a final display according to user specifications.
PRECONDITIONS: None.
POSTCONDITIONS: Variables and calls made according to users input
output set to start on a new line.
CALLERS: None.
CALLES: askPermission, getParameters(), getImage(), and doTileJob().
*/
public static void main(String args[]) throws FileNotFoundException
{
while(askPermission())
{
getParameters();
getImage();
printImage();
}
}
/*
FUNCTION NAME: askPermission ;
INPUT: none.
OUTPUT: a message to the user of this program.
PRECONDITIONS: output set to start on a new line.
POSTCONDITIONS: variable response has user's answer stored in it.
CALLERS: the main program
CALLES: None.
*/
public static boolean askPermission()
{
System.out.println("Would you like to print an image in a file?");
System.out.println("If yes, type a 'y', else type a 'n':");
Scanner answer = new Scanner(System.in);
String answer1 = answer.nextLine();
if(answer1.equals("n"))
{
return false;
}
return true;
}
/*
FUNCTION NAME getParameters ;
INPUT: the file name, number of tiles across and down.
OUTPUT: message "Getting Image".
PRECONDITIONS: the variable response has 'y' in it.
POSTCONDITIONS: variables set with the values entered by user.
CALLERS: the main program
CALLEES: none
*/
static void getParameters() throws FileNotFoundException
{
System.out.println("Please enter the file name: ");
Scanner filename = new Scanner(System.in);
String input = filename.nextLine();
Scanner fileName = new Scanner(new File(input));
imageHeight = fileName.nextInt();
imageWidth = fileName.nextInt();
}
/*
FUNCTION NAME: getImage ;
INPUT:the file name and the height and width of the pattern to be made.
OUTPUT: the message "Getting Image".
PRECONDITIONS: array for image declared, the variables fileName,
imageHeight and imageWidth set with proper values.
POSTCONDITIONS: the image is stored in the array.
CALLERS: the main program
CALLEES: none
*/
public static void getImage() throws FileNotFoundException
{
buffer = new char[imageHeight][imageWidth];
System.out.println("Getting Image...");
Scanner input = new Scanner(new File(fileName));
input.nextLine();
String line;
for (int i=0; i<imageHeight; i++) {
line = input.nextLine();
for(int j=0; j<imageWidth; j++){
buffer[i][j] = line.charAt(j);
}
System.out.println();
input.close();
}
}
/*
FUNCTION NAME: printImage
INPUT:the buffer with the image and the height and width of the
pattern to be made
OUTPUT: the patterns structured according to users input.
PRECONDITIONS: All of the variables are set and pattern is stored in'buffer'.
POSTCONDITIONS: Output displayed according to users input.
CALLERS: the main program
CALLEES: none
*/
// This function uses for loops to display the images. The inner most for loop prints one line of the picture.
public static void printImage() throws FileNotFoundException
{
buffer = new char [imageHeight][imageWidth];
Scanner input = new Scanner(new File(fileName));
String line;
for (int i=0; i<imageHeight; i++) {
line = input.nextLine();
for(int j=0; j<imageWidth; j++){
buffer[i][j] = line.charAt(j);
}
System.out.println();
}
input.close();
}
}