We help IT Professionals succeed at work.

Problems with recreating a saved object using ObjectInputStream

mark_667
mark_667 asked
on
Medium Priority
166 Views
Last Modified: 2010-03-31
I have the following code that is supposed to create a new object from a previously saved one I'm probably  overlooking something really obvious here, but can someone give me some pointers.

        if ((inFileOpen==false) && fileSaved==(false))
        {
            try {      
                FileDialog fDlg = new FileDialog(this,"Input File",FileDialog.LOAD);
                fDlg.show();
                fileName = fDlg.getFile();
               
                if (fileName != null)
                {
                    FileInputStream input = new FileInputStream(fileName);
                    ObjectInputStream  objIn = new ObjectInputStream(input);
                    newStore = (Store) objIn.readObject();
                    objIn.close();
                    inFileOpen = true;
                }
            }
           
            catch(IOException e)
            {
                System.out.println("cannot open file");
                e.printStackTrace();
            }
        }
        else if ((inFileOpen=true) && (fleSaved=true))
        {
            newStore = objIn.readObject();
            objIn.close();
        }

I get a problem with it not recognising the file dialog constructor. Also it does not like the objIn, even though it is declared(above on line 11): ObjectInputStream  objIn = new ObjectInputStream(input);

   309.             newStore = (Store) objIn.readObject();
                                       ^---^
*** Semantic Error: No accessible field named "objIn" was found in type "StoreView".

A few of these.

   295.                     newStore = objIn.readObject();
                                       ^----------------^
*** Semantic Error: The method "java.lang.Object readObject() throws java.io.IOException, java.lang.ClassNotFoundException;" can throw the checked exception "java.lang.ClassNotFoundException", so its invocation must be enclosed in a try statement that catches the exception, or else thismethod must be declared to throw the exception.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
objIn is not in scope. Declare it outside your *first* block
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>
  newStore = objIn.readObject();
            objIn.close();
>>

That should be enclosed in an exception handling block
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
try this:

        try
        {
        ObjectInputStream objIn = null;
        if ((inFileOpen==false) && fileSaved==(false))
        {
            try {      
                FileDialog fDlg = new FileDialog(this,"Input File",FileDialog.LOAD);
                fDlg.show();
                fileName = fDlg.getFile();
               
                if (fileName != null)
                {
                    FileInputStream input = new FileInputStream(fileName);
                    objIn = new ObjectInputStream(input);
                    newStore = (Store) objIn.readObject();
                    objIn.close();
                    inFileOpen = true;
                }
            }
           
            catch(IOException e)
            {
                System.out.println("cannot open file");
                e.printStackTrace();
            }
        }
        if ((inFileOpen==true) && (fleSaved==true))
        {
            if (objIn!=null)
            {
               newStore = objIn.readObject();
               objIn.close();
             }
        }
        }
        catch (Exception ex)
        {
           ex.printStackTrace();
         }

Author

Commented:
objects: there is  just one flaw in your code - on line 31 of your reply:

               newStore = objIn.readObject();
               ^---------------------------^
*** Semantic Error: The type of the right sub-expression,
"java.lang.Object", is not assignable to the variable, of type
"Store".

CEHj is this what you meant?

       if ((inFileOpen==false) && (fileSaved==false))
       {
           try {
                       ObjectInputStream  objIn = new ObjectInputStream(input);
                       newStore = objIn.readObject();

                       FileDialog fDlg = new FileDialog(this,"Input File",FileDialog.LOAD);
                       fDlg.show();
                       fileName = fDlg.getFile();

                       if (fileName != null)
                       {
                               FileInputStream input = new FileInputStream(fileName);
                               objIn.close();
                               inFileOpen = true;
                       }
               }

               catch(IOException e)
               {
                       System.out.println("cannot open file");
                       e.printStackTrace();
               }
       }
       else if ((inFileOpen=true) && (fileSaved=true))
       {
           newStore = objIn.readObject();
           objIn.close();
       }

or did you mean another exception handling block was needed?

And in case anyonne was wondering there was a typo in my original post, the statement  5 lines up from  the bottom should have had  an 'i' in 'fileSaved'.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> newStore = objIn.readObject();

Change it to: newStore = ( Store ) objIn.readObject () ;
CERTIFIED EXPERT
Top Expert 2016
Commented:
ObjectInputStream objIn will have to be declared somewhere before your try statement. This has to be the case or your code:

>>
else if ((inFileOpen=true) && (fleSaved=true))
        {
            newStore = objIn.readObject();
            objIn.close();
        }
>>

would have no 'objIn' to operate on. You can use the same reference, so there's no need to redeclare it here:

>>
FileInputStream input = new FileInputStream(fileName);
ObjectInputStream  objIn = new ObjectInputStream(input);
>>















//==============================================================

                  try {
                        if ((inFileOpen == false) && fileSaved == (false)) {

                              FileDialog fDlg = new FileDialog(this, "Input File", FileDialog.LOAD);
                              fDlg.setVisible(true);
                              fileName = fDlg.getFile();

                              if (fileName != null) {
                                    FileInputStream input = new FileInputStream(fileName);
                                    objIn = new ObjectInputStream(input);
                                    newStore = (Store) objIn.readObject();
                                    inFileOpen = true;
                              }

                        } else if ((inFileOpen = true) && (fileSaved = true)) {
                              newStore = (Store) objIn.readObject();

                        }
                  } catch (Exception e) {
                        e.printStackTrace();
                  } finally {
                        objIn.close();
                  }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sorry about the whitespace ;-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
            newStore = objIn.readObject();
               ^---------------------------^
*** Semantic Error: The type of the right sub-expression,
"java.lang.Object", is not assignable to the variable, of type
"Store".

Thats not what I posted, I posted:

    newStore = (Store) objIn.readObject();
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Thats not what I posted, I posted:


>>try this:

...


>>
if (objIn!=null)
{
      newStore = objIn.readObject();
      objIn.close();
}
>>             
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
At one point, it is: >> newStore = objIn.readObject();

:)
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.