Freelancer799
Freelancer799

Reputation: 15

FileInputStream null error

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

Upvotes: 1

Views: 4004

Answers (3)

Jarod D
Jarod D

Reputation: 145

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.

Upvotes: 1

Shawn
Shawn

Reputation: 7365

    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());
    }
}

Upvotes: 1

Kal
Kal

Reputation: 24910

Do this for your filename

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

Upvotes: 2

Related Questions