0

I had asked a question about implementing a class by subclass in Java previously and I found a different approach used.(different for me!) URL: here

public void paintComponent(Graphics g){

this line was confusing for me, for a class was passed as a parameter. Well, I thought of giving a try and understand before I asked here. Here's my code:

public class parameterObject{

String name;
int age;

public parameterObject(){}
public parameterObject(String inputName,int inputAge){

    name = inputName;
    age = inputAge;

}
public void testObject(){

System.out.println(name);
System.out.println(age);
}

}

and in the next class, I used a method to pass parameterObject as a parameter (in bjueJ environment)

public void testFunction(parameterObject pO, int a){
 pO.testObject();
}

when I called the method, testFunction(...)It asked for parameter values and I entered "arpan",19 and 20. error: expected.. and then I tried using null for the objectParameter and then the JVM threw nullPointException error.. what was supposed to happen and what is lacking in my understanding? please help.

Community
  • 1
  • 1
user3519322
  • 69
  • 1
  • 1
  • 7
  • No class is being passed as a parameter. An instance of Graphics is being passed as a parameter. Your problem is obscure and your question unclear. – user207421 Apr 15 '14 at 00:59

2 Answers2

0

if i understand what you did , i think you did like that:

X.testFunction("arpan", 19, 20);

which is not right, u have to do like this:

X.testFunction(new parameterObject("arpan", 19), 20);
Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37
0

If I understand correctly testFunction expects two parameters: First p0 of type parameterObject and then a of type int.

Did you call the method like this: testFunction("arpan", 19, 20)?

This call wouldn't "match the method signature". That means the types of the values you passed in don't match with those the method expects. "arpan" is a String and 19, 20 are two int.

You could do something like this instead:

parameterObject abc = new parameterObject("arpan", 19);
testFunction(abc, 17);  // one parameterObject and one int - just like testFunction expects
FelixM
  • 1
  • 3