File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of Objects NullPointer Exception

 
BharathiB balasubramanian
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

class test12 {

int x,y;

public void show(int xx,int yy)
{
x=xx;
y=yy;
System.out.println("TestTest"+x+y);
}

}

public class test1 extends Applet
{
public void init ()
{

test12 labelbh[]=new test12[4];

labelbh[0].show();
}

Null Pointer exception is thrown when I try to access a member function of test12
I am trying to create an array of objects
Thanks in Advance
Regards
Bharathi
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Bharathi!

arrays whose elements are of reference type are initialized to null. Initialize one of the elements of the array with something like labelbh[0] = new test12();

Notice we have a beginner's forum that seems more suitable for this question. This one is devoted to IO and streams.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per Jose's suggestion, moving to Java in General (Beginner).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic