Click here to Skip to main content
15,929,346 members
Home / Discussions / Java
   

Java

 
AnswerRe: Different object types (references) Pin
harold aptroot10-Jul-09 8:10
harold aptroot10-Jul-09 8:10 
GeneralRe: Different object types (references) [modified] Pin
Kujtim Hyseni10-Jul-09 8:17
Kujtim Hyseni10-Jul-09 8:17 
GeneralRe: Different object types (references) Pin
harold aptroot10-Jul-09 8:34
harold aptroot10-Jul-09 8:34 
GeneralRe: Different object types (references) Pin
Kujtim Hyseni10-Jul-09 9:49
Kujtim Hyseni10-Jul-09 9:49 
GeneralRe: Different object types (references) Pin
harold aptroot10-Jul-09 10:07
harold aptroot10-Jul-09 10:07 
GeneralRe: Different object types (references) Pin
Kujtim Hyseni10-Jul-09 10:43
Kujtim Hyseni10-Jul-09 10:43 
GeneralRe: Different object types (references) Pin
harold aptroot10-Jul-09 10:51
harold aptroot10-Jul-09 10:51 
AnswerRe: Solution - Different object types (references) Pin
Kujtim Hyseni10-Jul-09 10:54
Kujtim Hyseni10-Jul-09 10:54 
Hello,

I just solved the problem. It was because the instance was not created with ObjectType ot[] = new ObjectType[3]; so I had to add also the lines ot[0]=new ObjectType(); , ot[1]=new ObjectType(); and ot[2]=new ObjectType(); after that.

The working code looks as follows:

class DiffObjectClasses{
	public static void main(String [] args)
	{
		SerialObject so;
		IPObject iob;
		
		ObjectType ot[] = new ObjectType[3];
		
		ot[0]=new ObjectType();	// This was added!!!	
		ot[0].objectID=1;
		ot[0].objectType=1;
		so=new SerialObject();
		so.serialPortNo=21;
		ot[0].objectContent=so;
		
		ot[1]=new ObjectType();	// This was added!!!
		ot[1].objectID=2;
		ot[1].objectType=1;
		so=new SerialObject();
		so.serialPortNo=21;
		ot[1].objectContent=so;
		
		ot[2]=new ObjectType();	// This was added!!!
		ot[2].objectID=3;
		ot[2].objectType=2;
		iob=new IPObject();
		iob.host="localhost";
		iob.port=2200;
		ot[2].objectContent=iob;
		
		System.out.println("----------------------------");
		for(int indx=0; indx<ot.length;indx++)
		{
			System.out.println("ObjID:"+ot[indx].objectID);
			System.out.println("ObjType:"+ot[indx].objectType);
			switch(ot[indx].objectType)
			{
				case 1:
						so=(SerialObject)ot[indx].objectContent;
						System.out.println("Serial port no:"+so.serialPortNo);
						break;
				case 2:
						iob=(IPObject)ot[indx].objectContent;
						System.out.println("IP host:"+iob.host);
						System.out.println("IP port:"+iob.port);
						break;		
			}
		}
	}
}

class ObjectType extends Object{
	int objectID;
	int objectType;
	Object objectContent;
}

class SerialObject extends Object{
	int serialPortNo;
}

class IPObject extends Object{
	String host="";
	int port;
}


Thanks for other suggestions!
Question[Message Deleted] Pin
ana_ochia8-Jul-09 2:20
ana_ochia8-Jul-09 2:20 
AnswerRe: is there any chance that the Four square game be programmed using JAVA? Pin
Nagy Vilmos8-Jul-09 21:55
professionalNagy Vilmos8-Jul-09 21:55 
GeneralRe: is there any chance that the Four square game be programmed using JAVA? Pin
ana_ochia9-Jul-09 14:05
ana_ochia9-Jul-09 14:05 
GeneralRe: is there any chance that the Four square game be programmed using JAVA? Pin
Cedric Moonen9-Jul-09 20:22
Cedric Moonen9-Jul-09 20:22 
GeneralRe: is there any chance that the Four square game be programmed using JAVA? Pin
Nagy Vilmos9-Jul-09 22:03
professionalNagy Vilmos9-Jul-09 22:03 
QuestionLooking for some guideance or advice on messaging implementation Pin
tas28267-Jul-09 6:57
tas28267-Jul-09 6:57 
AnswerRe: Looking for some guideance or advice on messaging implementation Pin
David Skelly7-Jul-09 22:33
David Skelly7-Jul-09 22:33 
QuestionsortTable Help Pin
samo12055-Jul-09 9:49
samo12055-Jul-09 9:49 
AnswerRe: sortTable Help Pin
David Skelly5-Jul-09 22:10
David Skelly5-Jul-09 22:10 
Questionregarding LPD protocl Pin
kirankatta5-Jul-09 7:16
kirankatta5-Jul-09 7:16 
QuestionLoad balancing in p2p system Pin
nimmi priya4-Jul-09 19:59
nimmi priya4-Jul-09 19:59 
AnswerRe: Load balancing in p2p system Pin
Nagy Vilmos5-Jul-09 4:12
professionalNagy Vilmos5-Jul-09 4:12 
QuestionTraverse through the files and match words Pin
dharshani4-Jul-09 6:09
dharshani4-Jul-09 6:09 
QuestionPassword protected file Pin
ksss_maheshece3-Jul-09 20:11
ksss_maheshece3-Jul-09 20:11 
QuestionPassing a vector from java and accessing it in visual C++ Pin
Aditya223-Jul-09 0:25
Aditya223-Jul-09 0:25 
QuestionNeed help to finish attendance management system Pin
hruday872-Jul-09 20:01
hruday872-Jul-09 20:01 
AnswerRe: Need help to finish attendance management system Pin
Nagy Vilmos2-Jul-09 23:16
professionalNagy Vilmos2-Jul-09 23:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.