Results 1 to 3 of 3
  1. #1
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default java.lang.NullPointerException at autonomic.PbilMain.main(PbilMain.java:12)

    I have a class called Product. I would like to refer to objects of the class by index. eg.

    Java Code:
    public class PbilMain {
    
    public static void main(String[] args) {
    Product p[] = new Product[4];
    p[1].description = "Another trial";
    p[2].description = "Yet another trial";
    p[3].description = "";
    p[4].description = "";
    }
    }
    Can anybody spot the problem. On running it, I get "Exception in thread "main" java.lang.NullPointerException at autonomic.PbilMain.main(PbilMain.java:12)"

    Thanks.

    Felissa :p

  2. #2
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default

    You declared space for the array but you still need to declare space for each element in the array. Also, remember that arrays are zero based. That means the first element in the array is [0] and the last is, in your case, [3]. Something like:

    Java Code:
     public class PbilMain {
         public static void main(String[] args) {
             Product p[] = new Product[4];
     
             for( int i = 0; i < p.length; i++ )
                 p[i] = new Product();
             
             p[0].description = "Another trial";
             p[1].description = "Yet another trial";
             p[2].description = "";
             p[3].description = "";
         }
     }
    Greetings.

    Daniel:o

  3. #3
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    Thank you, I did what you said to me and it went out for me.

    Thank you very much again.

    Felissa

Similar Threads

  1. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  2. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 PM
  3. Replies: 1
    Last Post: 08-06-2007, 09:11 AM
  4. Replies: 1
    Last Post: 07-16-2007, 06:32 AM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 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
  •