954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

append item to array

Hi there!

I have some problem with java. I developing a multiplayer applet, there a class called userServer registry new users and logging in other users. the problem is, that I don't
know how to append in a array.

user Users[];
int cnt=0;
Users[cnt] = new user("emorjon2", "password");
cnt++;


is that the right way to append a item to an array??;:?: I myself don't think so, because
when I'm running the program it will be compiled without problem,
but then it print's out NullPointerException, what that means... :confused:

emorjon2
Newbie Poster
23 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

What you have is OK, just one thing missing...
you define a "users" variable, but you don't actually create an array, so the variable just contains a null pointer. You need something like
user Users = new user[99]; // creates an array with 99 slots for users

nb 1 Java convention is class names begin with a capital letter, variables begin with a lower-case letter - if you reverse this its confusing for other people

nb 2 Most people would use an ArrayList here rather than an array so they don'thave to worry about how big it is.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

thank's ^^

emorjon2
Newbie Poster
23 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

Mark this solved please.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: