Note that there are some explanatory texts on larger screens.

plurals
  1. POReading From File in Java and Null Pointer Exception
    primarykey
    data
    text
    <p>I'm pretty new to java and I've got a problem that I couldn't solve by myself. I googled the exception but problem is too specific as far as I can understand so I find myself here. Here is my problem.</p> <p>I have a class called Student which has fallowing data members and their get/set methods:</p> <pre><code>private String studentNumber; private String firstName; private String lastName; private int age; private String gender; private String country; </code></pre> <p>and I created an array of instances as fallowing:</p> <pre><code>Student studentList[] = new Student(10); </code></pre> <p>I got a database(a text file) as fallowing</p> <pre><code>081935 Cengiz rrrrr Male 21 Turkey 082935 Ayşe aaaaa Female 22 England 083935 Onur bbbbb Male 23 Germany 084935 Fatma ccccc Female 24 Cyprus 085935 Ali dddd Male 21 China 086935 Zehra eeee Female 22 Denmark 087935 Murat ffff Male 25 France 088935 Selin ggggg Female 26 Japan 086935 Cengiz hhhh Male 20 Korea 080935 Damla qqqqqq Female 19 Iran </code></pre> <p>What I'm trying to do is getting all these informations to my class instances and I try to achieve this is fallowing:</p> <pre><code>import java.io.*; import java.util.Scanner; public class StudentTracker { private static int counter = 0; private static Student studentList[]; public static void readFromFile() throws FileNotFoundException { File file = new File("Database.txt"); Scanner scanner = new Scanner(new FileReader(file)); try { while (scanner.hasNextLine()){ processLine(scanner.nextLine()); } } finally { scanner.close(); } } public static void processLine(String line) { Scanner scanner = new Scanner(line); scanner.useDelimiter(" "); if (scanner.hasNext()) { studentList[counter].setStudentNumber(scanner.next()); studentList[counter].setFirstName(scanner.next()); studentList[counter].setLastName(scanner.next()); studentList[counter].setGender(scanner.next()); studentList[counter].setAge(Integer.parseInt(scanner.next())); studentList[counter].setCountry(scanner.next()); counter++; } else { System.out.println("Empty or invalid line. Unable to process."); } } public static void main(String[] args) throws FileNotFoundException { studentList = new Student[10]; readFromFile(); for(int i = 0; i &lt; 10; i++) { System.out.printf(studentList[i].getStudentNumber(), " ", studentList[i].getFirstName()); } } } </code></pre> <p>but it gives fallowing error:</p> <pre><code>Exception in thread "main" java.lang.NullPointerException at StudentTracker.processLine(StudentTracker.java:28) at StudentTracker.readFromFile(StudentTracker.java:16) at StudentTracker.main(StudentTracker.java:68) </code></pre> <p>by the way couldn't find a function like C-scanf which gets the input until first white space so I find another way to parse the strings from line with</p> <p>readFromFile() and processLine functions but I'm not sure if they're workig as intended.</p> <p>Thank you in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload