Creating array of a class

I am trying to add an array of a newly defined class to a working program. It complies ok, but when I run it I get "java.lang.NullPointerException" at line new. The statement at line old runs
just fine. How can the oldArray work and the newArray not?

Exerpted code:
                       
public class test extends JPanel implements ActionListener{
   public test(){
      ...
      function1();
      ...      
   public void function1(){
      oldArray[0].first=0;   // line old
      newArray[0].first=0;   // line new
      ...
   oldClass[] oldArray=new oldClass[5];
   newClass[] newArray=new newClass[5];
   ...
class oldClass{
   int first;
}
class newClass{
   int first;
}
jc31415Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
_lychee_Connect With a Mentor Commented:
do you ?accidentally? put an oldClass object into oldArray[0]?
cos if u don't, both statements shouldn't work... this is cos the statement

A[] a = new A[5];
only says that a is an array of size 5 of A, but A[0], A[1], ... A[4] are all null at the start.. you need to do something like A[0] = new A(); to "put" the object into the array...
0
 
shaveriCommented:
exactly.....
0
All Courses

From novice to tech pro — start learning today.