|
-
August 20th, 2010, 10:13 AM
#1
Save variable into array position?
Where do I go wrong here?
I do have
Code:
String[] car = null;
car[0] = "test";
I get this error:
Exception in thread "main" java.lang.NullPointerException
Does this mean that I have to throw a special Exception, how can i find out which one if that is the case?
At top of the code i have this as I usually do:
Code:
public static void main(String[] args) throws IOException {
Thankful for any help.
-
August 20th, 2010, 11:47 AM
#2
Re: Save variable into array position?
Read this link carefully:
http://download.oracle.com/javase/tu...ts/arrays.html
Read it, run the samples, experiment with the samples, compare their samples with your code. If you still have problems after that post back.
-
August 20th, 2010, 11:57 AM
#3
Re: Save variable into array position?
Your variable is not assigned to any value, hence it is null. You cannot access anything of a varible that has no value. Do remember, that having an array initialized, does not mean that whatever is stored in it gets initialized. All of this is generally well explained, in any Java textbook and Sun's online tutorials. It is recommended that you study these. You would not expect to learned a foreign laguage with no reference material, would you? For instance, "thinking in Java" is available online for absolutely free.
So, check up on these references. there is no point going over the very basics here, for they are well explained in piblic sources.
-
August 20th, 2010, 01:02 PM
#4
Re: Save variable into array position?
 Originally Posted by LoopString
At top of the code i have this as I usually do:
Code:
public static void main(String[] args) throws IOException {
Why do you have that 'throws IOException' ?
If you don't understand what it does, don't use it until you do.
Time is an excellent teacher; but eventually it kills all its students...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
August 23rd, 2010, 12:38 AM
#5
Re: Save variable into array position?
Hey LoopString,
The problem with your code is that you have simply declared the Array, but haven't allocated any memory location to it. Because of that when you are initializing a new Array element and assigning it a value, it goes nowhere and throws a NullPointerException. What you need to do is something like this,
Code:
public class ArrayTest {
public static void main(String args[]) {
String[] car = null; //Declare the array.
car = new String[10]; //Allocate memory to the array.
car[0] = "test"; //Assign the value to first element.
System.out.println(car[0]); //Print the value on console.
}
}
And one more thing, as dlorde said, don't use the things if you don't know them. IOException is for things related to JAVA I/O, and should not be used unless you are really doing some stuff related to I/O.
Plus it's not a good idea to have your main throwing any exceptions. Better way, use try-catch blocks for your risky code and print out the exception. That would help you understand what went wrong and why. Your main method throwing exception will simply crash the JVM if it encounters any exception.
I hope that was helpful.
Goldest
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|