The Artima Developer Community
Sponsored Link

Java Answers Forum
Passing array of objects into methods

6 replies on 1 page. Most recent reply: Sep 17, 2003 7:52 AM by David

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Da Killah

Posts: 4
Nickname: dakillah
Registered: Sep, 2003

Passing array of objects into methods Posted: Sep 16, 2003 1:10 PM
Reply to this message Reply
Advertisement
I have a problem passing array of objects into functions. My problem is the following:

Class A, B, and C belong to the same package, and only the important code is shown below:

public class A {
public A() {}
}

public class B {

private A myObj1[];
public A() {
myObj1 = new A[25];
}

public A[] function1() {...}

public static void main(String[] args) {
B myObj2 = new B();
A myObj3[];
myObj3 = myObj2.function1();
}
}

public class C {

private B myObj4;
public C() {
myObj4 = new B();
}

public void function2() {
A temp[];
temp = myObj4.function1();
}
}

Notice how main method of class B is very similar to function2 method of class C.
MY problem is that when I run the main method of class B, it runs perfectly and the method runs

as I wanted it to run. When I run or call the function2 method from class C, Java gives me Null

Pointer Exception. I am using the same syntax in the same function! Why is one working and not

the other?! I even tried to initialize the temp variable before calling the function:

A temp[];
temp = new A[25];
temp = myObj4.function1();

And it still didn't work.

Please help! Thanks.


zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: Passing array of objects into methods Posted: Sep 16, 2003 2:58 PM
Reply to this message Reply
I think it is some auxiliary code there from which results the NullPointerException. The code pasted here is correct.

Da Killah

Posts: 4
Nickname: dakillah
Registered: Sep, 2003

Re: Passing array of objects into methods Posted: Sep 16, 2003 3:23 PM
Reply to this message Reply
The exception is being thrown exactly from this line.

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Passing array of objects into methods Posted: Sep 16, 2003 9:55 PM
Reply to this message Reply
did I miss my coffee that I am seeing wierd things here
public class B {
 
private A myObj1[];
 public A() {
 myObj1 = new A[25];
}
 


In class B how can u declare constructor of A??

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Passing array of objects into methods Posted: Sep 16, 2003 10:00 PM
Reply to this message Reply
if that is changed to constructor B the code is working fine boss

Da Killah

Posts: 4
Nickname: dakillah
Registered: Sep, 2003

Re: Passing array of objects into methods Posted: Sep 17, 2003 6:05 AM
Reply to this message Reply
I found it! My constructor for class C, didn't have the proper initialization:

instead of this:

private B myObj4;
public C() {
myObj4 = new B();
}


I had:

private B myObj4;
public C() {
B myObj4 = new B();
}

so I didn't initialize the private member myObj4, instead, I made a new one in the constructor which would never be accessed later, there my private member myOBj4 remained unitialized!

Thanks for the help guys.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Passing array of objects into methods Posted: Sep 17, 2003 7:52 AM
Reply to this message Reply
[frown]

You did the same on this forum as well...you posted code that doesn't have that error in it:
public class C {
 
private B myObj4;
public C() {
myObj4 = new B();
}

Flat View: This topic has 6 replies on 1 page
Topic: problems with JTable Previous Topic   Next Topic Topic: ResultSet going out of scope

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use