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 8716315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:03:05+00:00 2026-06-13T06:03:05+00:00

My two classes are designed too create an array of StudentData objects (name, Date

  • 0

My two classes are designed too create an array of StudentData objects (name, Date Of Birth, and ID), that include a toString override to print out all variables. It then serializes the array and saves it to a file named studentdata.txt. It should then read the array from the file and reconstruct a new array object from this data, and print out each item in the array to the console.

Am getting this error when attempting to complie…

Exception in thread "main" java.io.NotSerializableException: Student
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at StudentData.main(StudentData.java:38)

Also am not sure how to properly loop through my array and call my toString method to print out to the console, would i be right in assuming I should use a for each loop? Like this?

                 //for (Student s : ???) {
                  //System.out.println(How do I call toString from here?);

My classes

import java.io.*;         //importing input-output files
    class Student
    {


   String name;                     //declaration of variables
 String DOB;
   int id;



   Student(String naam,int idno, String dob)          //Initialising variables to user data
   { 
          name=naam;
          id=idno;
          DOB=dob;

    }

   public String toString() {
       return name+"\t"+id+"\t"+DOB+"\t";
}



}

Number 2

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class StudentData                     //main class
{
  public static void main(String args[]) throws IOException                  //exception handling
  {
         System.out.println("Enter the numbers of students:");
         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
         int n=Integer.parseInt(in.readLine());


         Student[]  S=new Student[n];                      // array of objects declared and defined
         for (int i = 0; i < S.length; i++)       {

               System.out.println("Enter the Details of Student no: "+(i+1));             //reading data form the user
               System.out.println("Name: ");
               String naam=in.readLine();
               System.out.println("ID no: ");
               int idno=Integer.parseInt(in.readLine());
               System.out.println("DOB: ");               
               String dob=(in.readLine());


              S[i]=new Student(naam,idno,dob);                          

              File studentFile = new File("StudentData.txt");
              try {
              FileOutputStream fileOutput = new FileOutputStream(studentFile);
              ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
              objectOutput.writeObject(S);

              S = null;

              FileInputStream fileInput = new FileInputStream(studentFile);
              ObjectInputStream objectInputStream = new ObjectInputStream(fileInput);

                S = (Student[]) objectInputStream.readObject();
            } catch (ClassNotFoundException e) {

                e.printStackTrace();
            }

              //for (Student s : ???) {
                  //System.out.println();
              }

         }








     }
  • 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-06-13T06:03:06+00:00Added an answer on June 13, 2026 at 6:03 am

    To get rid of the NotSerializableException: Student exception, just make your Student class implement the Serializable interface. (Note that this is a marker interface, so you don’t have to implement any methods.)

    class Student implements Serializable {
    }
    

    To loop through the array S of Student objects, try this:

    for (Student st : S) {
        System.out.println(st);
    }
    

    I’d pick a more descriptive name for the array though (students would be better). If you declare the array as

    Student[]  students = new Student[n];
    

    then your for loop can be more readable.

    for (Student student : students) {
        System.out.println(student);
    }
    

    Note that you don’t have to explicitly call the toString method on an object of type Student. Java will do that implicitly for you when you use an object as the parameter to a method that expects a String.

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

Sidebar

Related Questions

If I have two classes that both implement an interface, but also inherit, do
I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, one that inherits from the other. The base class is
I've designed a class that has with two properties - as string type and
I'm writting some tests for classes that handles UDP multicast communication. I designed the
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes A and B generated by cxf-codegen-plugin from my WSDL. A
I have two classes Foo and Bar . The tables in the database look
Supposed I have two classes like this: public class Parent { public string Id
I have two classes, Item and SoldItem . SoldItem inherits from Item adding some

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.