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.
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.
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.
Then you need to find it.No I do not know what value has the NULL value.
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.
Just on a sidenote, you never create any ArrayList objects in your code. So I guess the NPE probably has to do with that.
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
Glad you got it working.
If you don't understand my answer, don't ignore it, ask a question.
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.
The String: "PETER" can not be converted to an int (using a normal radix).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)
It will work better if the String only contains numeric digits: 0 to 9I am trying to convert a string to a int.
That operator is for arrays, not ArrayLists.use the [] operator
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.