我继续得到一个空指针异常错误

时间:2015-09-22 23:43:39

标签: java nullpointerexception

我是java的新手,任何帮助和/或提示都会很棒。我运行我的程序,并在我的getImage函数中不断给我一个空指针异常错误。我已经搞砸了指向问题所在的那条线,但似乎没有什么能解决它。该行以Scanner输入开头。 Eclipse没有在该行显示错误,所以它应该工作但我一直得到null异常错误。一切都编译并运行,直到程序在运行时到达getImage函数。那时它给了我错误。

    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();
        }

    }

0 个答案:

没有答案