Skip to main content
gamedev.net
🔒 Locked

Java 'struct' problem (null pointer exception, CORBA related)

Started by jack_bauer Jul 1, 2006 at 6:14 AM 0 replies 1.3k views
Original Post
jack_bauer
jack_bauer
If anyone is experienced with CORBA im trying to implement a struct specification in java and having a lot of trouble.. my IDL looks like the following module LMS { struct Alarm { string sensor; string zone; }; interface LMSIF { //functions and such declaared here }; }; From what i understad, a struct gets mapped directly to a java class, like the following public final class Alarm { public String sensor; public String zone; public Alarm() {} //default constructors, oublic Alarm(String _sensor, String _zone) {zone = _zone, sensor = _sensor} } Now im trying to access the values in the struct/class like so: class LMSServant extends _LMSIFImplBase { Alarm al; //functions here public LMSServant() { //this line causes the problem. al.sensor = "Sensor 1"; } } But I get constand NullPointerException errors when it hits the 'al.sensor='line. from a java point of view how does the code look? Any reason why it shouldnt be working? If anyone knows a bit of CORBA too, then again any ideas? I havent declared the class Alarm anywhere in the java files, should i be doing? I tried that but it started causing other problems..
Zahlman
Zahlman
In Java, variables of non-primitive type effectively are pointers. Well, they're sort of references and sort of pointers.

They're like C++ references in that:

- People call them references
- They cannot be 'invalid'
- You can't do pointer arithmetic on them
- They don't have an address
- You use '.' syntax with them rather than '->' syntax (but that's more to do with the rest of the language: there are no ->'s in Java)

They're like C++ pointers in that:

- java.lang.NullPointerException calls them pointers ;)
- They *can* be null
- They can be reseated - and as a corollary, assigning to them affects the pointer/reference, not the pointed-at/referred-to thing.

Anyway, the point being that 'Alarm al;' doesn't create any actual Alarm object - just a pointer/reference to one. You want 'Alarm al = new Alarm();'.

Note that this is quite consistent, and convenient, too:

Alarm a1 = new Alarm();Alarm a2 = new Alarm();a1 = a2; // a1 *aliases* a2 nowa2.ring(); // a1 "rings" too. Of course, there is only one actual object, so// "ring()" only happens once, but the point is that if "ring()" changes the// internal state of the object, then the change appears in a1 too, because it's// the same object.// And what of the Alarm that a1 used to refer to/point at? Well, it's// unrecoverable now, but this is NOT a memory leak in Java, because the// language is garbage-collected and you are *expected* to write things this way.void doRinging(Alarm a) {  a.ring(); // the caller sees any change to the internal state of 'a'  a = new Alarm(); // the caller does NOT receive the new Alarm. Also, the old  // Alarm is NOT garbage-collected, because the caller still is holding on to  // it. This is why it is correct to say that Java passes object references  // by value; it does not pass objects by reference.}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.