Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:10:03+00:00 2026-05-31T18:10:03+00:00

I’m pretty new to java and I’ve got a problem that I couldn’t solve

  • 0

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.

I have a class called Student which has fallowing data members and their get/set methods:

private String studentNumber;
private String firstName;
private String lastName;
private int age;
private String gender;
private String country;

and I created an array of instances as fallowing:

Student studentList[] = new Student(10);

I got a database(a text file) as fallowing

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

What I’m trying to do is getting all these informations to my class instances and I try to achieve this is fallowing:

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 < 10; i++) {
            System.out.printf(studentList[i].getStudentNumber(), " ",
                              studentList[i].getFirstName());

        }

    }

}

but it gives fallowing error:

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)

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

readFromFile() and processLine functions but I’m not sure if they’re workig as intended.

Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:10:04+00:00Added an answer on May 31, 2026 at 6:10 pm

    This line:

    studentList = new Student[10];
    

    creates an array of 10 elements. The value of each element isn’t a Student object; it’s a reference – and that reference will either be null, or a reference to a Student object (or a compatible type). Each element has a null value to start with.

    That line doesn’t create any Student objects.

    You need to create an instance of Student before you set its properties, e.g.

    if (scanner.hasNext()) {
      studentList[counter] = new Student();
      studentList[counter].setStudentNumber(scanner.next());
      ...
    

    Or:

    if (scanner.hasNext()) {
      Student student = new Student();
      student.setStudentNumber(scanner.next());
      ... fill in the other properties ...
      studentList[counter] = student;
      ...
    

    As a side note, this line:

    private static Student studentList[];
    

    … is not an idiomatic declaration. It’s valid, but most Java programmers would prefer to see:

    private static Student[] studentList;
    

    That way all the type information is kept in one place. (I’d also suggest using a List<Student> instead, and passing it into the method rather than using a static variable, but let’s not get ahead of ourselves…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.