Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of Objects NullPointer Exception

 
BharathiB balasubramanian
Greenhorn
Posts: 1
  • 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
  • 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: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per Jose's suggestion, moving to Java in General (Beginner).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic