Thread View: aus.computers.java
2 messages
2 total messages
Started by PrEdAt0r
Sat, 08 Jul 2000 00:00
How to return an array of strings to an applet?
Author: PrEdAt0r
Date: Sat, 08 Jul 2000 00:00
Date: Sat, 08 Jul 2000 00:00
29 lines
1339 bytes
1339 bytes
Hi i have an array of strings that that has to be returned to an
appletclass.
unfortunately i m getting a nullpointerexception.
i dont know why
here is the code of the method that returns the array and the calling code
of the applet
public String[] getOutOfStockItems(){
int itemsOOS = 0;
for(int i = 0; i < it.length;i++)
if(it[i].getQuantity() < 1)
itemsOOS += 1;
osi = new String[itemsOOS];
for(int i = 0; i < it.length; i++)
if(it[i].getQuantity() < 1)
osi[i] = it[i].getName();
return osi;
}
Calling code
if (e.getSource()==bt_reorder){
String[] array = Stock.getOutOfStockItems();
for(int i = 0; i<array.length; i++){
tf_reorder.setText(tf_reorder.getText()+" "+array[i]);
}
}
Help given will be appreciated.
Re: How to return an array of strings to an applet?
Author: Paul Murray
Date: Sun, 09 Jul 2000 00:00
Date: Sun, 09 Jul 2000 00:00
62 lines
3213 bytes
3213 bytes
"PrEdAt0r" <navneet@lisp.com.au> wrote in message
news:8k6eef$uln$1@bugstomper.ihug.com.au
> Hi i have an array of strings that that has to be returned to an
> appletclass.
> unfortunately i m getting a nullpointerexception.
> i dont know why
> here is the code of the method that returns the array and the calling code
> of the applet
>
> public String[] getOutOfStockItems(){
> int itemsOOS = 0;
> for(int i = 0; i < it.length;i++)
> if(it[i].getQuantity() < 1)
> itemsOOS += 1;
>
> osi = new String[itemsOOS];
>
> for(int i = 0; i < it.length; i++)
> if(it[i].getQuantity() < 1)
> osi[i] = it[i].getName();
> return osi;
> }
Yep. Unless the entire array contains only oos items, this is going to throw
an ArrayIndexOutOfBounds exception. This is possibly coming through to the
applet client as a null return value, which is giving you your null pointer
exception at array.length in the for(;;). If you had caught and reported RMI
errors instead of quietly ignoring them (which I suspect is what you have
done <g>), you'd have had a better idea of what is going on.
suppose that it[] contains fify items, and the only out of stock one is
it[49]. You code will create an array osi[1], and attempt to do a
osi[49] = it[49].getName(),
which will detonate. There are two ways to code this.
1 - the wrong way. Use a separate variable j as your index into osi[], and
increment it inside the if() of the second loop
int j = 0;
for(int i = 0; i < it.length; i++)
if(it[i].getQuantity() < 1)
osi[j++] = it[i].getName(),
2 - the right way. Code this up with an ArrayList.
import java.util.*;
List osi = new ArrayList();
for(int i = 0; i < it.length;i++)
if(it[i].getQuantity() < 1)
osi.add(it[i].getName());
return osi;
Before doing any more coding, read the tutorials available on the
collections API, and get used to using it. If you are concerned about the
performance hit, think about it the way your employer will. Processor power
and memory is cheap. Programmer time (development & maintainence) and
business downtime due to these kind of array bugs is expensive. Take the
performance hit, and use an API that does what you want without you having
to count array indexes.
--------------------------------------------------------------
http://www.users.bigpond.com/pmurray/index-2.html
ICQ: 26066755
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads