Problems with recreating a saved object using ObjectInputStream

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.
LVL 1
mark_667Asked:
Who is Participating?
 
CEHJCommented:
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();
                  }
0
 
CEHJCommented:
objIn is not in scope. Declare it outside your *first* block
0
 
CEHJCommented:
>>
  newStore = objIn.readObject();
            objIn.close();
>>

That should be enclosed in an exception handling block
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
objectsCommented:
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();
         }
0
 
mark_667Author 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'.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> newStore = objIn.readObject();

Change it to: newStore = ( Store ) objIn.readObject () ;
0
 
CEHJCommented:
Sorry about the whitespace ;-)
0
 
objectsCommented:
            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();
0
 
CEHJCommented:
>>Thats not what I posted, I posted:


>>try this:

...


>>
if (objIn!=null)
{
      newStore = objIn.readObject();
      objIn.close();
}
>>             
0
 
Mayank SAssociate Director - Product EngineeringCommented:
At one point, it is: >> newStore = objIn.readObject();

:)
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.