1
import java.util.*;
import java.io.*;

@SuppressWarnings("unused")
public class search 
{
public static String[] inputdata;
public static String[] routedata;

public static void main(String[] args)
{
    inputdata();
    routedata();

}

public static void inputdata()
{
    String strline;
    File data = new File("C:\\data\\sample_in.txt");
    int i=0;
    try
    {
        FileInputStream fstream = new FileInputStream(data);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        while ((strline = br.readLine()) != null)
        {
            //inputdata[i]=strline;
            System.out.println(strline);
            i++;
        }
    }
    catch (Exception e)
    {//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

public static void routedata()
{
    String strline;
    File routes = new File("C:\\data\\routes.txt");
    int i=0;
    try
    {
        FileInputStream fstream = new FileInputStream(routes);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        while ((strline = br.readLine()) != null)
        {
            //routedata[i]=strline;
            i++;
        }
    }
    catch (Exception e)
    {//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

}

I'm getting a null error for the file itself not being able to be read, am I doing something wrong? is the file not being read for some reason. I've searched around for awhile and can't seem to figure out what is wrong. any help would be appreciated. Also the inputdata[] string array is up at the top as a global variable but it is there, the problem I'm having is with the reading of the file everything else to my knowledge works. Thanks

2
  • Please provide the exact exception thrown, and which line it refers to.
    – aioobe
    Aug 17, 2011 at 15:02
  • 'I'm getting a null error for the file itself not being able to be read.' No you're not. There is no evidence here that your problem corresponds to 'null error for the file itself not being able to be read' or your title 'FileInputStream null error'. Both are impossible with the code you have posted. Also the DataInputStream in your I/O stack is completely pointless. Your question cannot be answered in its present form.
    – user207421
    Aug 18, 2011 at 10:58

3 Answers 3

2

Do this for your filename

File data = new File("C:\\data\\sample_in.txt");
5
  • It isn't an error that prevents the program from running, I should have clarified, the error null is resulting from the catch and is outputted as Error: null on the console Aug 17, 2011 at 15:14
  • Yes. The error you are catching does not have a message set. Do a e.printStackTrace() before you do your System.err.println()
    – Kal
    Aug 17, 2011 at 15:15
  • That is weird it is the string array, can it not store a whole line? Aug 17, 2011 at 15:18
  • What did you initialize the size of the string array? If the size is undetermined before you start populating it, it will be a better idea for you to use an arraylist instead of an array.
    – Kal
    Aug 17, 2011 at 15:20
  • @Freelancer799 -- That is the problem. Your arrays are not initialized. Initialize them like String[] inputdata = new String[100]; Note that this would only allow you to add 100 lines and beyond that, you would still get the same NPE.
    – Kal
    Aug 17, 2011 at 15:26
1
    public static void inputdata()
{
    String strline;
    int i=0;
    try
    {
        BufferedReader br = new BufferedReader(new FileReader("C:\\data\\sample_in.txt"));
        while ((strline = br.readLine()) != null)
        {
            inputdata[i]=strline;
            i++;
        }
    }
    catch (Exception e)
    {//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}
0
1

If this is a windows system you need to be using back slashes.

File data = new File("C:\\data\\sample_in.txt");

You are getting the null because its not finding the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.