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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:11:21+00:00 2026-05-28T03:11:21+00:00

I am writing some Java code to read each line of a data file

  • 0

I am writing some Java code to read each line of a data file into an array list, shuffle that list, and then perform some further operations on the shuffled data. The problem is that, after shuffling, I am getting null elements for some of the elements in the array list.

//this file contains the data
String input = ...;
//this file contains the number of rows in the data file
String input2 = ...;
FileInputStream fstream2 = new FileInputStream(input2);
DataInputStream in2 = new DataInputStream(fstream2);
BufferedReader brIndex = new BufferedReader(new InputStreamReader(in2));

for(int i = 1; i <= N; i++) {
        FileInputStream fstream = new FileInputStream(input+(i+1)+".txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader brData = new BufferedReader(new InputStreamReader(in));
        num = Integer.parseInt(brIndex.readLine());

        ArrayList<String> temp = new ArrayList<String>();
        for(int j = 0; j < num; j++) {
            temp.add(brData.readLine());
        }
        brData.close();
        Collections.shuffle(temp);

        //read in shuffled data
        for(int j = 0; j < num; j++) {
            rowData = temp.get(j); ...
}

In executing the code, I get a NullPointerException after this when working with rowData. The original file has 17,169 rows (none of them are null). temp.size() is also 17,169 but when I printed temp out to the console, temp.get(j) was null for some j and not for others.

Can anyone explain to me why this is the case and how to avoid it?

  • 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-05-28T03:11:21+00:00Added an answer on May 28, 2026 at 3:11 am

    The problem might be there are not enough lines in the file as num.

    Try:

    List<String> temp = new ArrayList<String>();
    String line;
    while((line = br.readLine()) != null) {
        temp.add(brData.readLine());
    }
    

    You could also check for null elements in the list before you actually shuffle the list, as shuffle does not insert any new element to the list.

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

Sidebar

Related Questions

I am writing some Java code that authenticates to Active Directory using SASL GSSAPI.
How would I go about writing some code to allow access to a Java
I'm writing some acceptance tests for a java webapp that returns JSON objects. I
While writing some C code, I decided to compile it to assembly and read
I have some C++ code that is writing to a socket that is being
I am trying to read some objects from a file. The code works fine
I've been trying to write some very fast Java code that has to do
I'm writing some Java code in vim and really miss the feature afforded by
I'd like to write some (java) code that takes a PDF document, and creates
I am having trouble writing some Java code, which will create a container/folder in

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.