Running JNI within Java Applet problem.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, June 13, 2007 01:23 AM

Hello... I have problem with Java Applet for calling JNI method. I'm using NetBeans as my IDE. All I wanted to do is to call C language function to operate Laser Range Finder and get the data. I managed to get the data into Java from C using JNI, but problems occured when I tried to plot the data into Java Applet. Here is my Java source code: package ploturgscan; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; public class PlotURGScan extends Applet { private int arrayResults[][]; Boolean lock = new Boolean(true); int arraySize = -1; public void sendArrayResults(int results[][]) {    More>>

Hello...

I have problem with Java Applet for calling JNI method.


I'm using NetBeans as my IDE. All I wanted to do is to call C language function to operate Laser Range Finder and get the data. I managed to get the data into Java from C using JNI, but problems occured when I tried to plot the data into Java Applet.


Here is my Java source code:

			
package ploturgscan;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class PlotURGScan extends Applet {

private int arrayResults[][];
Boolean lock = new Boolean(true);
int arraySize = -1;

public void sendArrayResults(int results[][]) {
arraySize = results.length;
arrayResults = new int[results.length][];
System.arraycopy(results, 0, arrayResults, 0, arraySize);
}

public void displayArray() {
for (int i = 44; i <725 ; i++) {
System.out.print("(" + arrayResults[i][0] + ", " + arrayResults[i][1] + ") ");
if(i % 12 == 0) System.out.println();
}
System.out.println();
}

public void init() {
setBackground(Color.YELLOW);
}

public void start() {

int[][] ar = new int[769][2];
int count = 3;

for(int i = 0; i < 769; i++) {
for(int j = 0; j < 2; j++) {
ar[i][j] = count;
}
count++;
}

// Calling the native method.
PlotURGScan am = new PlotURGScan();
am.getURGScan(ar, am.lock);
am.displayArray();

repaint();
}

public void stop() { }

public void destroy() { }

public void paint(Graphics g) {
for(int i = 44; i < 725; i++) {
g.drawOval(arrayResults[i][0], arrayResults[i][1], 1, 1);
}
}

private native void getURGScan(int[][] ar, Boolean lock);

static {
System.load("/home/echo/dFiles/nbProjects/lib/libPlotURGScanNative.so");
}

}


here is my C source-code:

			
#include
#include
#include
#include "PlotURGScanNative.h"

using namespace VXV;

JNIEXPORT void JNICALL Java_ploturgscan_PlotURGScan_getURGScan
(JNIEnv *env, jobject jobj, jobjectArray elements, jobject lock) {

jobjectArray ret;
jint arraysize;
int asize;
jclass cls;
jmethodID mid;
jfieldID fid;
int localArrayCopy[769][2];

for(int i = 0; i < 769; i++) {
jintArray oneDim = (jintArray)env->GetObjectArrayElement(elements, i);
jint *element = env->GetIntArrayElements(oneDim, 0);
for(int j = 0; j < 2; j++) {
localArrayCopy[i][j]= element[j];
}
}

// Getting URG data.
int URG_X[769], URG_Y[769];
static const double step0_rad = -135.0 * 2.0 * M_PI / 360.0;

// Connect to the URG.
const char* device = "/dev/ttyUSB0";
URGCtrl urg;
if(urg.connect(device) < 0) {
printf("URG::connect: %s
", urg.what());
exit(1);
}

// Scan and fill the array.
urg.capture();
for(int i = 44; i < 725; ++i)
if(urg.length[i] < 20){
URG_X[i] = URG_Y[i] = 0;
}else{
URG_X[i] = (int)(urg.length[i]*cos(step0_rad + ((769-i) * 2.0 * M_PI / 1024)));
URG_Y[i] = (int)(urg.length[i]*sin(step0_rad + ((769-i) * 2.0 * M_PI / 1024)));
localArrayCopy[i][0] = URG_X[i];
localArrayCopy[i][1] = URG_Y[i];
}

// for(int i = 44; i < 725; ++i) printf("C++ result: %d [%d %d]
", i, localArrayCopy[i][0], localArrayCopy[i][1]);

// Create array to send back
jintArray row = (jintArray)env->NewIntArray(769);
ret = (jobjectArray)env->NewObjectArray(769, env->GetObjectClass(row), 0);

for(int i = 0; i < 769; i++) {
row = (jintArray)env->NewIntArray(2);
env->SetIntArrayRegion((jintArray)row, (jsize)0, 2, (jint *)localArrayCopy[i]);
env->SetObjectArrayElement(ret, i, row);
}

cls = env->GetObjectClass(jobj);
mid = env->GetMethodID(cls, "sendArrayResults", "([[I)V");
if (mid == 0) {
printf("Can't find method sendArrayResults
");
return;
}

env->ExceptionClear();
env->MonitorEnter(lock);
env->CallVoidMethod(jobj, mid, ret);
env->MonitorExit(lock);
if(env->ExceptionOccurred()) {
printf("error occured copying array back
");
env->ExceptionDescribe();
env->ExceptionClear();
}

fid = env->GetFieldID(cls, "arraySize", "I");
if (fid == 0) {
printf("Can't find field arraySize
");
return;
}

asize = env->GetIntField(jobj,fid);
if(!env->ExceptionOccurred()) {
printf("Java array size = %d
", asize);
} else {
env->ExceptionClear();
}

return;
}


Here is my output:

			
(-73, 128) (-73, 129) (-74, 133) (-73, 134) (-73, 136)
(-73, 136) (-72, 137) (-71, 137) (-71, 140) (-71, 141) (-70, 141) (-68, 141) (-66, 138) (-65, 139) (-64, 138) (-63, 139) (-63, 140)
(-62, 141) (-63, 144) (-63, 147) (-61, 145) (-61, 147) (-60, 148) (-58, 146) (-57, 146) (-56, 146) (-52, 139) (-51, 137) (-50, 138)
(-48, 134) (-47, 134) (-46, 134) (-45, 134) (-43, 131) (-42, 132) (-41, 131) (-40, 131) (-40, 132) (-38, 129) (-37, 128) (-36, 129)
(-36, 130) (-34, 127) (-33, 127) (-32, 126) (-31, 127) (-30, 126) (-29, 124) (-29, 126) (-28, 124) (-27, 125) (-26, 124) (-25, 123)
(-24, 123) (-24, 125) (-23, 123) (-22, 124) (-21, 122) (-20, 122) (-19, 121) (-18, 120) (-18, 121) (-17, 121) (-16, 120) (-15, 119)
(-14, 119) (-13, 119) (-13, 119) (-12, 119) (-11, 119) (-11, 119) (-10, 119) (-9, 117) (-8, 117) (-7, 116) (-7, 116) (-6, 116)
(-5, 116) (-5, 116) (-4, 116) (-3, 116) (-2, 115) (-2, 115) (-1, 113) (0, 115) (0, 116) (0, 116) (1, 113) (2, 113)
(2, 113) (3, 116) (4, 113) (4, 113) (5, 117) (6, 114) (7, 114) (7, 114) (8, 114) (9, 114) (9, 110) (10, 110)
(10, 110) (11, 110) (12, 110) (12, 110) (13, 109) (14, 110) (14, 109) (16, 114) (16, 109) (17, 114) (18, 116) (19, 115)
(20, 116) (21, 119) (21, 116) (22, 116) (23, 116) (23, 115) (24, 113) (24, 112) (25, 112) (25, 112) (26, 111) (27, 111)
(27, 111) (28, 112) (29, 112) (30, 111) (30, 111) (31, 111) (32, 110) (32, 109) (33, 109) (33, 108) (34, 108) (35, 108)
(37, 112) (37, 111) (37, 108) (38, 108) (38, 108) (39, 108) (40, 109) (41, 109) (42, 109) (43, 109) (43, 109) (44, 109)
(45, 109) (46, 111) (47, 111) (48, 110) (49, 110) (49, 110) (49, 107) (49, 106) (51, 109) (52, 109) (54, 111) (55, 111)
(56, 111) (56, 110) (57, 110) (56, 107) (56, 105) (57, 105) (56, 103) (57, 103) (59, 105) (60, 105) (60, 104) (61, 104)
(60, 101) (61, 100) (61, 100) (63, 101) (64, 101) (65, 101) (67, 103) (67, 102) (68, 102) (68, 101) (67, 98) (68, 98)
(70, 100) (71, 100) (72, 99) (75, 103) (76, 102) (76, 102) (77, 101) (79, 103) (80, 103) (81, 102) (81, 102) (82, 101)
(83, 101) (83, 99) (82, 97) (83, 97) (83, 96) (84, 96) (86, 98) (88, 98) (88, 97) (89, 97) (89, 96) (90, 96)
(91, 96) (92, 96) (92, 95) (94, 95) (94, 94) (95, 94) (96, 94) (97, 93) (97, 93) (99, 93) (100, 93) (100, 91)
(101, 92) (101, 90) (104, 92) (101, 88) (102, 88) (102, 87) (103, 86) (103, 85) (103, 85) (104, 84) (104, 83) (108, 85)
(108, 84) (110, 84) (109, 83) (112, 84) (113, 83) (116, 85) (114, 82) (114, 81) (115, 81) (119, 82) (121, 83) (121, 82)
(123, 82) (122, 80) (123, 80) (122, 78) (124, 78) (124, 77) (125, 77) (126, 76) (126, 76) (120, 71) (120, 70) (127, 73)
(127, 72) (128, 71) (128, 70) (129, 70) (129, 69) (130, 68) (128, 66) (132, 67) (131, 66) (129, 64) (132, 64) (136, 65)
(135, 64) (136, 63) (137, 62) (139, 62) (137, 60) (138, 60) (138, 59) (139, 58) (139, 57) (139, 56) (142, 57) (140, 55)
(147, 56) (141, 53) (148, 55) (148, 54) (148, 53) (149, 52) (152, 52) (155, 52) (156, 51) (156, 50) (158, 50) (157, 48)
(158, 48) (159, 47) (159, 46) (157, 44) (158, 43) (158, 42) (158, 41) (165, 42) (169, 42) (170, 41) (170, 40) (168, 38)
(168, 37) (169, 36) (169, 35) (170, 34) (186, 37) (187, 36) (187, 34) (186, 33) (188, 32) (189, 31) (183, 29) (187, 29)
(189, 28) (188, 26) (186, 25) (188, 24) (186, 23) (185, 21) (185, 20) (184, 19) (183, 18) (182, 16) (182, 15) (182, 14)
(182, 13) (178, 12) (178, 10) (178, 9) (178, 8) (178, 7) (174, 6) (174, 5) (174, 4) (174, 3) (174, 2) (174, 1)
(177, 0) (172, -1) (172, -2) (172, -3) (167, -4) (167, -5) (167, -6) (165, -7) (164, -8) (164, -9) (164, -10) (163, -11)
(164, -12) (165, -13) (168, -14) (168, -15) (168, -16) (168, -17) (166, -18) (166, -19) (166, -20) (166, -21) (166, -22) (166, -23)
(164, -24) (162, -25) (161, -26) (159, -26) (161, -28) (161, -29) (161, -30) (159, -30) (159, -31) (159, -32) (158, -33) (158, -34)
(158, -35) (156, -36) (156, -37) (155, -37) (154, -38) (153, -39) (154, -40) (154, -41) (155, -42) (156, -44) (156, -45) (156, -46)
(155, -47) (154, -47) (154, -48) (154, -49) (152, -50) (152, -51) (152, -52) (151, -53) (151, -54) (151, -55) (150, -56) (150, -57)
(153, -59) (153, -60) (153, -61) (152, -62) (147, -61) (147, -62) (142, -61) (146, -63) (147, -65) (149, -67) (146, -67) (145, -67)
(145, -68) (139, -67) (139, -67) (139, -69) (139, -70) (138, -70) (138, -71) (143, -75) (143, -76) (143, -77) (141, -77) (140, -78)
(140, -79) (139, -80) (138, -80) (130, -77) (137, -82) (129, -78) (129, -79) (129, -81) (133, -84) (132, -85) (132, -86) (131, -86)
(131, -87) (137, -93) (129, -88) (128, -89) (128, -90) (129, -92) (128, -93) (128, -93) (128, -95) (127, -95) (127, -96) (126, -97)
(126, -98) (125, -99) (124, -99) (126, -102) (126, -103) (126, -104) (122, -103) (121, -103) (121, -104) (121, -105) (115, -102) (119, -107)
(119, -108) (116, -106) (115, -107) (118, -110) (117, -111) (116, -112) (115, -113) (115, -113) (114, -114) (115, -116) (114, -117) (115, -119)
(114, -120) (112, -119) (113, -122) (112, -123) (110, -121) (109, -122) (108, -122) (107, -123) (111, -128) (110, -129) (109, -130) (108, -130)
(107, -131) (107, -132) (104, -131) (105, -133) (104, -134) (103, -134) (101, -133) (102, -135) (100, -134) (98, -134) (97, -135) (96, -135)
(96, -136) (95, -137) (95, -138) (94, -139) (93, -140) (93, -141) (92, -141) (93, -145) (93, -147) (92, -148) (91, -148) (90, -149)
(88, -148) (88, -148) (87, -149) (87, -151) (86, -153) (85, -153) (84, -153) (83, -153) (82, -154) (80, -152) (79, -152) (79, -155)
(78, -156) (80, -163) (79, -163) (78, -164) (77, -164) (76, -164) (75, -164) (74, -165) (76, -171) (75, -172) (74, -172) (73, -173)
(70, -169) (68, -169) (67, -169) (68, -174) (67, -174) (66, -174) (63, -169) (62, -170) (56, -158) (54, -154) (53, -155) (52, -155)
(51, -155) (51, -157) (50, -158) (48, -156) (47, -156) (46, -157) (45, -156) (44, -156) (43, -157) (43, -162) (40, -154) (40, -156)
(39, -157) (37, -153) (36, -153) (35, -153) (33, -150) (32, -149) (31, -149) (30, -149) (29, -150) (28, -148) (27, -145) (26, -145)
(25, -144) (24, -144) (23, -145) (22, -144) (21, -143) (19, -138) (18, -138) (17, -138) (17, -138) (16, -137) (15, -136) (14, -136)
(13, -135) (12, -133) (11, -133) (10, -135) (10, -135) (9, -135) (8, -135) (7, -130) (6, -130) (5, -130) (4, -128) (3, -125)
(3, -125) (2, -125) (1, -124) (0, -125) (0, -125) (0, -124) (-1, -120) (-2, -119) (-2, -118) (-3, -118) (-4, -118) (-5, -119)
(-5, -117) (-6, -121) (-7, -121) (-7, -114) (-8, -112) (-9, -112) (-9, -112) (-10, -113) (-11, -112) (-11, -108) (-12, -108) (-12, -107)
(-13, -106) (-13, -106) (-14, -107) (-15, -108) (-17, -114) (-17, -114) (-18, -115) (-19, -115) (-19, -113) (-19, -109) (-20, -109) (-20, -108)
(-21, -108) (-22, -108) (-22, -108) (-23, -108) (-24, -108) (-24, -104) (-24, -102) (-24, -102) (-25, -101) (-26, -101) (-27, -102) (-27, -102)
(-28, -102) (-28, -101) (-28, -99) (-29, -98) (-30, -99) (-30, -98) (-31, -98) (-31, -97) (-32, -97) (-32, -97) (-34, -99) (-34, -99)
(-37, -103) (-37, -103) (-38, -103) (-38, -100) (-38, -100) (-38, -97) (-38, -95) (-38, -95) (-39, -95) (-39, -94) (-41, -97) (-42, -97)
(-41, -94) (-42, -93) (-42, -93) (-43, -93) (-44, -94) (-45, -95) (-46, -96) (-47, -95) (-47, -94) (-47, -93) (-47, -92) (-48, -92)
(-49, -92) (-47, -87) (-51, -93) (-52, -93)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at ploturgscan.PlotURGScan.paint(PlotURGScan.java:58)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.X11.XRepaintArea.paintComponent(XRepaintArea.java:56)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.X11.XComponentPeer.handleEvent(XComponentPeer.java:683)
at java.awt.Component.dispatchEventImpl(Component.java:4486)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at ploturgscan.PlotURGScan.paint(PlotURGScan.java:58)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.X11.XRepaintArea.paintComponent(XRepaintArea.java:56)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.X11.XComponentPeer.handleEvent(XComponentPeer.java:683)
at java.awt.Component.dispatchEventImpl(Component.java:4486)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Java array size = 769


As you can see the "Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException" is cousing the program to stop.


Does anybody know how to fix this?



Thank you very much.
   <<Less
About | Sitemap | Contact