AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

problem in Array -- Help please

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
ssaa
Guest





PostPosted: Sat Apr 03, 2004 8:46 am    Post subject: problem in Array -- Help please Reply with quote



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;
}


}

Back to top
ssaa
Guest





PostPosted: Sat Apr 03, 2004 8:57 am    Post subject: Re: problem in Array -- Help please Reply with quote



ALso please note that i am looking for dynamic array .

Thanks


ssaa wrote:
Quote:
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;
}


}



Back to top
Christophe Vanfleteren
Guest





PostPosted: Sat Apr 03, 2004 9:04 am    Post subject: Re: problem in Array -- Help please Reply with quote



ssaa wrote:

Quote:
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;
}


}

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.



--
Kind regards,
Christophe Vanfleteren

Back to top
ssaa
Guest





PostPosted: Sat Apr 03, 2004 9:10 am    Post subject: Re: problem in Array -- Help please Reply with quote

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.


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


Christophe Vanfleteren wrote:
Quote:
ssaa wrote:


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;
}


}


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.





Back to top
Christophe Vanfleteren
Guest





PostPosted: Sat Apr 03, 2004 9:33 am    Post subject: Re: problem in Array -- Help please Reply with quote

ssaa wrote:

Quote:
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 ?

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.

--
Kind regards,
Christophe Vanfleteren

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.