Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:52:19+00:00 2026-06-02T21:52:19+00:00

I am reading a text file representing a maze pattern. each line is read

  • 0

I am reading a text file representing a maze pattern.
each line is read into a 1d char array and then the one 1d array is inserted into the 2d char array.

In the following method get a nullpointerexception at

        line = (input.readLine()).toCharArray();

private void fillArrayFromFile() throws IOException 
{

    BufferedReader input = new BufferedReader(new FileReader("maze.txt"));

    //Read the first two lines of the text file and determine the x and y length of the 2d array
    mazeArray = new char[Integer.parseInt(input.readLine())][Integer.parseInt(input.readLine())];

    char [] line = new char[10];

    //Add each line of input to mazeArray
    for (int x = 0; x < mazeArray[0].length; x++) 
    {
        line = (input.readLine()).toCharArray();
        System.out.print(x + " : ");
        System.out.println(line);
        mazeArray[x] = line;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:52:22+00:00Added an answer on June 2, 2026 at 9:52 pm

    The obvious answer is that input.readLine() is returning null, and since you are calling a method of an object that is pointing to nothing, you are getting a NullPointerException.

    The root of this problem though, is the discrepancy between your perception of rows and columns and that of the text file. Also, you are looping through the incorrect index, if I am reading your code correctly.

    Here is an example text file:

    4
    6
    a b c d
    b c d a
    a d c a
    b d b a
    c d a a
    b a b a
    

    A good way of thinking about this is to think about the number of rows and columns you have.

    For example, the text file above says “I have 4 columns and 6 rows”.

    That also means x ranges from 0 to 3 and y ranges from 0 to 5.

    In your terms, the x-length is the number of columns, and the y-length is the number of rows.

    Of course, remember that rows go across, and columns go downwards, assuming the indices are increasing.

    This is very important. Because x and y are essentially indices to a specific location in the grid.

    Although that is probably the cause, you also have a few semantic errors that I’ll fix below.

    BufferedReader input = new BufferedReader(new FileReader("maze.txt"));
    
    //Read the first two lines of the text file and determine the x and y length of the 2d array
    int mazeWidth = Integer.parseInt(input.readLine());  //A.K.A number of columns
    int mazeHeight= Integer.parseInt(input.readLine());  //A.K.A number of rows
    
    //          ranges: [0...3][0...5] in our example
    mazeArray = new char[mazeWidth][mazeHeight];
    
    char [] line;
    
    //Add each line of input to mazeArray
    for (int y = 0; y < mazeHeight; y++) 
    {
        line = (input.readLine()).toCharArray();
    
        if ( line.length != mazeWidth )
        {
            System.err.println("Error for line " + y + ". Has incorrect number of characters (" + line.length + " should be " + mazeWidth + ").");
        }
        else {
            System.out.print(y + " : ");
            System.out.println(java.util.Arrays.toString(line));
            mazeArray[y] = line;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here I'm reading text file which contains a integer in each line and I'm
I'm reading a text file line by line, and storing it into a NSArray.
When reading from a text file, one typically creates a FileReader and then nests
I'm reading a text file line by line and converting it into a string.
I am reading a text file line by line with VBA into a worksheet.
I am reading a text file line by line in C++. I'm using this
So I'm attempting to create a Befunge interperter and reading a text file into
I am reading a text file which I know its 38th line is Uncalibrated
Hi experts I'm looking to make a loop of reading a text file then
I am reading from a text file line by line. StreamReader reader = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.