• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

object array..... help

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my program is as below and i m getting NullPointerException . how to create an array for object.



import java.io.*;
import java.util.*;
import java.lang.*;
class AccessPoint
{
public void display()throws IOException
{
int x=388,y=560;

System.out.println("accesspoint is located at ("+x+","+y+")");
}

}
class Ex
{
public static void main(String args[]) throws IOException
{

AccessPoint ap[] = new AccessPoint[5];
for(int j=0;j<5;j++)
ap[j].display();

}
}
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear sudharshiya,
You've created an array of AccessPoint object references, but the objects themselves are not created yet. You need to insert the following line in the for loop:


And by the way, the IOException is never thrown from the display method, so you don't need to declare it.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to expand on what Nadeem said: When you create the array with "new AccessPoint[5]," you have an array object. But until you assign AccessPoint instances, all the reference elements within the array have a default value of null. Therefore, the problem occurs when you try to dereference one of these null values with "ap[j].display();"
 
sudharshiya mohan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply. it helped me alot
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic