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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:47:28+00:00 2026-05-15T01:47:28+00:00

I’m writing a little Java program (it’s an ImageJ plugin, but the problem is

  • 0

I’m writing a little Java program (it’s an ImageJ plugin, but the problem is not specifically ImageJ related) and I have some problem, most probably due to the fact that I never really programmed in Java before…

So, I have a Vector of Vectors and I’m trying to save it to a file and read it.

The variable is defined as:

Vector <Vector <myROI> > ROIs = new Vector <Vector <myROI> >();

where myROI is a class that I previously defined.

Now, to write the vector to a file I use:

void saveROIs()
    {
    SaveDialog save = new SaveDialog("Save ROIs...", imp.getTitle(), ".xroi");
    String name = save.getFileName();
    if (name == null)
        return;
    String dir = save.getDirectory();

    try
        {
        FileOutputStream fos = new FileOutputStream(dir+name);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(ROIs);
            oos.close();
        }
    catch (Exception e)
        {
        IJ.log(e.toString());
        }
    }

This correctly generates a binary file containing (I suppose) the object ROIs.

Now, I use a very similar code to read the file:

void loadROIs()
    {
    OpenDialog open = new OpenDialog("Load ROIs...", imp.getTitle(), ".xroi");

    String name = open.getFileName();
    if (name == null)
        return;
    String dir = open.getDirectory();

    try
        {
            FileInputStream fin = new FileInputStream(dir+name);
            ObjectInputStream ois = new ObjectInputStream(fin);
        ROIs = (Vector <Vector <myROI> >) ois.readObject(); // This gives error
            ois.close();
            }
    catch (Exception e)
        {
        IJ.log(e.toString());
        }
    }

But this function does not work.

First, I get a warning:

 warning: [unchecked] unchecked cast found   : java.lang.Object
 required: java.util.Vector<java.util.Vector<myROI>>
            ROIs = (Vector <Vector <myROI> >) ois.readObject();
                                                        ^

I Googled for that and see that I can suppress by prepending @SuppressWarnings("unchecked"), but this just makes things worst, as I get an error:

<identifier> expected
   ROIs = (Vector <Vector <myROI> >) ois.readObject();
        ^

In any case, if I omit @SuppressWarnings and ignore the warning, the object is not read and an exception is thrown

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: myROI

Again Google tells me myROI needs to implements Serializable. I tried just adding implements Serializable to the class definition, but it is not sufficient. Can anyone give me some hints on how to procede in this case? Also, how to get rid of the typecast warning?

  • 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-15T01:47:28+00:00Added an answer on May 15, 2026 at 1:47 am
    warning: [unchecked] unchecked cast found
    

    This is innocent and should indeed be fixed using @SuppressWarnings("unchecked") annotation.

    <identifier> expected
    

    This can impossibly be caused by adding the annotation. Probably you added it at the wrong place (should be right before method declaration), or you did something else which caused that the declaration of ROIs disappeared.

    java.io.NotSerializableException: myROI
    

    This means that the class identified by the given full qualified class name is not serializable. Expect from that it is not serializable, there’s another serious problem: that class is not inside a package. This is not necessarily the cause of the current problem, but this may lead to future problems because packageless classes are invisible for classes inside a package.

    To make a class serializable you need to let it implements Serializable and ensure that all fields are serializable as well. Primitives are by default serializable and String itself already implements it as you can read in its Javadoc. If a field really cannot be made serializable for some reason, then you need to declare it transient and if necessary override private void readObject() and private void writeObject() accordingly so that the unserializable object can be written/read to the steam anyway. To the point, you just need to save its state in some way that exactly the same state can be restored afterwards.

    E.g.

    public class SerializableObject implements Serializable {
    
        private transient UnserializableObject unserializableObject;
    
        private void writeObject(ObjectOutputStream oos) throws IOException {
            oos.defaultWriteObject();
            oos.writeObject(unserializableObject.getSerializableProperty());
            oos.writeObject(unserializableObject.getAnotherSerializableProperty());
            // ...
        }
    
        private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
            ois.defaultReadObject();
            unserializableObject = new UnserializableObject();
            unserializableObject.setSerializableProperty((SomeSerializableObject) ois.readObject());
            unserializableObject.setAnotherSerializableProperty((AnotherSerializableObject) ois.readObject());
            // ...
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.