• Post Reply Bookmark Topic Watch Topic
  • New Topic

object array..... help  RSS feed

 
sudharshiya mohan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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();

}
}
 
Nadeem Awad
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks for your reply. it helped me alot
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!