[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems with recreating a saved object using ObjectInputStream

Posted on 2006-03-31
11
Medium Priority
?
151 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.
0
Comment
Question by:mark_667
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16342826
objIn is not in scope. Declare it outside your *first* block
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16342845
>>
  newStore = objIn.readObject();
            objIn.close();
>>

That should be enclosed in an exception handling block
0
 
LVL 92

Expert Comment

by:objects
ID: 16346680
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:mark_667
ID: 16371754
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16371782
>> newStore = objIn.readObject();

Change it to: newStore = ( Store ) objIn.readObject () ;
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 16372062
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16372075
Sorry about the whitespace ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 16375747
            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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16375862
>>Thats not what I posted, I posted:


>>try this:

...


>>
if (objIn!=null)
{
      newStore = objIn.readObject();
      objIn.close();
}
>>             
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16378200
At one point, it is: >> newStore = objIn.readObject();

:)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16403272
:-)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 9 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question