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

  • SEARCH
  • Home
  • 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 8038441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:14:11+00:00 2026-06-05T03:14:11+00:00

I have been making a little program that needs to read a list of

  • 0

I have been making a little program that needs to read a list of golf courses that could be changeing and needs to be called when ever. Here is the code:

    public class Courses {
public String[] courselist;
public void loadCourses() throws IOException{
    int count = 0;
    int counter = 0;
    File f = new File("src//courses//courses.txt");
    BufferedReader reader = new BufferedReader(new FileReader(f));
    while(count<1){
        String s = reader.readLine();
        if(s.equalsIgnoreCase("*stop*")){
            reader.close();
            count = 5;

        }else{
            courselist[counter] = s;
            counter++;
        }
        s = "";
    }
}

}

And now this is what is in the txt file.

    RiverChase 
    Steward Peninsula 
    Lake Park 
    Coyote Ridge 
    *stop* 

Now when ever i start to run the program because i call the method instantly it gives me a throw exeption and it is because of the array. And i need to stay and array because i use it in a JComboBox. If you can help or fix the problem. Most likely im just doing it wrong, im a noob. Just help. Thanks in advance.
I know all the file reader and stuff works because it prints out to the system correct, i just need help writing it to the array repetedly.

  • 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-05T03:14:13+00:00Added an answer on June 5, 2026 at 3:14 am

    Change your code to assign a new array during loadCourses(), and add a call to loadCourses() to your constructor:

    public class Courses {
        public String[] courselist;
    
        public Courses() throws IOException {  // <-- Added constructor
            loadCourses();                     // <-- Added call to loadCourses
        }
    
        public void loadCourses() throws IOException {
            int count = 0;
            int counter = 0;
            File f = new File("src//courses//courses.txt");
            BufferedReader reader = new BufferedReader(new FileReader(f));
            List<String> courses = new ArrayList<String>(); // <-- A List can grow 
            while(true){
                String s = reader.readLine();
                if (s.equalsIgnoreCase("*stop*")){
                    break;
                }
                courses.add(s);
            }
            courseList = courses.toArray(new String[0]); // <-- assign it here
        }
    }
    

    This ensures that when you create an instance, it starts out life with the array initialised. Not only will you not get an error, but the data will always be correct (unlike other answers that simply create an empty (ie useless) array.

    Note that this code will work with any number of course names in the file (not just 5).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a game that needs a crosshair. I have been playing with
For some time i have been making more or less little games. One level
I'm making a little application that needs to know how long the user has
I have been making a little toy web application in C# along the lines
I've been studying and making little games for a while, and I have decided
I have been making some research in the domain of servers for a website
I have been using the Rails console a fair bit lately and its making
We have been given a little project (As part of my OS course) to
I have encountered a problem in my program that has me somewhat stumped. I
I have been making a volunteer sign up sheet for our hockey club. I

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.