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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:03:11+00:00 2026-06-13T03:03:11+00:00

I encounter a java.lang.NullPointerException error when running my Java program, and after reading up

  • 0

I encounter a java.lang.NullPointerException error when running my Java program, and after reading up on it I think I understand what the error means, but I’m still not sure how to fix it.

import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
public class readFile {


public static ArrayList<String> pullFirst(String fileName) throws IOException{
    String filen = "C:\\Users\\Steven\\Desktop\\Tests\\wunderground\\outputTweetsWeather.txt";
    ArrayList<String> arl = new ArrayList<String>(); // java 6 ArrayList 

    FileInputStream fs = new FileInputStream(filen); // this is how you access a file in java
    DataInputStream din = new DataInputStream(fs);
    BufferedReader bin = new BufferedReader(new InputStreamReader(din));

    String line;    
    while(!(line = bin.readLine()).startsWith("String index out of range")){ // read each line in the file while they exist
        arl.add(line);                // add them to the array list
    }

    return arl;                                  // return the array list
}

public static void main(String[] args, int j) {

    // this is just demo code to prove it works so you can check the output.
    try{
    ArrayList<String> rVal = pullFirst("testFile.txt");
        for(String a : rVal){
            //System.out.println(a + "\n"); // insert write to file code here
           if(a.startsWith("+t")){
       //          System.out.println(a);
           }
        }
        int i = 36;


        PrintStream out5 = new PrintStream(new FileOutputStream("forpopUp1.txt", true));

        System.setOut(out5);
     System.out.println(rVal.get(2 + j*i)); // Display Value


    }catch(Exception e){
        System.out.print("Problem in readFile" + e);
    }

}
}

Now I’m not sure if it’s to do with the fact that my main line is

public static void main(String[] args, int j) {

But, as far as I’m aware I have to have it this way, as this class is executed after an if condition from another class is ran, using the line

readFile.main(args, counterForreadFile);

This is the result of the StackTrace:

java.lang.NullPointerException
at readFile.pullFirst(readFile.java:22)
at readFile.main(readFile.java:33)

Any help would be appreciated!

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

    I believe the error is when the file you read was exhausted:

    While reading the file:

    while(!(line = bin.readLine()).startsWith("String index out of range")){ 
      ...
    }
    

    When the file is exhausted the BufferedReader will return null.

    When you do (line=bin.readLine()).startsWith(..) you actually try to invoke startsWith() on null in this cases – and thus the error.

    To solve this, you should iterate as:

    while ((line = bin.readLine()) != null) { 
      if (line.startsWith("String index out of range")) break; 
      //rest of the code
    }
    

    This provide you null safety when the reader is exhausted.

    While it is definetly an issue – I cannot be sure it is the exact problem you are encountering until you provide a full stack trace.


    P.S. As I already said in comments – getting the stack trace (which provides more information on the issue) you can add to your catch block, so it will be something like that:

    }catch(Exception e){
        System.out.print("Problem in readFile" + e);
        e.printStackTrace();
    }
    

    An alternative is to declare main() as throws Exception, and remove the catch block (and try block as well).

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

Sidebar

Related Questions

I encounter error like this in my sql in running my program, How can
I've encounted an error trying to implement Apache commons-compress in my Java application: java.lang.ClassNotFoundException:
Encounter following lines of code, but couldn't understand it. What is this (/ ...
Following is a snippet that throws java.lang.NullPointerException . else if(jRadioButton2.isSelected()) { // chrome selected
Hi I am creating a program and I encountered an error when running it.
I'm trying to implement Java server and C client. I get java.lang.NullPointerException when I
I'm no Java expert but I can usually figure out the errors I encounter
I was trying to do a Vigenere cipher program in java, and I encountered
Hi I am beginner in Java security, i encounter the following issue, when calling
[ UPDATE : After discussion on the Glassfish forums/ML at http://forums.java.net/jive/thread.jspa?messageID=480532 a bug was

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.