Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 32

Thread: reading files

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default reading files

    import java.io.*;
    import java.util.Formatter;
    import java.util.Scanner;
    import java.util.Vector;
     
    public class read_file 
    {
    	private Formatter make_file;
    	private Vector<String>names;
    	private int counter;
    	private Vector<Integer>age;
     
    	void FileInput()
    	{
    		File input = new File("example.txt");
     
    		if(!input.exists())
    		{
    			try{make_file = new Formatter("example.txt");System.out.println("A file had to be made, named "+input);}
    			catch(IOException e){System.out.println("File was unable to be made");}
    		}
    		else
    		{
    			try 
    			{
    			Scanner reader = new Scanner(new FileInputStream(input));
     
    			while(reader.hasNext())
    			{
    				names.add(reader.nextLine());
    				age.add(reader.nextInt());
    				counter++;	
    			}
     
    			}catch(Exception e){System.out.println("The file could not be read");}
    		}
    	}
     
    	void PrintFile()
    	{
     
    		for(int i=0;i<counter;i++){System.out.println(names.get(i)+" "+age.get(i));}
    	}
     
    	public static void main(String[] args)
    	{
    		read_file r = new read_file();
     
    		r.FileInput();
    		r.PrintFile();
     
    	}
     
    }

    I am playing with different way to take input from file it is not working.
    }catch(Exception e){System.out.println("The file could not be read");}
    keeps popping up. The file is being created though. Is a vector a good way to handle this? I read some bad things about vectors in Java but I used them all the time in C++. What is the deal with that?

    The files look like:

    name age
    name age
    name age
    etc........


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    Add a call to the printStackTrace() method to the catch block so that the location and reason for the exception is shown.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    java.lang.NullPointerException
    at read_file.FileInput(read_file.java:30)
    at read_file.main(read_file.java:49)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    java.lang.NullPointerException
    at read_file.FileInput(read_file.java:30)
    There was a null value used on line 30 when it was executed. Look at line 30, find the variable with the null value and then backtrack to find out why the variable was null.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    NULL??? I didn't even push anything into the vector... Do I need to create the Vector again "new"? I tried to do that and I got a bunch of errors.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    What variable was null? Was that variable ever assigned a value?

    You can define a variable in one place in the code (say as a class instance variable)
    and assign it a value in another place like in the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    I know that but why would I initialize a vector. I used it because it is flexible and can grow in size as I wish. If not I would have made an array. I am beginning to think Vectors work differently in Java then C++.

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: reading files

    A Vector is an object like any other. All objects must be initialized before you use them, there are no exceptions.

    By the way, Vector is a very old and outdated class. A better choice is the ArrayList class.

  9. #9
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    Ahhhh arrayList, never heard of this before. I will use this, thanks. So I must do something like ArrayList<String> list = new ArrayList<String>(); before I can use it?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    work differently in Java then C++.
    Yes. Java's object reference variables are similiar to C++s pointers.
    Defining a reference variable does not create an object. You need a new statement to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: reading files

    Quote Originally Posted by jocdrew21 View Post
    Ahhhh arrayList, never heard of this before. I will use this, thanks. So I must do something like ArrayList<String> list = new ArrayList<String>(); before I can use it?
    This would be better coding practice as some might say:
    List<String> someList = new ArrayList<>();

  12. #12
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    import java.io.*;
    import java.util.ArrayList;
    import java.util.Formatter;
    import java.util.List;
    import java.util.Scanner;
     
     
    public class read_file 
    {
    	private Formatter make_file;
    	private ArrayList<String>names;
    	private int counter;
    	private ArrayList<Integer>age;
     
    	// error saysjava.lang.NullPointerException, java.lang.NullPointerException
    	//at read_file.FileInput(read_file.java:37)
    	//at read_file.main(read_file.java:56)
     
    	//tried to make a constructor and create new string objects but this doesn't work either.
    	public read_file(){/*names = new ArrayList<String>(); age= new ArrayList<Integer>();*/} 
     
    	void FileInput()
    	{
    		File input = new File("example.txt");
     
    		if(!input.exists())
    		{
    			try{make_file = new Formatter("example.txt");System.out.println("A file had to be made, named "+input);}
    			catch(IOException e){System.out.println("File was unable to be made");}
    		}
    		else
    		{
    			try 
    			{
    			Scanner reader = new Scanner(new FileInputStream(input));
     
    			while(reader.hasNextLine())
    			{
     
    				names.add(reader.nextLine());
    				age.add(reader.nextInt());
    				counter++;	
    			}
    			reader.close();
    			}catch(Exception e){System.out.println("The file could not be read "+e);e.printStackTrace();}
    		}
    	}
     
    	void PrintFile()
    	{
     
    		for(int i=0;i<counter;i++){System.out.println(names.get(i)+" "+age.get(i));}
    	}
     
    	public static void main(String[] args)
    	{
    		read_file r = new read_file();
     
    		r.FileInput();
    		r.PrintFile();
    	}
    }

    I am still having issues with this code. I am trying to learn more about file handling but keep getting this error. I stopped using vectors and am now using ArrayList and from what I have read it is a linked list right?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    keep getting this error
    Please copy the full text of the error message and paste it here. It has important info about the error.

    BTW your naming conventions are reversed. Class names should begin with uppercase. method names with lowercase.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    java.lang.NullPointerException
    java.lang.NullPointerException
    at read_file.FileInput(read_file.java:37)
    at read_file.main(read_file.java:56)

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    There is a variable with a null value when the source at line number 37 is executed. Look at the line and find the variable that has the null value and then backtrack in the code to see why that variable does not have a non-null value.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    public read_file(){names=null;age=null; }

    That is what I am trying to do. I do not know what the heck is going on.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    That line of code wouldn't cause a NullPointerException.
    Are you sure that is the line where the exception happens? Check again.

    BTW putting multiple statements on one line makes the code harder to read.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    The issue is on line 38 like the error says.
    I was not getting any errors until I put names and numbers into the file.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    Sorry, I haven't see the error message with the error on line 38. The posted error message says it happened on line 37.

    What is on that line? What variable on that line has the null value?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    I moved a line on my compiler so now 37 was 38, we are talking about the same error.

    I know what you are trying to do here and I really appreciate it. I have a solid understanding in C and C++ and am trying to learn Java on my own. Therefore I understand the diligence required to learn a high level language. I am not here looking for a freebee which I am sure is a common practice around here.

    We have established null is the issue. I made the values null in the constructor, didn't work, Tried to make a new object and initialize it, nope didn't work either.

    Not liking how vague the errors are in Java.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    What variable has the null value?

    Did you read post#10?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    AS I said I created an new object already and it didn't work either. I even tried to create it in my constructor.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    I can't say what is wrong without seeing the code.
    It sounds like you could be defining two variables with the same name at different scopes, giving a value to one and then trying to use the other which still has the null default value.
      String aStr = null;     // first definition
      { 
          String sStr = "avalue";  // second definition
      }  //  aStr with value goes out of scope here
       // here aStr's value is null
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: reading files

    Post 12 has the code.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading files

    Did you find the variable with the null value?
    What variable was it?

    You are avoiding answering that question.

    To solve a NPE:
    first find the variable with the null value
    then backtrack to see why it has a null value.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem reading in the files
    By Skynet928 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 28th, 2013, 01:05 PM
  2. Reading files and writing files
    By ProgrammerNewbie in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 3rd, 2012, 12:13 AM
  3. reading .txt files
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 8th, 2012, 12:47 PM
  4. Reading many files using a scanner
    By jayjames90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2009, 04:35 PM