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 2 of 2 FirstFirst 12
Results 26 to 32 of 32

Thread: reading files

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

    Default Re: reading files

    No I do not know what value has the NULL value. I created a ArrayList twice, an int and string, set them a private. Later I used them on line 38 or 37. I took in the value from the scanner and that is where the error is.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reading files

    No I do not know what value has the NULL value.
    Then you need to find it.
    How many variables are on line 37?
    Add a println() statement just before line 37 that prints out the values of ALL the variables used on line 37 so you can see which variable is null.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: reading files

    Just on a sidenote, you never create any ArrayList objects in your code. So I guess the NPE probably has to do with that.

  4. #29
    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.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(){ } 
     
    	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));
    			names = new ArrayList<String>();
    			age = new ArrayList<Integer>();
     
    			while(reader.hasNextLine())
    			{
    				names.add(reader.next());
    				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();
    	}
    }

    Thanks for putting up with me Norm

    I am picking up what you were putting down, it works

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reading files

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: reading files

    void PrintFile()
    	{
     
    		for(int i=0;i<counter;i++)
    		{
    			System.out.println(names.get(i)+" "+age.get(i));
    		}
     
    		for(int j=0; j<counter; j++)
    		{
    			int value = Integer.parseInt(names.get(j));
     
    			System.out.println("The integer value of this string is: "+value);
    		}
    	}

    I am continuing to try new things and just experiment and I am getting the following errors:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "PETER"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at read_file.PrintFile(read_file.java:58)
    at read_file.main(read_file.java:69)

    I am trying to convert a string to a int.

    At first I wanted to simply use the [] operator in a loop for each char while taking the (int) value of it and adding it (+=) to a variable.
    I guess ArrayList does not quite work that way.

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reading files

    Exception in thread "main" java.lang.NumberFormatException: For input string: "PETER"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    The String: "PETER" can not be converted to an int (using a normal radix).

    I am trying to convert a string to a int.
    It will work better if the String only contains numeric digits: 0 to 9

    use the [] operator
    That operator is for arrays, not ArrayLists.
    Use a method like get() to access the contents of an object like an ArrayList
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

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