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.

Results 1 to 3 of 3

Thread: Unable to Serialize arrayList of Object even after implementing the class

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Unable to Serialize arrayList of Object even after implementing the class

    So I was trying to serialize an arraylist of object that hold names but it gives me unserializable error although implemented it in both classes too after it failed to run when i implemented it the main class only.
    I will paste the code and the error message
    main class
    package serial;
     
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class main implements Serializable {
     
     
    	public static void main(String[] args) throws IOException {
    		Scanner input = new Scanner(System.in);
    		ArrayList<names> name= new ArrayList<names>();
     
     
    		{
    			name.add(new names());
    			File File = new File("C:\\Users\\kuchuk borom debbarm\\Desktop\\test\\name.txt");
    			if(File.exists()) {
     
    			}
    			else {
    				File.createNewFile();
    			}
    			ObjectOutputStream fileF= new ObjectOutputStream(new FileOutputStream(File));
    			fileF.writeObject(name); //this is where the code fails
    			fileF.close();
    			}
    		}
     
    }
    secondary class
    package serial;
     
    import java.io.Serializable;
    import java.util.Scanner;
     
    public class names implements Serializable {
    	Scanner input=new Scanner(System.in);
    public names() {
    	System.out.println("uy");
    String names=input.nextLine();
    }
    }
    here is the error
    uy
    kuku
    Exception in thread "main" java.io.NotSerializableException: java.util.Scanner
    	at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
    	at java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1553)
    	at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1510)
    	at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
    	at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
    	at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
    	at java.base/java.util.ArrayList.writeObject(ArrayList.java:896)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    	at java.base/java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1130)
    	at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1497)
    	at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
    	at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
    	at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
    	at serial.main.main(main.java:35)
    Last edited by Norm; July 13th, 2019 at 02:15 PM. Reason: Changed \ to / in ending code tag

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,682
    Thanks
    53
    Thanked 2,421 Times in 2,374 Posts

    Default Re: Unable to Serialize arrayList of Object even after implementing the class

    Exception in thread "main" java.io.NotSerializableException: java.util.Scanner
    Look at the API doc for this class: java.io.NotSerializableException
    It explains the problem.

    Note: Java naming conventions says that class names should begin with an UPPERCASE letter: Names and Main
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Unable to Serialize arrayList of Object even after implementing the class

    Ty I did look it up and found that scanner is messing things up
    N about naming , I didn't care to follow the rule because I was just testing things out I do follow the naming rule in my main codes

    --- Update ---

    Ty I did look it up and found that scanner is messing things up
    N about naming , I didn't care to follow the rule because I was just testing things out I do follow the naming rule in my main codes

Similar Threads

  1. Replies: 2
    Last Post: April 8th, 2014, 12:05 PM
  2. Write a driver program implementing an ArrayList of CD object
    By LeahH in forum Object Oriented Programming
    Replies: 10
    Last Post: April 20th, 2013, 07:35 AM
  3. Implementing a Linked List into a Queue class?
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 9th, 2012, 12:07 AM
  4. How to implement GregoraianCalendar which is subclass of Calender?
    By Meirikai in forum Object Oriented Programming
    Replies: 4
    Last Post: October 30th, 2012, 03:15 PM
  5. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM

Tags for this Thread