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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

wat wrong with my code?

Hi,

i am not very familiar with  ObjectInputStream,
and not quite sure y i i can't see either "HAS TEXT" or "HAS NO TEXT" ??


import java.io.*;
import java.util.*;

class test
{
      private static ObjectInputStream objInStr;
      private static List theCategory;
      
      public static void main(String[] args)
      {
            System.out.println("~~1");

            try
            {
            System.out.println("~~2");
            
            objInStr = new ObjectInputStream( new FileInputStream( "c:\\data.dat" ) );
            theCategory = (List) objInStr.readObject();

            Iterator crsItr = theCategory.iterator();

            if( crsItr.hasNext() )
            {      
                  System.out.println("HAS NEXT");
            }
            else
            {
                  System.out.println("HAS NO NEXT");
            }

            }
            catch(Exception e)
            {
                  
            }
      
      }
}
0
tom_mk
Asked:
tom_mk
  • 17
  • 13
  • 2
  • +2
1 Solution
 
objectsCommented:
probably because an exception is being thrown
print out the exception so you know when it occurs

          catch(Exception e)
          {
               e.printStackTrace();
          }
0
 
tom_mkAuthor Commented:
result



~~1
~~2
java.lang.ClassCastException: java.util.Collections$SynchronizedMap
        at test.main(test.java:19)
Press any key to continue...
0
 
tom_mkAuthor Commented:
line 19 : theCategory = (List) objInStr.readObject();
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
the object you are reading from that file is not a List.
0
 
objectsCommented:
try:

     private static Map theCategory;
     
     public static void main(String[] args)
     {
          System.out.println("~~1");

          try
          {
          System.out.println("~~2");
         
          objInStr = new ObjectInputStream( new FileInputStream( "c:\\data.dat" ) );
          theCategory = (Map) objInStr.readObject();
0
 
tom_mkAuthor Commented:
wat i can do with my iterator then?
0
 
objectsCommented:
to iterate thru the keys:

Iterator crsItr = theCategory.keySet().iterator();

to iterate thru the values:

Iterator crsItr = theCategory.values().iterator();
0
 
suprapto45Commented:
You do not need your iterator. You can check whether "theCategory is null or not" if null then you can print something.

The codes are derived from object's codes.

          try
          {
          System.out.println("~~2");
         
          objInStr = new ObjectInputStream( new FileInputStream( "c:\\data.dat" ) );
          theCategory = (Map) objInStr.readObject();

          if( theCategory != null)
          {    
               System.out.println("HAS NEXT");
          }
          else
          {
               System.out.println("HAS NO NEXT");
          }

Regards
Dave
0
 
objectsCommented:
> You do not need your iterator. You can check whether "theCategory is null or not" if null then you can print something.

you do need it, that doesn't tell you whats in the map
(and it will always be not null)
0
 
petmagdyCommented:
tom_mk,

try this:

    public static void main(String[] args)
     {
          System.out.println("~~1");

          try
          {
          System.out.println("~~2");
         
          objInStr = new ObjectInputStream( new FileInputStream( "c:\\data.dat" ) );
          System.out.println(objInStr.readObject().getClass().getName());
.....
.......

and post the result
0
 
tom_mkAuthor Commented:
how can i output the value of the obj i go through?

Tom
0
 
tom_mkAuthor Commented:
i think .. i gonna post the whole code here.. cos i don't really sure how to explain to you guys

0
 
objectsCommented:
import java.io.*;
import java.util.*;

class test
{
     private static ObjectInputStream objInStr;
     private static Map theCategory;
     
     public static void main(String[] args)
     {
          System.out.println("~~1");

          try
          {
          System.out.println("~~2");
         
          objInStr = new ObjectInputStream( new FileInputStream( "c:\\data.dat" ) );
          theCategory = (Map) objInStr.readObject();

          Iterator crsItr = theCategory.keySet().iterator();
          while( crsItr.hasNext() )
          {
              Object key = crsItr.next();
              Object value = theCategory.get(key);    
              System.out.println(key+"="+value);
          }

          }
          catch(Exception e)
          {
               e.printStackTrace();
          }
     
     }
}
0
 
suprapto45Commented:
Yes,

object is right. Thanks objects to remind me :)


Regards
Dave
0
 
tom_mkAuthor Commented:
this is wat i received from the code below

C:\EAS2>java -Djava.rmi.server.codebase=http://localhost:6767/  -Djava.security.
policy=java.policy client.EASClient
Working
:) 1
EXCEPTION*ClientFrame : java.lang.NullPointerException
current login id = null
current login id = initialValue
PROCESSING 1
PROCESSING 2
EXCEPTION*post : java.lang.NullPointerException
Exception in thread "main" java.lang.NullPointerException
        at javax.swing.DefaultComboBoxModel.<init>(Unknown Source)
        at javax.swing.JComboBox.<init>(Unknown Source)
        at client.PostAdPanel.addComboBox(PostAdPanel.java:129)
        at client.PostAdPanel.<init>(PostAdPanel.java:87)
        at client.ClientFrame.<init>(ClientFrame.java:83)
        at client.EASClient.main(EASClient.java:49)

C:\EAS2>pause
Press any key to continue . . .


ServerServiceImpl.java
-----------------------------------------
part of the code

public class ServerServicesImp extends CommServicesImp implements ServerServices
{
      private CategoryCollection theCategory;
      private ObjectInputStream objInStr;

      public void loadCategory() throws IOException, ClassNotFoundException, RemoteException
      {

            ObjectInputStream objInStr = new ObjectInputStream( new FileInputStream( "data.dat" ) );

            if (objInStr == null)
            {
                System.out.println("ERROR: Cannot load data.");
            
            }
            else
            {
                  System.out.println("category : loading **** ");
            }
            
            theCategory.load( objInStr );
            //objInStr.close();
      }
      
      
      public Vector grabLoadedCategory() throws RemoteException
      {
            Vector read = new Vector();
            
            Iterator crsItr = theCategory.iterator();
            
            while( crsItr.hasNext() )
            {
                  Category next = (Category) crsItr.next();
                  
                  String temp = next.getName();
                  read.add(temp);
                  
                  /*
                  Object[] categoryData = new Object[2];
                  categoryData[0] = next.getName();
                  categoryTableModel.addCategory( categoryData );
                  */
            }
            
            return read;
            
      }  

----------------------------------------------------
PostAdPanel.java
----------------------------------------------------
      private Vector tempCategory;            
      private String[] categorylist;


      private ServerServices connect;
      private      UserProfile loginProfile;

      
      public PostAdPanel(ServerServices easServices)
      {
            connect = easServices;
            
            // PANEL
            postP = new JPanel();
            postP.setLayout(new BoxLayout(postP, BoxLayout.PAGE_AXIS));
            
            try
            {
                  connect.loadCategory();
                  connect.grabLoadedCategory().copyInto(categorylist);
                  
                  if(categorylist != null)
                  {
                        System.out.println("*********** > : " + categorylist[1]);
                  }
                  else
                  {
                        System.out.println("*********** > : CATEGORY NOT EXIST");
                  }
                  
                  
                  
                  
            }
            catch(RemoteException e0)
            {
                  System.out.println("REMOTE EXCEPTION*post : " + e0.toString());
            }
            catch(IOException e1)
            {
                  System.out.println("IO EXCEPTION*post : " + e1.toString());
            }
            catch(Exception e2)
            {
                  System.out.println("EXCEPTION*post : " + e2.toString());      
            }
      

------------------------------------
CategoryCollection.java
------------------------------------------------
public class CategoryCollection implements Serializable {

      public CategoryCollection()
      {
          categories = Collections.synchronizedList( new ArrayList() );
      }
      
               public void load( ObjectInputStream objInStr ) throws IOException, ClassNotFoundException
      {
            categories = (List) objInStr.readObject();
            
            System.out.println("************** YHA **************" );      
            
            if(categories == null)
            {
                  System.out.println("category is NULL" );      
            }
            else
            {
                  System.out.println("category is NOT NULL" );
            }
      }
0
 
tom_mkAuthor Commented:
i think i forgot clientFrame.java

public class ClientFrame extends JFrame
{

               // global variable
      
      public static void setCurrentProfile(String theUser)
      {
              CurrentSessionUser = theUser;
      }
      
      public static String getCurrentUserProfile()
      {
            return CurrentSessionUser;
      }
      
      public ClientFrame(ServerServices easServices, CommServices adsServices)
      {
            //MYeasServices = easServices;
            
            this.easServices = easServices;
            this.adsServices = adsServices;      
            
            try
            {
                  System.out.println(":) 1");
                  
                  easServices.loadCategory();
                  
                  System.out.println(":) 2");
            }
            catch(RemoteException e0)
            {
                  System.out.println("REMOTE EXCEPTION*ClientFrame  : " + e0.toString());
            }
            catch(IOException e1)
            {
                  System.out.println("IO EXCEPTION*ClientFrame : " + e1.toString());
            }
            catch(Exception e2)
            {
                  System.out.println("EXCEPTION*ClientFrame : " + e2.toString());      
            }
0
 
objectsCommented:
theCategory is null, you need to initialise it:

     private CategoryCollection theCategory = new CategoryCollection();
0
 
tom_mkAuthor Commented:
is that the only error?

cos after i initialized it..

i received another error :(


C:\EAS2>java -Djava.rmi.server.codebase=http://localhost:6767/  -Djava.security.
policy=java.policy client.EASClient
Working
:) 1
EXCEPTION*ClientFrame : java.lang.ClassCastException: java.util.Collections$Sync
hronizedMap
current login id = null
current login id = initialValue
PROCESSING 1
PROCESSING 2
EXCEPTION*post : java.lang.ClassCastException: java.util.Collections$Synchronize
dMap
Exception in thread "main" java.lang.NullPointerException
        at javax.swing.DefaultComboBoxModel.<init>(Unknown Source)
        at javax.swing.JComboBox.<init>(Unknown Source)
        at client.PostAdPanel.addComboBox(PostAdPanel.java:129)
        at client.PostAdPanel.<init>(PostAdPanel.java:87)
        at client.ClientFrame.<init>(ClientFrame.java:83)
        at client.EASClient.main(EASClient.java:49)

C:\EAS2>pause
Press any key to continue . . .
0
 
tom_mkAuthor Commented:
seems like

      connect.loadCategory();
      connect.grabLoadedCategory().copyInto(categorylist);

are not working... u think so?
0
 
objectsCommented:
>      categories = (List) objInStr.readObject();

looks like the following is causing you problems as it is a Map and not a List
see my earlier comments
0
 
tom_mkAuthor Commented:
i don't think so though

cos there is some other fucntion that uses this,, and it works fine

      public void load( ObjectInputStream objInStr ) throws IOException, ClassNotFoundException
      {
            categories = (List) objInStr.readObject();
            
            System.out.println("************** YHA **************" );      
            
            if(categories == null)
            {
                  System.out.println("category is NULL" );      
            }
            else
            {
                  System.out.println("category is NOT NULL" );
            }
      }
0
 
objectsCommented:
They could be reading a differnet file.

I don't see any of those println()'s appearing in your log.
0
 
tom_mkAuthor Commented:
tat the reason i don't understand though..

cos  i can't see any of "category is NULL" or "category is NOT NULL"
0
 
objectsCommented:
thats cause an exception is being thrown, caused by you attempting to cast a Map to a List
0
 
tom_mkAuthor Commented:
the problem is .i can not change it to MAp, b/c it it going to conflict with another func that is working.

any idea to go around this prob?

Tom
0
 
objectsCommented:
then write seperate method for reading a serialized map instead of a serialized List
0
 
tom_mkAuthor Commented:
can u suggest me how to do that?
0
 
objectsCommented:
have a look at the code I posted earlier to read a Map.
0
 
tom_mkAuthor Commented:
Obj,

wat do i need to import for MAP.... i think Map is just interafece, isn't it?

0
 
tom_mkAuthor Commented:
i found it.. let me try the code first
0
 
tom_mkAuthor Commented:
i don't see any result back from the file...any idea?

      public void loadCategory() throws IOException, ClassNotFoundException, RemoteException
      {
            System.out.println("~~1");
            try
            {
                  System.out.println("~~2");
                  ObjectInputStream objInStr = new ObjectInputStream( new FileInputStream( "c:\\data.dat" ) );

                  if (objInStr == null)
                  {
                      System.out.println("ERROR: Cannot load data.");
                  }
            
                  theCategory2 = (Map) objInStr.readObject();

                Iterator crsItr = theCategory2.keySet().iterator();
         
                while( crsItr.hasNext() )
                {
                        Object key = crsItr.next();
                    Object value = theCategory2.get(key);    
                    System.out.println(key+"="+value);
                }
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }      
      }
0
 
tom_mkAuthor Commented:
i tried this..(code below) i can see ""WORKING: Can load data." but i never see "~~5",

public void loadCategory() throws IOException, ClassNotFoundException, RemoteException
      {
            System.out.println("~~1");
            try
            {
                  System.out.println("~~2");
                  ObjectInputStream objInStr = new ObjectInputStream( new FileInputStream( "c:\\data.dat" ) );

                  if (objInStr == null)
                  {
                      System.out.println("ERROR: Cannot load data.");
                  }
                  else
                  {
                        System.out.println("WORKING: Can load data.");
                  }
                  
                  System.out.println("~~3");
            
                  theCategory2 = (Map) objInStr.readObject();

                Iterator crsItr = theCategory2.keySet().iterator();
                  
                  System.out.println("~~4");

                while( crsItr.hasNext() )
                {
                        System.out.println("~~5");

                        Object key = crsItr.next();
                    Object value = theCategory2.get(key);    
                    System.out.println(key+"="+value);
                }
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }      
      }
0
 
objectsCommented:
the map must be empty
0
 
TrigunaCommented:
Hey, you cannot read / write an object in Java. So, the solution for you is to add the elements from List to a Vector and Send those vector elements from Client to Server or Vice Versa. I this will work.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 17
  • 13
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now