Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

problem in Array -- Help please

Thread view: 
ssaa - 03 Apr 2004 09:46 GMT
Hi,
when i try to call the following class function "set_world(String w)"
from my main class, i am always getting error "error found". it looks
like the problem is with String array "worlds". but i cannot understand.
   can anyone figure out the problem and explain little bit.

Thanks alot.

***********************************************************

import java.io.*;
import java.lang.*;
import java.util.*;

class operations
{
    private String[] worlds;
    operations(){ }
    public void set_world(String w)
    {
        int count = 0;
        try
        {
        worlds[count] = "ABCD";
        }catch ( Exception ee)
        {System.out.println("error found"); }   
   
    }
   
    public String[] get_world()
    {
        return worlds;   
    }

   
}
ssaa - 03 Apr 2004 09:57 GMT
ALso please note that i am looking for dynamic array .

Thanks

> Hi,
> when i try to call the following class function "set_world(String w)"
[quoted text clipped - 32 lines]
>    
> }
Christophe Vanfleteren - 03 Apr 2004 10:04 GMT
> Hi,
> when i try to call the following class function "set_world(String w)"
[quoted text clipped - 31 lines]
>
> }

First of all, try to follow the naming conventions in your code:
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

The problem you're having, is that you're trying to use an array of Strings
that is not instantiated (so it is still null).

So change the line
private String[] worlds;
to
private String[] worlds = new String[SIZE_YOU_NEED];

You also do something very bad when catching the Exception. Instead of
seeing the actual error, you now only see your own message. At the very
least, do something like ee.printStacktrace(), which would have shown the
specific error you were having (NullPointerException), and on what line it
was.

Signature

Kind regards,
Christophe Vanfleteren

ssaa - 03 Apr 2004 10:10 GMT
Thanks alot for the hint, But As you said i need to use

private String[] worlds = new String[SIZE_YOU_NEED];

i was wondering, is there any way to create a dynamic array because my
array size can vary. what is your suggestion for using Vector for
storing string ?

Thanks once again.

=***********************************************

>>Hi,
>>when i try to call the following class function "set_world(String w)"
[quoted text clipped - 48 lines]
> specific error you were having (NullPointerException), and on what line it
> was.
Christophe Vanfleteren - 03 Apr 2004 10:33 GMT
> Thanks alot for the hint, But As you said i need to use
>
[quoted text clipped - 3 lines]
> array size can vary. what is your suggestion for using Vector for
> storing string ?

Please don't toppost.

One of the classes of the Collections framework will do:
http://java.sun.com/docs/books/tutorial/collections/

An implementation of the List interface (like ArrayList) resembles a
resizable array the most.

Signature

Kind regards,
Christophe Vanfleteren



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.