🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: aus.computers.java
Article #606

Re: How to return an array of strings to an applet?

#606
From: Paul Murray
Date: Sun, 09 Jul 2000 00:00
62 lines
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
Technical Details
Message-ID: <i6Z95.8707$c5.23931@newsfeeds.bigpond.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8k6eef$uln$1@bugstomper.ihug.com.au>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d6db,2413761ff66947de
X-Google-Attributes: gid10d6db,public
From: "Paul Murray" <pmurray@nospam.bigpond.com>
Subject: Re: How to return an array of strings to an applet?
Date: 2000/07/09
Message-ID: <i6Z95.8707$c5.23931@newsfeeds.bigpond.com>
X-Deja-AN: 644211372
References: <8k6eef$uln$1@bugstomper.ihug.com.au>
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
X-Trace: newsfeeds.bigpond.com 963141134 139.134.123.16 (Sun, 09 Jul 2000 21:12:14 EST)
Organization: Telstra BigPond Internet Services (http://www.bigpond.com)
X-MSMail-Priority: Normal
NNTP-Posting-Date: Sun, 09 Jul 2000 21:12:14 EST
Newsgroups: aus.computers.java