|
|
Hello,
this is a simple question I think. I am trying to get the array of bytes as a reference from method, although in the method in creates the array, outside returns the original initialized with null. How to get it from routine, but as parameter since routine returns boolean. Maybe routine should return class containing the array and boolean variable.
This is the code:
public class PassingRefs{
static boolean varbyteobject(byte [] obj)
{
System.out.println("---inside routine---");
int rndlen=(int)(Math.round(Math.random()*50)+1);
obj=new byte[rndlen];
System.out.println(obj.length);
return true;
}
public static void main(String [] args){
byte [] inpobj=null;
varbyteobject(inpobj);
System.out.println("---out from routine---");
System.out.println(inpobj.length);
}
}
and this is what I get after execution:
---inside routine---
29
---out from routine---
Exception in thread "main" java.lang.NullPointerException
at PassingRefs.main(PassingRefs.java:18)
Kujtim
|
|
|
|
|
In Java, all method parameters are passed by value, not by reference.
In other words, when you call varbyteobject in your code, it does not pass the reference to the original byte array. It passes a copy of the reference to the byte array. So, this line of code:
obj=new byte[rndlen];
has no effect on the original reference to the byte array in the calling main method since you are updating the copy, not the original.
It is explained in more detail in this article:
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html[^]
You can just return the new byte array from your method. Why do you want to return a boolean?
|
|
|
|
|
SPIRANCA wrote:
static boolean varbyteobject(byte [] obj)
{
System.out.println("---inside routine---");
int rndlen=(int)(Math.round(Math.random()*50)+1);
obj=new byte[rndlen];
System.out.println(obj.length);
return true;
}
varbyteobject(inpobj);
Why do you need to return a boolean ? Why not just pass the byte[] back?
Personally I would return an intialised byte[] if it was true and null if it was false .
static byte[] varbyteobject(byte[] obj)
{
...
return obj;
}
if ((inpobj = varbyteobject(inpobj)) != null)
System.out.println(inpobj.length);
else
System.out.println("Array not initialised.");
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
Possible solution would be to return a struct-class(since Java doesn't supports structs), so that it contains the byte array and the boolean variable(which tells about success or failure). Although we could check the success or failure from the byte array-some of the byte contain that info.
Thanks anyway,
Kujtim
|
|
|
|
|
Success or failure in what sense? Looking at the example code you have posted there is very little that can go wrong.
The convention in Java is that success of an operation like this is assumed and you can expect a byte array to be returned: if anything goes wrong and the operation cannot be completed successfully, an exception is thrown. This may be a checked or an unchecked exception. There is a lot of debate over which of these is better, and I have no desire to open that debate up here (we'll be here forever while people argue backwards and forwards). Lets just say that if the operation fails, an exception is thrown and the type of exception tells you what went wrong. The calling method then catches the exception and responds accordingly.
In other words, your method returns an intialised byte array. If the initialisation of the byte array fails, throw an exception indicating why it failed. I can't tell you what exception would be best because I don't know what you expect to go wrong from the example code you posted.
You certainly could make it work by returning a class containing the byte array and a success/failure flag but this is not the preferred way of doing it in Java.
|
|
|
|
|
This very very simplified part of the large project I am developing. It will manage (register, unregister, list, ...) clients to listen/write at serial(COM) and TCP/IP. So the byte array is actually stream that commes(input arg) and goes(returned by function) by the so called "bridge" I am developing. Bytes of the stream contain the info from / to the requester.
Hope this is more clear.
Kujtim
|
|
|
|
|
In that case, I would definitely use an exception to indicate failure. Personally I would use a checked exception in this case (puts on tin hat and runs for cover from the "I hate checked exceptions" brigade) but YMMV.
Returning a success indicator is a very C way of doing things. Using a class as an equivalent of a struct to hold the byte array and a yes/no success flag is the result of trying to force C coding practices onto Java, which is why it feels clumsy and inelegant.
|
|
|
|
|
The remote user issues registration request from the GUI. For that request we build a stream which contains all the details. It is executed on the host module, if succeeds it receives the answer upon success, if not it receives failure message stream. We defined dedicated PROTOCOL for this.
Any failure is catched and handled by try/catch internally. But the remote objects needs to communicate between, thus the protocol between them.
Kujtim
modified on Monday, July 27, 2009 9:42 AM
|
|
|
|
|
Well, I thought I understood your problem, but now I'm confused. It sounds as if you are passing data cross-process using a custom protocol. If it's a dedicated protocol that you have designed yourself, you are free to pass data in whatever format you want. I'm not sure what that has to do with returning data from Java method calls and I'm not sure what answer you're expecting to get from this forum so I will bow out at this point.
|
|
|
|
|
Am I right that what you require is that any change to the stored data is propogated back up the line to anyone interested? If so, then this sounds more like a subscribe/publish pattern.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
DOes this form includes question regarding javascripts??:S
|
|
|
|
|
|
Beat me to it.
Howeever javascript is a sub-set of java. [damned I can't keep a straight face ]
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
I know JAva != javascript but there is no form for asking about js so i thought may be here thanks anyways
|
|
|
|
|
Don't worry too much, but the Webbie people do more javascript and you'll get a better response there.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
hi
i have developed one application using openoffice and JOD convertor to convert a document in pdf. its working fine but problem is like i have to install openoffice on
my machine. is there other way using any opensource to convert a word document into pdf.
please dont suggest for POI and otehr api..because they not support fully conversion and we had tried it.
if u have any other suggestion please reply.
Thanks
|
|
|
|
|
After a quick Google search I found this[^].
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
Hi
Thanks for suggestion but i have already told u like i want to implement using openoffice and JOD convertor ...but now i have to install openoffice in my system..i want to avoid installation of Openoffice just need to include some jar file..
if is there any way...Thanks
|
|
|
|
|
JODConverter must connect to an OpenOffice.org instance in order to work. Typically this is done by connecting to the openoffice.org service on the local machine, hence needing openoffice installed. However, it can connect to an instance on another machine. Have a look at this[^].
If you don't want to connect to different machines and don't want to install open office locally then I suggest using a different API.
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
Thanks For Suggestion,I thought if i will go for openoffice and JOD convertor then i have to use OO service. it is mandatory thing right ? Thanks for Reply..
i already tried other api .if u have any idea about some other api please suggest me
|
|
|
|
|
Hi people,
I am very new to webservices. I been research on webservices and i understand it is something similar to an API.
I am working on a project that require the use of an GSM Modem. Its like a generic SMS Processing Agent that anyone can use it instead of building the capability into each application that needs it.
I am using an opensource sms library called "SMSlib".
So basically i need to create a web service that allows applications to interface with the processing agent to
-retrieve sms
-send sms
-keep a transaction log of communications with other applications.
So, can someone give me an advice on what kinda of methods do i need to have in my web service in order to achieve my goals.
Thanks
|
|
|
|
|
|
Hi i am new to java,
we have a project of taking that big one file and save the two classes inside two diffrent files, but i recieve an error
the error is :
D:\>java MonkiesDemo.java
Exception in thread "main" java.lang.NoClassDefFoundError: MonkiesDemo/ja
Caused by: java.lang.ClassNotFoundException: MonkiesDemo.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: MonkiesDemo.java. Program will exit
the project i have to make is :
http://www.abelski.com/moodle/mod/assignment/view.php?id=676[^]
http://pastebin.com/m4bdad8f7[^]
http://pastebin.com/m52543652[^]
Thanks
|
|
|
|
|