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 8098139
In Process

The Archive Base Latest Questions

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

Exception in thread main java.lang.NullPointerException at Library.loadBooks(Library.java:179) at UseLibrary.main(UseLibrary.java:105) This error makes me crazy!

  • 0
Exception in thread "main" java.lang.NullPointerException
    at Library.loadBooks(Library.java:179)
    at UseLibrary.main(UseLibrary.java:105)

This error makes me crazy!

public void loadBooks(String s) throws IOException{
    String str[] = new String[6];
    String inFName = ".//" + s ;
    BufferedReader input = new BufferedReader(new FileReader(inFName));
    int x;
    double y;
    String line = "";
    while(line != null){

        for(int i=0; i<6; i++){
            str[i] = new String();
            line = input.readLine();
            str = line.split("[-]");
            x = Integer.parseInt(str[1]);
            y = Double.parseDouble(str[2]);
            Book a = new Book(str[0], x, y, str[3], str[4], str[5]);
            add(a);
        } 

    }
}

What’s the problem with this code?

I initialize the array, but it didn’t run!

Update 1

In save.txt I have is

1 Don Knuth-290-23.45-The Art of Programming with Java-HG456-Engineering-5 
2 A. Camp-400-13.45-An Open Life-HSA234-Philosophy-1 
3 James Jones-140-12.11-Oh, Java Yeah!-SDF213-Science Fiction-2 
4 J. Campbell-250-32.45-An Open Life-JH45-Science-3 
5 Mary Kennedy-230-56.32-Intro to CS Using Java as the Language-USN123-Science-4
  • 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-05T21:59:13+00:00Added an answer on June 5, 2026 at 9:59 pm

    The problem I think is

    You have 5 or less lines in your saved.txt and because of for loop for the 6th iteration as there is no data for line, you are getting NullPointerException.

    Please follow below step and let me know what you get…

    Instead of

    while(line != null){
    
        for(int i=0; i<6; i++){
            str[i] = new String();
            line = input.readLine();
            str = line.split("[-]");
            x = Integer.parseInt(str[1]);
            y = Double.parseDouble(str[2]);
            Book a = new Book(str[0], x, y, str[3], str[4], str[5]);
            add(a);
        } 
    
    }
    

    Use

    while ((line = input.readLine()) != null) {
            str = line.split("[-]");
            Book a = new Book(str[0], Integer.parseInt(str[1]), Double.parseDouble(str[2]), str[3], str[4], str[5]);
            add(a);
    }
    

    Let me know if you still get any problem.

    Update 1

    Per your update,

    In save.txt I have is

    1 Don Knuth-290-23.45-The Art of Programming with Java-HG456-Engineering-5 
    2 A. Camp-400-13.45-An Open Life-HSA234-Philosophy-1 
    3 James Jones-140-12.11-Oh, Java Yeah!-SDF213-Science Fiction-2 
    4 J. Campbell-250-32.45-An Open Life-JH45-Science-3 
    5 Mary Kennedy-230-56.32-Intro to CS Using Java as the Language-USN123-Science-4
    

    As you see there are 5 lines in your file, for 6th iteration you are getting NullPointerException.

    Also read, how to print content of files in Java.

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

Sidebar

Related Questions

Exception in thread main java.lang.NullPointerException at com.hibernate.UserDAO.findAll(UserDAO.java:154) at test.main(test.java:12) I am getting this error
I'm having a problem where I receive this error: Exception in thread main java.lang.NullPointerException
I'm getting the error: Exception in thread main java.lang.NullPointerException at com.deanchester.minos.utils.DatabaseManager.createTables(DatabaseManager.java:59) at com.deanchester.minos.tests.testAddContestantMethod.main(testAddContestantMethod.java:21) I
When running next code I get a error Exception in thread main java.lang.NullPointerException I
the program gives following exception: Exception in thread main java.lang.NullPointerException at myclasses.BubbleSort.run(BubbleSort.java:42) at acm.program.Program.runHook(Program.java:1519)
I get this exception: Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 100 at Vindu.<init>(setevelger.java:64) at setevelger.main(setevelger.java:22)
Exception in thread main java.lang.NullPointerException at java.io.Reader.<init>(Unknown Source) at java.io.InputStreamReader.<init>(Unknown Source) //at InputStreamReader inStream
Exception in thread Thread-0 java.lang.NullPointerException at org.apache.http.impl.nio.reactor.AbstractIOReactor.closeActiveChannels(AbstractIOReactor.java:532) at org.apache.http.impl.nio.reactor.AbstractIOReactor.hardShutdown(AbstractIOReactor.java:564) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.doShutdown(AbstractMultiworkerIOReactor.java:411) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:340) at
Exception in thread Thread-2 java.lang.NumberFormatException: For input string: 3 int test = Integer.parseInt(result[0]); This
I have the following error Exception in thread main javax.naming.NameNotFoundException: CounterBean not bound trying

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.