AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Structs and null pointer exceptions

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and CORBA
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri Jun 30, 2006 12:11 am    Post subject: Structs and null pointer exceptions Reply with quote



I have an IDL looking something like this

module LMS
{

struct Alarm
{
string sensor;
string zone;
};

interface LMSIF
{
void raise_alarm(in Alarm aReading);
void send_alarm_to_rmc(in string alarm);
string test_case();
};
};

and a java file looking something like:

class LMSServant extends _LMSIFImplBase
{
Alarm al;
//functions here
public LMSServ()
{
//this line causes the problem.
al.sensor = "Sensor 1";
}

}

The problem i'm having is when i try to modify or write to the Alarm
struct i get a NullPointerException every time.
Am i doing something wrong?

Do i need to define anything extra to get an IDL struct to work as a
java class?
I tried redefining Alarm as a class with the same variables but this
caused all sorts of other errors..

Any help appreciated Smile
Back to top
Piet van Oostrum
Guest





PostPosted: Sat Jul 01, 2006 5:07 pm    Post subject: Re: Structs and null pointer exceptions Reply with quote



Quote:
jamesb457 (AT) gmail (DOT) com (J) wrote:

J> I have an IDL looking something like this
J> module LMS
J> {

J> struct Alarm
J> {
J> string sensor;
J> string zone;
J> };

J> interface LMSIF
J> {
J> void raise_alarm(in Alarm aReading);
J> void send_alarm_to_rmc(in string alarm);
J> string test_case();
J> };
J> };

J> and a java file looking something like:

J> class LMSServant extends _LMSIFImplBase
J> {
J> Alarm al;
J> //functions here
J> public LMSServ()
J> {
J> //this line causes the problem.
J> al.sensor = "Sensor 1";
J> }

J> }

J> The problem i'm having is when i try to modify or write to the Alarm
J> struct i get a NullPointerException every time.
J> Am i doing something wrong?

yes. You only declare the Alarm variable al, but you don't reserve any
memory for it. It initializes with null. In Java Corba structs are classes
and you have to create an instance with new. In contrast to C and C++ where
structs get memory automatically.

So Alarm al = new Alarm(); should solve it I think.
--
Piet van Oostrum <piet (AT) cs (DOT) uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: piet (AT) vanoostrum (DOT) org
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and CORBA All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.