Hey,
i googled around for a solution but seem unable to come up with a reasonable solution to the problem of passing a class as a parameter. I wish to mimic the syntax used in java like below :

setPenColor(new Color(255, 0, 255));

Where i can pass a new object as a parameter, however since no constructor has a return type, and if i use something like :

//output returns a hex string, part of RGBColor class
public String getOutput()
    {
        return output;

    }

//the add function in the pen class
public int add(String c) {
        System.out.print(c);
        return 0;
    }

pen.add(new RGBColor(233,233,233).getoutput);

Then it returns a null object, can anyone link me or explain a way to pass a new instance without previously instantiating an object or without refering a specific type to the add function please :)

I think you are confused in passing a class as a paramater,
You can pass class as a paramater...

Lets Take an example

//----------- Class a -----------------
class a
{
    private int i;
    a(int j)
    {
        i = j;
    }

    public void print()
    {
    System.out.println(i);
    }
}


//----------------- Class b
class b
    {
        b(a A)
        {
            A.print();
        }
    }


//---------------- Main Class which is controlling the other classes    
public class c
{
    public static void main(String args[])
        {
            b B1  = new b(new a(10));
            b B2  = new b(new a(10));
        }
}       

Here i have taken two class a and b, while c is only for central controlling the two. b class constructor takes a class parameter and print it in the constructor.

Edited 4 Years Ago by Majestics

Does the code have compiler error messages? Please post the full text of the error messages.

de have compiler error messages? Plea

when i try to do it as examples above i get :

Exception in thread "main" java.lang.NullPointerException
    at RGBColour.<init>(RGBColour.java:6)
    at compiler.main(test.java:31)

The instancing of a class for a parameter is fine, but as soon as i try to use the getOutput function i get a null pointer.

Where is the code where you actually store something in the field named output?

Exception in thread "main" java.lang.NullPointerException
at RGBColour.<init>(RGBColour.java:6)

What variable at line 6 has a null value. That is causing the NPE.

getOutput() is a method, but you call it without its () - yet there's no compiler message?
Do you have a variable with the same name?

Edited 4 Years Ago by JamesCherrill

also: getoutput is not the same as getOutput
java is case sensitive. you are telling your method not that you are passing a color, but you are passing the (non-existant) variable getoutput.

 //the add function in the pen class
 public int add(String c) {
         System.out.print(c);
         return 0;
     }

why exactly does your add method return anything? since the result is always the same, it might as well be void.

Roger the case error, but getoutput must be defined (but not initialised) somewhere or you would get a compiler error, not a run-time NPE

This question has already been answered. Start a new discussion instead.