Javax run time error NullPointer from method " public void serialEvent( )"

hi lads, i got two pc commicate with each other through serial cable, but on the receiver end, i need 2 classes, class A is for taking the incoming data and put them into an array X, and the other class B will access the array X and taking the content out for further processing.

in class A, there is a method called "public void serialEvent(SerialPortEvent event)", inside this method, whenever data is available on the inputstream, they will be saved into an array X, also B will appear in here as well to access X, the code looks like the following,

public void serialEvent(SerialPortEvent event) {

switch(event.getEventType()) {

case SerialPortEvent.BI:

case SerialPortEvent.OE:

case SerialPortEvent.FE:

case SerialPortEvent.PE:

case SerialPortEvent.CD:

case SerialPortEvent.CTS:

case SerialPortEvent.DSR:

case SerialPortEvent.RI:

case SerialPortEvent.OUTPUT_BUFFER_EMPTY:

break;

case SerialPortEvent.DATA_AVAILABLE:

try {

while (inputStream.available() > 0) {

inputStream.read(tempBuffer);

X =A.storeInArray(tempBuffer);

B.accessX (X);//////Problems occurred!!!!!!!!

}

} catch (IOException e) {}

break;

}

}

At B.accessX (X), a run time error "NullPointerException" occurred, could anyone help me for that please? thanks in advance

[1417 byte] By [nerdEvera] at [2007-11-27 10:57:06]
# 1

B is null

Do try and use more descriptive names for variables classes and methods. There's nothing to be gained from using short names like that! Also, classes begin with Uppercase letters, everything else with a lower-case one. The exception being constants, such as Calendar.MONTH

georgemca at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 2

Thanks for replying...

In the program, i didnt use B, it is a long name that is called Receiverr, and it was intialized inside the constructor of A that is called BTManager in the program. Sorry about the confusion that i made.

the method public void serialEvent( ) is inside the class A (BTManager)

Message was edited by:

nerdEver

Message was edited by:

nerdEver

nerdEvera at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 3

> Thanks for replying...

>

> In the program, i didnt use B, it is a long name that

> is called Receiverr, and it was intialized

> inside the constructor of A that is called

> BTManager in the program. Sorry about the

> confusion that i made.

Cool. Always best to give us as little work as possible to do!

But my other point still stands - B (or it's namesake) is null

georgemca at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 4

Cheers buddy, i will try it anyway

nerdEvera at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 5

Where do you declare B? Is it an instance variable inside A? Do you initialise it properly? Moreover, you seem to use method accessX(X[]) as if it is a static method! Don't you get a warning like "trying to access a static method in a non-static way"?

ntalamaia at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 6

> Cheers buddy, i will try it anyway

Try what?

georgemca at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 7

tanx, ntalamai

B is declared in A and initialized in the constructor of A

inside function accessX(X[ ]), there is a static method used,that is System.arraycopy( ), would that be the reason? but i didnt get any wanrning actually

Message was edited by:

nerdEver

nerdEvera at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 8

tanx georgemc,

i tried different names for B, but didnt really work

nerdEvera at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 9

You have to find out which object is null. You can start by using

...

(if b==null || X==null) {

System.out.println("b: "+b+", X: "+X);

System.exit(1);

}

b.accessX(X);

...

You should also use the stack trace to identify which line exactly causes the exception. You can post the exception trace here, if you cannot find it your self.

ntalamaia at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 10

> tanx georgemc,

>

> i tried different names for B, but didnt really work

LOL sorry, no, I mean use clearer names so we can understand it better! It won't fix your problem. The reference 'B' is null, but you know that now :-)

Post the constructor of the A class. B can't be getting initialized, as you claim, or it wouldn't be null

georgemca at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 11

You have to find out which object is null. You can start by using

...

(if b==null || X==null) {

System.out.println("b: "+b+", X: "+X);

System.exit(1);

}

b.accessX(X);

...

tanx 4 ur effort, i did try that, and the system exits because b is null and it never go down to "b.accessX (X)", how can i make it not null?

nerdEvera at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 12

Post the constructor of A: you should use something like:

public A() {

...

B b = new B();

...

}

Also when posting code, try to use the code button. It makes your code look much nicer.

ntalamaia at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...
# 13

ntalamai , sorry for the late reply...

that is exactly where was causing the problem, i just solved it today..

tanx very much

nerdEvera at 2007-7-29 12:07:10 > top of Java-index,Core,Core APIs...