CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Posts
    13

    Question 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.

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    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.

  3. #3
    Join Date
    Apr 2007
    Posts
    442

    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.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Save variable into array position?

    Quote Originally Posted by LoopString View Post
    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.

  5. #5
    Join Date
    Oct 2009
    Posts
    8

    Talking 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
  •  





Click Here to Expand Forum to Full Width

Featured