Results 1 to 6 of 6
  1. #1
    Jana is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default NullPointer.exception in main (arrays)

    Hi,

    This might be probably obvious, but I can't figure out, why I'm getting this error. I'm trying to set the value of the first item in my array called "collection" of type Card1.

    CLASS is:

    public class Card1
    {
    private int value;
    private int suit;

    Card1(int val, int sui)
    {
    value = val;
    suit = sui;
    }

    public void setValue(int x)
    {
    value = x;
    }

    public void setSuit(int y)
    {
    suit = y;
    }
    }


    and the MAIN:

    public class X
    {

    public static void main(String[] args)
    {
    Card1 [] collection = new Card1[5];


    collection[0].setValue(5);
    collection[0].setSuit(6);
    System.out.print(collection[0]);

    }

    }

    Anyone? Thank you.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you never instantiate the individual cards in your array.

    All "Card1 [] collection = new Card1[5];" does is give you a new array with 5 uninstantiated references.

    before you can do .setValue() and so forth, you need to do do something like: collection[0] = new Card();

    note that this calls the constructor of Card, but instantiating your array did not.

  3. #3
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    I haven't tested your code, but could problem be in constructor ? There's need to be constructor with empty arguments when you want to initalize as array ?

  4. #4
    Jana is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default You're right. It's the instantiation

    I did not know that you have to instantiate objects within the main. So I figured it out this way (I instantiated the values of the "suit" and "value" attributes to zero):

    Java Code:
    public class X
    {
    
      public static void main(String[] args)
      {
    	  	Card1 [] collection = new Card1[5];
    	  	[B]for(int i = 0; i<5; i++)
    	  	{
    		  	collection[i] = new Card1(0,0);
    	  	}[/B]		
    		collection[0].setValue(5);
    		collection[0].setSuit(6);
    		System.out.print(collection[0].getValue() + "," + collection[0].getSuit());
    		System.exit(0);
    	}  	
    }
    Thank you anyway.J.

  5. #5
    Jana is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default Plus the CLASS - constructor one for this is

    I just post this as well as I modified the class in case someone else runs into the same problem so thats its intelligible: :)

    Java Code:
    public class Card1
    {
    	public int value;
    	public int suit;
    	
    	Card1(int val, int sui)
    	{
    		value = val;
    		suit = sui;
    	}
    	
    	public void setValue(int x)
    	{
    		value = x;
    		}
    	
    	public void setSuit(int y)
    	{
    		suit = y;
    		}		
    		
    	public int getValue()
    	{
    		return value;
    		}
    	
    	public int getSuit()
    	{
    		return suit;
    		}	
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 01:44 PM
  2. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 PM
  3. Main method with throws Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 02:48 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  5. Replies: 1
    Last Post: 08-06-2007, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •