File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes copying string arrays returned from functions(urgent). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "copying string arrays returned from functions(urgent). " Watch "copying string arrays returned from functions(urgent). " New topic
Author

copying string arrays returned from functions(urgent).

sharath kv
Greenhorn

Joined: May 06, 2003
Posts: 20
Hi
I have 2 classes as follows:
public class A
{
String sv[] = new String[1000];
// Add values into sv.............
.........................
.........................
.........................
B k = new B(); // Call Class B
B.cards_output(sv); // invoke a method in Class B
} // End of Class A
public class B
{
// general declaration
public String copy_sv[] = new String[1000];
......
......
......
......
public void cards_output(String sv[]) // function in class B
{
for(int i=0;i<sv.length();i++)
{
copy_sv=sv;
}
}
} // End of Class B
I can only use copy_sv[] only in the functions that i pass that value to, I need copy_sv[] to be accessible everywhere in the program.
Now when ever I try to make a reference to copy_sv[] elsewhere in the program I am getting a Null pointer exception.
I tried using cloneable interface with clone method but again got a null pointer exception.
jim gotti
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
for one, in your for loop in Class B, you are using the String length method on an array.
for(int i=0;i<sv.length();i++) should be
for(int i=0;i<sv.length;i++) since sv is an array being passed to the method, not a string.
also the creation syntax of the array is a lil jumbled.
String sv[] = new String[1000]; should be
String[] sv = new String[1000];
this may be the cause of the null pointer...since there is no actual array being created, and you are trying to make reference to something that doesnt exist.
Also, in class A you have

you are creating a class type B reference with the name k...but you are calling the cards_output method of class B....with class B as the reference?
shoulnd it be

?
not sure if this is the complete program, but Class A seems to have no purpose. Seems you can accomplish what you want by creating an array of strings in a main method, and invoking the copy method of class B from main.
Also if you want to copy an array, im not sure just redirecting its reference will so. This will just make two references point to one array.
something like

may be what you are attempting to do. at least this way , you will have two seperate arrays, with the same info...an actual copy instead of two diff references pointing to one array.

Also, if you want to use methods of Class A from B, you will have to utilize inheritence and make B extend A, or put them in the same package.
[ May 16, 2003: Message edited by: jim gotti ]
[ May 16, 2003: Message edited by: jim gotti ]
[ May 16, 2003: Message edited by: jim gotti ]
jim gotti
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
heh i see you cross posted in the advanced section.
at least post here to know if my input was of any use, i am still learning Java, but would really like to know if what i am saying [typing] is actually of any use (which i can also use to learn from)
thnx
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Originally posted by jim gotti:

String sv[] = new String[1000]; should be
String[] sv = new String[1000];

Just to reply here, there is no difference between these two statements. Java accepts both String[] sv and String sv[] (The latter syntax is accepted to make the transition to java easier for c/c++ programmers...)
ALthough this thread (or the other) should probably be closed....


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Originally posted by Joel McNary:
ALthough this thread (or the other) should probably be closed....[/QB]

I see that Jim already did that. Never mind me....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copying string arrays returned from functions(urgent).
 
Similar Threads
Scrolling Problem
copying string arrays returned from functions(urgent)
copying string arrays returned from function(urgent)
Compile Error Vs runtime Error
why not NullPointerException