wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array of Objects NullPointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array of Objects NullPointer Exception" Watch "Array of Objects NullPointer Exception" New topic
Author

Array of Objects NullPointer Exception

BharathiB balasubramanian
Greenhorn

Joined: May 29, 2004
Posts: 1
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

As per Jose's suggestion, moving to Java in General (Beginner).


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of Objects NullPointer Exception